Gentle Introduction to Operating System

  • View
    21

  • Download
    4

Embed Size (px)

DESCRIPTION

Gentle Introduction to Operating System. Anthony K. H. Tung( Associate Professor Department of Computer Science. http://www.comp.nus.edu.sg/~atung http://www.renren.com/profile.do?id=313870900&_ua_flag=93w. Objectives. - PowerPoint PPT Presentation

Transcript

  • Gentle Introduction to Operating SystemAnthony K. H. Tung(Associate ProfessorDepartment of Computer Science

    http://www.comp.nus.edu.sg/~atunghttp://www.renren.com/profile.do?id=313870900&_ua_flag=93w

  • ObjectivesOperating System in SOC is a second year course(CS2106) which have prerequisites of eitherCS1104/ CS2100 : Computer OrganizationEE2007: Microprocessor SystemsAim of this talk:Provide some hardware background so that you can understand OS betterExplain why instead of just how and what?Provide a framework that can merge different concepts together

  • ApproachWe will design an OS together, starting from a simple one and finally to a complex one:Single user, single processSingle user, multiple processesMultiple users, multiple processesWhile designing, we will take into consideration various desiderataEfficiencyRobustness to changesRobustness to errorsConsistencyIsolation

  • What is OS?As the name implied, operating system is a software system that operate something? What is that something?something=hardwareWhy do we need to put in a software layer that operate the hardware?To appreciate something, we need to ask ourselves what is something do not exist

  • In the beginningThere is no such thing as an OS, each user write their own program to control the hardware which have their own control code or instruction setExample: hard diskHave many tracksEach track have many sectorsData are stored in each sector as binary bits

    1010010100.

  • Eg. of Instruction Set for Hard DiskPhysically the disk is control by electronic circuitinstruction sector no. memory loc. 1 0 0 1 0 1 1 1 0 1 1 0

    CodeDescriptionBinary00Reset Disk Drives0001Check Drive Status0102Read Sectors From Drive1003Write Sectors To Drive11

  • Storing Data as FilesWhen there was no OS(i.e. no fgets, fopen), programmers have to organize the sectors into files in their programsExample: Use the first sector as file directory and indicate what are the sectors use to store data for each file. Figure on the right show file directory for two files, numbers and lettersnumbers, 2, 5, 8, -1letters, 3, 6, 1023, -1 . . .20, 4, 100, A, Z, B, H, .900, 1000, 50, B, C, I, A, .54, 20, 10,.O, R, Q, G Sector 2Sector 3Sector 4Sector 5Sector 6Sector 7Sector 8Sector 9Sector 1023Sector 1024Sector 1filenameEnd markerFile directory sector..

  • Storing Data as Files(II)How to read the file numbers?numbers, 2, 5, 8, -1letters, 3, 6, 1023, -1 . . .20, 4, 100, A, Z, B, H, .900, 1000, 50, B, C, I, A, .54, 20, 10,.O, R, Q, G Sector 2Sector 3Sector 4Sector 5Sector 6Sector 7Sector 8Sector 9Sector 1023Sector 1024Sector 1filenameEnd markerStep 1: 10, Sector 1, Mem. Location 20 // Read Sector 1 // into memory location 20Step 2: Search location 20 for filename=numbers;Step 3: sector_num = first sector of numbers;Step 3: While (sector_num-1) doStep 4: { 10, Sector sector_num, Mem. Location 21;Step 5: process data in mem. Location 21;Step 6: }

    Note: 10 is the hardware code for reading a sector of the disk..

  • What is the problem of such an approach?Difficulty in programming. Have to remember the command code and repeat the same thing in every program.Different programmers might implement the file differently. What if one of them resign?Not robust to change: What if a different brand of disk with different instruction code is used? i.e. 01 is used to read a sector instead?

  • Having an OS avoid these problemsIn its most basic form, OS provide a list of routines or services that are called by application programs to perform certain tasks through software interruptsDetails of such call involve three concepts:Program Counter (PC)Interrupt Vector Table (IVT)Context Switch

    Open FileClose FileGet System DataGet/Set File AttributeRead fileWrite fileAllocate MemoryWrite to ScreenWrite to Printer . .For (i=0;i

  • Program Counter (PC)The program counter(PC) is a register that store the address of the next instruction to be executed by the CPUint i=5, j=6, k=7;i=i+1;j=j+2;k=i+j;i=0;end10001002100310041005100610071008Memory address1001ProgramvariablesijkPC: 1000CPU:At the start, PC point to start of program

  • Program Counter (PC)The program counter(PC) is a register that store the address of the next instruction to be executed by the CPUint i=5, j=6, k=7;i=i+1;j=j+2;k=i+ji=0;end56710001002100310041005100610071008Memory address1001ProgramvariablesijkPC: 1001CPU:int i=5, j=6, k=7;Fetch instruction at 1000 and execute. Increment PC to 1001

  • Program Counter (PC)The program counter(PC) is a register that store the address of the next instruction to be executed by the CPUint i=5, j=6, k=7;i=i+1;j=j+2;k=i+ji=0;end66710001002100310041005100610071008Memory address1001ProgramvariablesijkPC: 1002CPU:i=i+1Fetch instruction at 1001 and execute. Increment PC to 1002

  • Program Counter (PC)The program counter(PC) is a register that store the address of the next instruction to be executed by the CPUint i=5, j=6, k=7;i=i+1;j=j+2;k=i+ji=0;end68710001002100310041005100610071008Memory address1001ProgramvariablesijkPC: 1003CPU:j=j+2Fetch instruction at 1002 and execute. Increment PC to 1003

  • Program Counter (PC)The program counter(PC) is a register that store the address of the next instruction to be executed by the CPUint i=5, j=6, k=7;i=i+1;j=j+2;k=i+ji=0;end081410001002100310041005100610071008Memory address1001ProgramvariablesijkPC: 1005CPU:k=i+j;Fetch instruction at 1004 and execute. Increment PC to 1005 and end program

  • Interrupt Vector Table(IVT)During interrupt, the program counter(PC) must be set to point to the address of the OS service in order to execute the routine there. How do the program know where is the address?IVT: store the location of each interrupt service

    Open FileClose FileGet System DataGet/Set File AttributeRead fileWrite fileAllocate MemoryWrite to ScreenWrite to Printer . .For (i=0;i

  • Let see how a software interrupt happen now! (User Mode)

    Open FileClose FileGet System DataGet/Set File AttributeRead fileWrite fileAllocate MemoryWrite to ScreenWrite to PrinterINT 01, numbers0001-0050021-00280029-00350036-00420043-00550056-00680069-00800036-0042Memory address0006-00201002OS ServicesApplicationProgramPC: 1002CPU:Interrupt Vector TableLet say the application program is running and the PC is pointing at location 1002i=i+1;1003

    Interrupt No.ServiceAddress01Open File000102Close File000603Get SD002104Get/Set File Attribute002905Read file003606Write file0043

  • Let see how a software interrupt happen now! (User Mode)

    Open FileClose FileGet System DataGet/Set File AttributeRead fileWrite fileAllocate MemoryWrite to ScreenWrite to PrinterINT 01, numbers0001-0050021-00280029-00350036-00420043-00550056-00680069-00800036-0042Memory address0006-00201002OS ServicesApplicationProgramPC: 1003CPU:INT 01, numbersInterrupt Vector TableIt load in the instruction and see that it is an interrupt command(INT) asking for interrupt service 01i=i+1;1003

    Interrupt No.ServiceAddress01Open File000102Close File000603Get SD002104Get/Set File Attribute002905Read file003606Write file0043

  • Let see how a software interrupt happen now! (User Mode)

    Open FileClose FileGet System DataGet/Set File AttributeRead fileWrite fileAllocate MemoryWrite to ScreenWrite to PrinterINT 01, numbers0001-0050021-00280029-00350036-00420043-00550056-00680069-00800036-0042Memory address0006-00201002OS ServicesApplicationProgramPC: 1003CPU:INT 01, numbersInterrupt Vector TableIt look up the IVT and see that the service Open File is locate at address 0001i=i+1;1003

    Interrupt No.ServiceAddress01Open File000102Close File000603Get SD002104Get/Set File Attribute002905Read file003606Write file0043

  • Let see how a software interrupt happen now! (Kernel Mode)

    Open FileClose FileGet System DataGet/Set File AttributeRead fileWrite fileAllocate MemoryWrite to ScreenWrite to PrinterINT 01, numbers0001-0050021-00280029-00350036-00420043-00550056-00680069-00800036-0042Memory address0006-00201002OS ServicesApplicationProgramPC: 0001CPU:Interrupt Vector TableIt then set PC to 0001 and start fetching instructions from the Open File service therei=i+1;1003

    Interrupt No.ServiceAddress01Open File000102Close File000603Get SD002104Get/Set File Attribute002905Read file003606Write file0043

  • Let see how a software interrupt happen now! (Kernel Mode)

    Open FileClose FileGet System DataGet/Set File AttributeRead fileWrite fileAllocate MemoryWrite to ScreenWrite to PrinterINT 01, numbers0001-0050021-00280029-00350036-00420043-00550056-00680069-00800036-0042Memory address0006-00201002OS ServicesApplicationProgramPC: 0005CPU: End of Open FileInterrupt Vector TableAssuming we reach the end of Open File routine, then how do we know where we should continue next?i=i+1;1003

    Interrupt No.ServiceAddress01Open File000102Close File000603Get SD002104Get/Set File Attribute002905Read file003606Write file0043

  • Context(I): User Mode

    Open FileClose FileGet System DataGet/Set File AttributeRead fileWrite fileAllocate MemoryWrite to ScreenWrite to PrinterINT 01, numbers0001-0050021-00280029-00350036-00420043-00550056-00680069-00800036-0042Memory address0006-00201002OS ServicesApplicationProgramPC: 1003CPU:INT 01, numbersInterrupt Vector TableActually, when an interrupt happens, we need to save the context of the application program before we jump to the interrupt service routine!i=i+1;1003Context stack

    Interrupt No.ServiceAddress01Open File000102Close File000603Get SD002104Get/Set File Attribute002905Read file003606Write file0043

    Prog