1 / 68

Gentle Introduction to Operating System

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.

Télécharger la présentation

Gentle Introduction to Operating System

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Objectives • Operating System in SOC is a second year course(CS2106) which have prerequisites of either • CS1104/ CS2100 : Computer Organization • EE2007: Microprocessor Systems • Aim of this talk: • Provide some hardware background so that you can understand OS better • Explain “why” instead of just “how” and “what”? • Provide a framework that can merge different concepts together

  3. Approach • We will design an OS together, starting from a simple one and finally to a complex one: • Single user, single process • Single user, multiple processes • Multiple users, multiple processes • While designing, we will take into consideration various desiderata • Efficiency • Robustness to changes • Robustness to errors • Consistency • Isolation

  4. What is OS? • As the name implied, “operating system” is a software system that operate something? What is that “something”? • “something”=hardware • Why 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…

  5. In the beginning… • There 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 set • Example: hard disk • Have many tracks • Each track have many sectors • Data are stored in each sector as binary bits 1010010100….

  6. Eg. of Instruction Set for Hard Disk • Physically the disk is control by electronic circuit instruction sector no. memory loc. 1 0 0 1 0 1 1 1 0 1 1 0

  7. Storing Data as Files • When there was no OS(i.e. no fgets, fopen…), programmers have to organize the sectors into files in their programs • Example: 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 “letters” End marker filename “numbers”, 2, 5, 8, -1 “letters”, 3, 6, 1023, -1 . . . Sector 1 Sector 2 20, 4, 100, ………… Sector 3 File directory sector A, Z, B, H, ………. Sector 4 900, 1000, 50, ………… Sector 5 Sector 6 B, C, I, A, ………. Sector 7 54, 20, 10,…………. Sector 8 Sector 9 . . Sector 1023 O, R, Q, G ………… Sector 1024

  8. Storing Data as Files(II) • How to read the file “numbers”? End marker filename “numbers”, 2, 5, 8, -1 “letters”, 3, 6, 1023, -1 . . . Step 1: 10, Sector 1, Mem. Location 20 // Read Sector 1 // into memory location 20 Step 2: Search location 20 for filename=“numbers”; Step 3: sector_num = first sector of “numbers”; Step 3: While (sector_num<>-1) do Step 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 Sector 1 Sector 2 20, 4, 100, ………… Sector 3 A, Z, B, H, ………. Sector 4 900, 1000, 50, ………… Sector 5 Sector 6 B, C, I, A, ………. Sector 7 54, 20, 10,…………. Sector 8 Sector 9 . . Sector 1023 O, R, Q, G ………… Sector 1024

  9. 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?

  10. Having an OS avoid these problems • In its most basic form, OS provide a list of routines or services that are called by application programs to perform certain tasks through software interrupts • Details of such call involve three concepts: • Program Counter (PC) • Interrupt Vector Table (IVT) • Context Switch Memory address 0001-005 Open File 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file Allocate Memory 0056-0068 Write to Screen 0069-0080 0036-0042 Write to Printer . . For (i=0;i<10;i++) write file(“numbers”,…) . . . 1000-2000 Application Program

  11. Program Counter (PC) • The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU Memory address 1000 int i=5, j=6, k=7; 1001 i=i+1; 1002 j=j+2; Program 1003 k=i+j; i=0; 1004 1005 end CPU: i 1006 j PC: 1000 variables 1007 1008 k At the start, PC point to start of program

  12. Program Counter (PC) • The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU Memory address 1000 int i=5, j=6, k=7; 1001 i=i+1; 1002 j=j+2; Program 1003 k=i+j i=0; 1004 1005 end CPU:int i=5, j=6, k=7; i 5 1006 j PC: 1001 6 variables 1007 1008 7 k Fetch instruction at 1000 and execute. Increment PC to 1001

  13. Program Counter (PC) • The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU Memory address 1000 int i=5, j=6, k=7; 1001 i=i+1; 1002 j=j+2; Program 1003 k=i+j i=0; 1004 1005 end CPU:i=i+1 i 6 1006 j PC: 1002 6 variables 1007 1008 7 k Fetch instruction at 1001 and execute. Increment PC to 1002

  14. Program Counter (PC) • The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU Memory address 1000 int i=5, j=6, k=7; 1001 i=i+1; 1002 j=j+2; Program 1003 k=i+j i=0; 1004 1005 end CPU:j=j+2 i 6 1006 j PC: 1003 8 variables 1007 1008 7 k Fetch instruction at 1002 and execute. Increment PC to 1003

  15. Program Counter (PC) • The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU Memory address 1000 int i=5, j=6, k=7; 1001 i=i+1; 1002 j=j+2; Program 1003 k=i+j i=0; 1004 1005 end CPU:k=i+j; i 0 1006 j PC: 1005 8 variables 1007 1008 14 k Fetch instruction at 1004 and execute. Increment PC to 1005 and end program

  16. 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 Memory address 0001-005 Open File 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU: Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 0043 0036-0042 Write to Printer Interrupt Vector Table . . For (i=0;i<10;i++) write file(“numbers”,…) . . . 1000-2000 Application Program

  17. Let see how a software interrupt happen now! (User Mode) Memory address 0001-005 Open File 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU: Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 1002 0036-0042 Write to Printer Interrupt Vector Table Let say the application program is running and the PC is pointing at location 1002 1002 Application Program INT 01, “numbers” 1003 i=i+1;

  18. Let see how a software interrupt happen now! (User Mode) Memory address 0001-005 Open File 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU:INT 01, “numbers” Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 1003 0036-0042 Write to Printer Interrupt Vector Table It load in the instruction and see that it is an interrupt command(INT) asking for interrupt service 01 1002 Application Program INT 01, “numbers” 1003 i=i+1;

  19. Let see how a software interrupt happen now! (User Mode) Memory address 0001-005 Open File 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU:INT 01, “numbers” Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 1003 0036-0042 Write to Printer Interrupt Vector Table It look up the IVT and see that the service “Open File” is locate at address 0001 1002 Application Program INT 01, “numbers” 1003 i=i+1;

  20. Let see how a software interrupt happen now! (Kernel Mode) Memory address Open File 0001-005 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU: Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 0001 0036-0042 Write to Printer Interrupt Vector Table It then set PC to 0001 and start fetching instructions from the “Open File” service there 1002 Application Program INT 01, “numbers” 1003 i=i+1;

  21. Let see how a software interrupt happen now! (Kernel Mode) Memory address Open File 0001-005 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU: End of Open File Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 0005 0036-0042 Write to Printer Assuming we reach the end of “Open File” routine, then how do we know where we should continue next? Interrupt Vector Table 1002 Application Program INT 01, “numbers” 1003 i=i+1;

  22. Context(I): User Mode Memory address 0001-005 Open File 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU:INT 01, “numbers” Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 1003 0036-0042 Write to Printer Interrupt Vector Table Actually, when an interrupt happens, we need to save the context of the application program before we jump to the interrupt service routine! 1002 Application Program INT 01, “numbers” 1003 i=i+1; Context stack

  23. Context(II): Kernel Mode Memory address Open File 0001-005 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU: End of Open File Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 0005 0036-0042 Write to Printer Interrupt Vector Table So when read the end of “Open File”, we look at the context stack and jump back to the place where interrupt occurs! 1002 Application Program INT 01, “numbers” 1003 i=i+1; Context stack

  24. Context(III): return from interrupt(User mode again!) Memory address Open File 0001-005 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute OS Services Read file 0036-0042 0043-0055 Write file CPU: Allocate Memory 0056-0068 Write to Screen 0069-0080 PC: 1003 0036-0042 Write to Printer Interrupt Vector Table So when read the end of “Open File”, we look at the context stack and jump back to the place where interrupt occurs! 1002 Application Program INT 01, “numbers” 1003 i=i+1; Context stack

  25. Context(IV): Final Notes • Note that in a context stack, there can be many entries as if many different processes/programs are running at the same time and issues different interrupts at different point in time program address Context stack

  26. Making OS more efficient • Since the OS provide a set of routines/services that are called by all application programs, it make sense to ensure that these routines/services are efficient so that the whole computer system is efficient as a whole • We will next look at two concepts that make the OS more efficient in using the CPU: • Caching • Hardware interrupts

  27. Caching(Motivation) Read 1 number: 2 ms Sum 2 numbers: 0.5 ms Initialize hard disk: 5ms Transfer 1 number: 2 ms CPU Disk • We wish to read 100 numbers from the disk and sum them up. These are the time that is need for each operations: • Initialize hard disk: 5ms • Read 1 number: 2 ms • Transfer 1 number: 2 ms • Sum 2 numbers: 0.5ms • How long will it take to finish what we want to do?

  28. Caching(Motivation) Read 1 number: 2 ms Sum 2 numbers: 0.5 ms Initialize hard disk: 5ms Transfer 1 number: 2 ms CPU Disk • Assuming no cache/buffer • Initialize 100 times for 100 numbers: 5 x 100 ms = 500 ms • Read 100 numbers: 2 x 100ms = 200ms • Transfer 100 numbers: 2 x 100ms = 200ms • Sum 100 numbers: 100 x 0.5 = 50ms • Total = 950ms

  29. Caching(Motivation) Read 1 number: 2 ms Sum 2 numbers: 0.5 ms Initialize hard disk: 5ms Disk CPU Transfer 1 number: 2 ms 100, 29, 50, 200,…, 90 100, 29, 50, 200,…, 90 CPU Memory Cache Local buffer of 100 numbers • Assuming we DO have cache that can store all 100 numbers • Initialize 1 times for 100 numbers: 5 x 1 ms = 5 ms • Read 100 numbers: 2 x 100ms = 200ms • Transfer 100 numbers: 2 x 100ms = 200ms • Sum 100 numbers: 100 x 0.5 = 50ms • Total = 455ms

  30. Caching(Motivation) Read 1 number: 2 ms Sum 2 numbers: 0.5 ms Initialize hard disk: 5ms Disk CPU Transfer 1 number: 2 ms 100, 29, 50, 200,…, 90 100, 29, 50, 200,…, 90 CPU Memory Cache Local buffer of 100 numbers Because of this, data on hard disk are stored as a block called “sector”. One sector can contain one set of numbers. Only the first read of the sector will cause access to the disk. If the sector is stored in the memory cache, subsequent read will be from the cache.

  31. Caching(Motivation) Read 1 number: 2 ms Sum 2 numbers: 0.5 ms Initialize hard disk: 5ms Disk CPU Transfer 1 number: 2 ms 100, 29, 50, 200,…, 90 100, 29, 50, 200,…, 90 CPU Memory Cache Local buffer of 100 numbers Can we do better? If we are summing N numbers, does it always make sense to have a local buffer or sector of N numbers for the disk? How about first loading 50 numbers, let the CPU start to compute and then CURRENTLY load the other 50 numbers?

  32. Caching & Interrupt (Motivation) • Concurrent reading and calculating (180ms) idle (205ms) idle ( 25ms) Sum first 50 No. ( 25ms) Sum next 50 No. CPU Initialize command hardware disk interrupt Harkware disk interrupt Initialize command 2 Disk Initialize (5ms) Read 50 No. (100ms) Transfer 50 No. (100ms) Initialize 2 (5ms) Read 50 No. (100ms) Transfer 50 No. (100ms) idle (25ms) Total time = 205ms + 205ms + 25ms = 435ms Time is saved since summing the first 50 numbers is done concurrent with disk I/O What if we divide the numbers into 4 blocks of 25 numbers ? Optimal number of blocks? • Initialize hard disk: 5ms • Read 1 number: 2 ms • Transfer 1 number: 2 ms • Sum 2 numbers: 0.5ms

  33. Interrupt and Currency • The concurrent execution in previous example is possible because of hardware interrupt AND caching. • The CPU leave the task of reading data to the hard disk and perform other task(adding number). The disk inform the CPU using interrupt when it complete its reading and transferring of data • Notice that we have more from single program, single process to single program with 2 processes (multi-threaded) For (i=0; i<100;i++) fscanf(input,"%d",&number[i]); producer Total = 0; For (i=0; i<100;i++) number[i]=-1; Fork() For (i=0; i<100;i++) { while number[i]==-1; Total=Total+number[i]);} consumer

  34. Memory Hierarchy for Caching • In general, CPU only read data from the processor registers and CPU cache • Cache miss result in fetching from the next layer • Pipelining(流水线) • In general, caching and buffering serve to coordinate devices with different speed

  35. Who handle hardware interrup? Memory address • Same as in software interrupt! • Hardware interrupt sent signal to CPU which will then give control to one of the OS interrupt handling routine 0001-005 Open File 0006-0020 Close File 0021-0028 Get System Data 0029-0035 Get/Set File Attribute Read file 0036-0042 0043-0055 Write file Handle disk interrupt 0056-0068 Scheduler 0069-0080 0036-0042 Write to Printer Interrupt Vector Table

  36. Single User, Multiple Programs (180ms) idle (205ms) idle ( 25ms) Sum first 50 No. ( 25ms) Sum next 50 No. CPU Initialize command hardware disk interrupt Harkware disk interrupt Initialize command 2 Disk Initialize (5ms) Read 50 No. (100ms) Transfer 50 No. (100ms) Initialize 2 (5ms) Read 50 No. (100ms) Transfer 50 No. (100ms) idle (25ms) Notice that the CPU still have idle time in the ADD_100_NUMBER example earlier. What can we do with these idle time? Answer: Swap the context of other programs in and run (Just like in Windows)

  37. Clock interrupt • A hardware implemented clock issue a clock interrupt at regular interval. Eg. intel process give a clock interrupt to the OS every 15ms. This can be changed by the OS which can use this clock as an “alarm clock” • This “alarm clock” is used to wake up a Scheduler which will select one of the programs to be ran in the CPU

  38. Multitasking/Time Sharing Using Clock Memory address CPU initially running Process XXX at location 1003 Clock Interrupt Occur! 0001-005 Open File 0006-0020 Close File 0021-0028 Scheduler 0029-0035 Get/Set File Attribute . . . . . . CPU:Instruction from XXX PC: 1003 1000-2000 Process XXX 2001-2800 Program YYY 2801-3500 Process ZZZ Context stack

  39. Multitasking/Time Sharing Using Clock Memory address Context of Process XXX saved and Scheduler loaded 0001-005 Open File 0006-0020 Close File 0021-0028 Scheduler 0029-0035 Get/Set File Attribute . . . . . . CPU:Scheduler PC: 0021 1000-2000 Process XXX 2001-2800 Program YYY 2801-3500 Process ZZZ Context stack

  40. Multitasking/Time Sharing Using Clock Memory address Scheduler select Program YYY to be ran next 0001-005 Open File 0006-0020 Close File 0021-0028 Scheduler 0029-0035 Get/Set File Attribute . . . . . . CPU:Scheduler PC: 0021 1000-2000 Process XXX 2001-2800 Program YYY Context stack 2801-3500 Process ZZZ

  41. Multitasking/Time Sharing Using Clock Memory address Scheduler select Program YYY to be ran next 0001-005 Open File 0006-0020 Close File 0021-0028 Scheduler 0029-0035 Get/Set File Attribute . . . . . . CPU:Instruction from YYY PC: 2200 1000-2000 Process XXX 2001-2800 Program YYY 2801-3500 Process ZZZ Context stack

  42. Multitasking/Time Sharing Using Clock Memory address Then after a certain period, clock interrupt occur again 0001-005 Open File 0006-0020 Close File 0021-0028 Scheduler 0029-0035 Get/Set File Attribute . . . . . . CPU:Instruction from YYY PC: 2600 1000-2000 Process XXX 2001-2800 Program YYY 2801-3500 Process ZZZ Context stack

  43. Multitasking/Time Sharing Using Clock Memory address Context of Programm YYY saved Scheduler is loaded again 0001-005 Open File 0006-0020 Close File 0021-0028 Scheduler 0029-0035 Get/Set File Attribute . . . . . . CPU:Scheduler PC: 0021 1000-2000 Process XXX 2001-2800 Program YYY 2801-3500 Process ZZZ Context stack

  44. Multitasking/Time Sharing Using Clock Memory address Scheduler decide to load Process ZZZ next 0001-005 Open File 0006-0020 Close File 0021-0028 Scheduler 0029-0035 Get/Set File Attribute . . . . . . CPU:Instruction from ZZZ PC: 3010 1000-2000 Process XXX 2001-2800 Program YYY 2801-3500 Process ZZZ Context stack

  45. Multitasking/Time Sharing • Note that sometimes program are swapped out by the scheduler not because its time slice is used up but because it is waiting for some input/output (eg. reading disk) and is thus idle • This is made known to the OS because input/output are also handled by the OS through software interrupts!

  46. Isolation: Security and Protection • Since multiple programs are at different stage of their execution concurrently in the system, care must be take care that they are isolated from each other i.e. they don’t affect each other through writing/reading from each others memory or file. • Again, this can be control by the OS since all these are done through its interrupt services

  47. Data Consistency Yapeng Deposit $50 Bank Data Read Record (Yapeng, BC345, $100)

  48. Data Consistency Yapeng Deposit $50 Bank Data Read Record (Yapeng, BC345, $100) Write Record (Yapeng, BC345, $150) Very simple?

  49. Data Consistency Yapeng Deposit $30 Data Read Record (Yapeng, BC345, $150) Now both Yapeng and his wife Wang Fei deposit money concurrently Read Record (Yapeng, BC345, $150) Wang Fei Deposit $100

  50. Data Consistency Yapeng Deposit $30 Data Read Record (Yapeng, BC345, $150) Wang Fei update record first Write Record (Yapeng, BC345, $250) Read Record (Yapeng, BC345, $150) Wang Fei Deposit $100

More Related