Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Slide 5-1 Device Management Copyright © 2004 Pearson Education, Inc. 5 Operating Systems: A Modern Perspective, Chapter 5 Announcements • Homework Set #1 due Thursday 11 am, see moodle • copyright issues with lectures • Program Assignment #1 coming soon • Read chapters 4 and 5 – skip sections 4.7, 4.8 – skip Sections 5.4, 5.5 for now Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Slide 5-2 Slide 5-3 Determining When I/O is Complete CPU CPU Interrupt Pending Device Device Device Device Device Device • CPU incorporates an “interrupt pending” flag • When device.busy → FALSE, interrupt pending flag is set • Hardware “tells” OS that the interrupt occurred • Interrupt handler part of the OS makes process ready to run Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Slide 5-4 Control Unit with Interrupt (Hardware) PC = <machine start address>; IR = memory[PC]; haltFlag = CLEAR; while(haltFlag not SET) { execute(IR); PC = PC + sizeof(INSTRUCT); IR = memory[PC]; if(InterruptRequest) { memory[0] = PC; PC = memory[1] }; could be a trap instr. memory[1] contains the address of the interrupt handler Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Examples of Exceptions in Pentium Systems Slide 5-5 Class Cause Async/ Sync Return behavior Trap Intentional exception Sync Fault Potentially recoverable error nonrecoverable error signal from I/O device Sync always returns to next instruction might return to current instruction never returns Abort Interrupt Copyright © 2004 Pearson Education, Inc. Sync Async Operating Systems: A Modern Perspective, Chapter 5 always returns to next instruction The Trap Instruction Operation Mode Slide 5-6 a trap is a “software” interrupt S Branch Table 1 2 trap 3 Trusted Code User Supervisorhardware interrupts behave similarly, an interrupt gives an offset into interrupt vector table Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Examples of Exceptions in Pentium Systems Slide 5-7 Exception Table, also called Branch Table or Jump Table or Interrupt Vector Exception Number Description Exception Class 0 Divide error fault 13 General protection fault fault 14 Page fault fault 18 machine check abort 32-127 OS-defined Interrupt or trap OS assigns 128 System call Trap 129-255 OS-defined Interrupt or trap Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 nonmaskable maskable interrupts Maskable Interrupts Slide 5-8 • Maskable interrupts can be turned off by CPU before execution of critical instruction sequences – are used by device controllers to talk with CPU • Nonmaskable interrupts/exceptions is reserved for events such as unrecoverable memory errors and cannot be turned off by the CPU • Can have multiple interrupt priority levels – high-priority interrupts can preempt execution of a lowpriority interrupt Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Disabling Maskable Interrupts Slide 5-9 saveProcessorState() { for(i=0; i<NumberOfRegisters; i++) memory[K+i] = R[i]; for(i=0; i<NumberOfStatusRegisters; i++) memory[K+NumberOfRegisters+i] = StatusRegister[i]; } PC = <machine start address>; IR = memory[PC]; haltFlag = CLEAR; while(haltFlag not SET) { execute(IR); PC = PC + sizeof(INSTRUCT); IR = memory[PC]; if(InterruptRequest && InterruptEnabled) { disableInterupts(); memory[0] = PC; have to reenable PC = memory[1] }; interrupts after done Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Classes of System Calls Invoked by trap system call interface Information CommManagement unications Process File Device control Management Management • • • • • • • end, abort load, execute create, terminate get attributes, set wait for time wait event, signal event allocate memory, free • • • • • • • • request device, release read, write, reposition get attributes, set logically attach or detach devices Note Similarity create, delete open, close read, write, reposition get attributes, set Copyright © 2004 Pearson Education, Inc. • • • Slide 5-10 • • • • create connection, delete send messages, receive transfer status info attach remote devices, detach get time/date, set get system data, set get process, file, or device attributes, set Operating Systems: A Modern Perspective, Chapter 5 The Device Driver Interface … write(…); … Device DeviceInterface Interface Terminal Terminal Driver Driver Printer Printer Driver Driver Disk Disk Driver Driver Terminal Terminal Controller Controller Printer Printer Controller Controller Disk Disk Controller Controller Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Slide 5-11 Slide 5-12 Device Management Organization System Interface Application Application Process Process e.g. write() File File Manager Manager Hardware Interface Device-Independent Device-Independent Device-Dependent Device-Dependent Device Manager or I/O Subsystem drivers Command Command Status Status Data Data Device Controller Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Device System Call Interface Slide 5-13 • Functions available to application programs • Abstract all devices (and files) to a few interfaces • Make interfaces as similar as possible – Block vs character – Sequential vs direct/random access – Blocking versus Non-Blocking I/O • blocking system call: process put on wait queue until I/O completes • non-blocking system call: returns immediately with partial number of bytes transferred, e.g. keyboard, mouse, network sockets – Synchronous versus asynchronous • asynchronous returns immediately, but at some later time, the full number of bytes requested is transferred • Device driver implements functions (one entry point per API function) Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Example: BSD UNIX Driver open close ioctl read write strategy select stop Copyright © 2004 Pearson Education, Inc. Prepare dev for operation No longer using the device Character dev specific info Character dev input op Character dev output op Block dev input/output ops Character dev check for data Discontinue a stream output op Operating Systems: A Modern Perspective, Chapter 5 Slide 5-14 Device Independent Function Call Trap Table funci(…) dev_func_i(devID, …) { // Processing common to all devices … switch(devID) { case dev0: dev0_func_i(…); break; case dev1: dev1_func_i(…); break; … case devM: devM_func_i(…); break; }; // Processing common to all devices … } Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Slide 5-15 Overlapping the Operation of a Device and the CPU . . . read(dev_I, “%d”, x); y = f(x) . . . . . . startRead(dev_I, “%d”, x); . . . While(stillReading()) ; y = f(x) . . . Data on device Variable x Register Memory CPU Device dev_I Copyright © 2004 Pearson Education, Inc. Slide 5-16 Operating Systems: A Modern Perspective, Chapter 5 Overlapping CPU-Controller Operations in a Process Slide 5-17 could be non-blocking or asynchronous system call App I/O Ctlr t1 Copyright © 2004 Pearson Education, Inc. t2 t3 t4 t5 t6 t7 Operating Systems: A Modern Perspective, Chapter 5 t8 t9 Overlapping Processing and I/O App1 makes a blocking or synchronous system call App 1 App 2 I/O Ctlr t1 Copyright © 2004 Pearson Education, Inc. t2 t3 Operating Systems: A Modern Perspective, Chapter 5 t4 Slide 5-18 Device Manager I/O Strategies Slide 5-19 • Underneath the blocking/non-blocking synchronous/asynchronous system call API, OS can implement several strategies for I/O with devices – direct I/O with polling – direct I/O with interrupts – DMA with interrupts Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Polling I/O Read Operation read(device, …); Data 1 System Interface read driver 5 write driver 2 3 4 Hardware Interface Command Command Status Status Data Data Device Controller Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Slide 5-20 Interrupt-driven I/O Operation read(device, …); 1 Slide 5-21 9 8b Data System Interface Device Status Table 2 7 4 read driver Device Device Handler Handler write driver 6 3 Interrupt Interrupt Handler Handler 5 Hardware Interface Command Command Status Status Data Data Device Controller Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 8a Driver-Kernel Interface Slide 5-22 • Drivers are distinct from main part of kernel • Kernel makes calls on specific functions, drivers implement them • Drivers use kernel functions for: – – – – Device allocation Resource (e.g., memory) allocation Scheduling etc. (varies from OS to OS) Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 DMA with Interrupts Example 1. device driver told to transfer bytes from disk to memory 2. dev driver informs disk controller 5. DMA sends each DMA/bus/interrupt Controller byte to memory 6. DMA interrupts CPU when done Copyright © 2004 Pearson Education, Inc. Slide 5-23 CPU CPU/Memory Bus Memory I/O Bus 3. disk controller initiates DMA Disk Controller 4. disk controller sends each byte to DMA controller Operating Systems: A Modern Perspective, Chapter 5 Handling Interrupts Device driver J Device status table int read(…) { // Prepare for I/O save_state(J); out dev# // Done (no return) } Device interrupt handler J void dev_handler(…) { get_state(J); //Cleanup after op signal(dev[j]); return_from_sys_call(); } J Interrupt InterruptHandler Handler Device DeviceController Controller Copyright © 2004 Pearson Education, Inc. Slide 5-24 Operating Systems: A Modern Perspective, Chapter 5 Handling Interrupts(2) Device driver J Device interrupt handler J int read(…) { … out dev# // Return after interrupt wait(dev[J}); return_from_sys_call(); } void dev_handler(…) { //Cleanup after op signal(dev[j]); } Interrupt InterruptHandler Handler Device DeviceController Controller Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 5 Slide 5-25