Chap. 3. Input/Output 17 janvier 07 3.1 Principles of I/O hardware 3.2 Principles of I/O software 3.3 Deadlocks [3.4 Overview of IO in Minix 3] [3.5 Block Devices in Minix 3] [3.6 RAM Disks] 3.7 Disks 3.8 Terminals Various Andrew S. Tanenbaum Modern Operating Systems, 2nd edition Prentice Hall 2001 1
3.1 Principles of I/O Hardware Some network Some device Other Some typical device, network, and bus data rates 2
Device Controllers I/O devices have components: mechanical component electronic component The electronic component is the device controller may be able to handle multiple devices Controller's tasks convert serial bit stream to block of bytes perform error correction as necessary make available to main memory 3
Memory-Mapped I/O (1) Separate I/O and memory space Memory-mapped I/O Hybrid 4
Memory-Mapped I/O (2) (a) A single-bus architecture (b) A dual-bus memory architecture 5
Interrupts Revisited How interrupts happens Remark: Connections between devices and interrupt controller actually use interrupt lines on the bus rather than dedicated wires 6
Interrupts Revisited Fig. 2.33 Interrupt processing hardware on a 32-bit Intel PC Andrew S. Tanenbaum, Albert S. Woodhull: Operating Systems: Design and Implementation, 2nd edition, Prentice Hall 1997 7
Direct Memory Access (DMA) Operation of a DMA transfer 8
3.2 Principles of I/O Software Goals of I/O Software (1) Device independence programs can access any I/O device without specifying device in advance (floppy, hard drive, or CD-ROM) Uniform naming name of a file or device a string or an integer not depending on which machine Error handling handle as close to the hardware as possible 9
Goals of I/O Software (2) Synchronous vs. asynchronous transfers blocked transfers vs. interrupt-driven Buffering data coming off a device cannot be stored in final destination Sharable vs. dedicated devices disks are sharable tape drives would not be 10
Programmed I/O (1) Steps in printing a string 11
Programmed I/O (2) Writing a string to the printer using programmed I/O 12
Interrupt-Driven I/O Writing a string to the printer using interrupt-driven I/O Code executed when print system call is made Interrupt service procedure 13
I/O Using DMA Printing a string using DMA code executed when the print system call is made interrupt service procedure 14
I/O Software Layers Layers of the I/O Software System 15
3.2.2 Interrupt Handlers (1) Interrupt handlers are best hidden have driver starting an I/O operation block until interrupt notifies of completion Interrupt procedure does its task then unblocks driver that started it Steps must be performed in software after interrupt completed 1. Save regs not already saved by interrupt hardware 2. Set up context for interrupt service procedure 16
Interrupt Handlers (2) Steps must be performed in software after interrupt completed 1. Save regs not already saved by interrupt hardware 2. Set up context for interrupt service procedure Set up stack for interrupt service procedure 3. Ack interrupt controller, reenable interrupts 4. Copy registers from where saved 5. Run service procedure 6. Set up MMU context for process to run next 7. Load new process' registers 8. Start running the new process 17
3.2.3 Device Drivers Logical position of device drivers is shown here Communications between drivers and device controllers goes over the bus 18
3.2.4 Device-Independent I/O Software (1) Uniform interfacing for device drivers Buffering Error reporting Allocating and releasing dedicate devices Providing a deice-independent block size Functions of the device-independent I/O software 19
3.2.4 Device-Independent I/O Software (2) (a) Without a standard driver interface (b) With a standard driver interface 20
3.2.4 Device-Independent I/O Software (3) (a) Unbuffered input (b) Buffering in user space (c) Buffering in the kernel followed by copying to user space (d) Double buffering in the kernel 21
3.2.4 Device-Independent I/O Software (4) Networking may involve many copies 22
3.2.5 User-Space I/O Software Layers of the I/O system and the main functions of each layer 23
Section. 3.3 Deadlocks 3.3.1 Resource 3.3.2 Introduction to deadlocks 3.3.3 The ostrich algorithm 3.3.4 Detection and Recovery 3.3.5 Deadlock Prevention 3.3.6 Deadlock Avoidance 3.3.7 Other Issues Andrew S. Tanenbaum Modern Operating Systems, 2nd edition Prentice Hall 2001 24
3.3.1 Resources Examples of computer resources Devices: printer, tape drive, hard disk, CD, Data: table, word (in read or write mode), Preemptable / Non-preemptable resources Preemptable: Can be taken away from a process with no ill effects Non-preemptable: Will cause the process to fail if taken away Sequence of events required to use a resource 1. Request the resource 2. Use the resource 3. Release the resource Requesting process (if access is denied) May be blocked May fail with error code 25
3.3.2 Introduction to Deadlocks Formal definition : A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause None of the processes can run release resources be awakened 26
Four Conditions for Deadlock Mutual exclusion condition Each resource is assigned to one process or is available Hold and wait condition Processes holding resources can request additional resources No preemption condition Previously granted resources cannot forcibly taken away Circular wait condition Must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain 27
Deadlock Modeling (1) Four conditions modeled with directed graphs (Holt, 1972) (a) resource R is assigned to process A (b) process B is requesting/waiting for resource S process C and D are in deadlock over resources T and U 28
Deadlock Modeling (2) How deadlock occurs 29
Deadlock Modeling (3) (o) (p) (q) How deadlock can be avoided 30
Strategies for dealing with Deadlocks 1. Just ignore the problem altogether Ostrich algorithm 2. Detection and recovery Directed graphs or matrix-based algorithms 3. Dynamic avoidance Careful resource allocation Banker s algorithm 4. Prevention Negating one of the four necessary conditions 31
3.3.3 The Ostrich Algorithm Pretend there is no problem Reasonable if deadlocks occur very rarely cost of prevention is high Unix, Linux, Mac OS X, Windows, take this approach! It is a trade off between convenience correctness 32
3.3.4 Detection and Recovery Just monitoring the requests and release of resources On release: update of the resource graph and detection of cycles On cycle detection: processes are killed until cycle is broken A cruder method Periodically checked if one or more processes are blocked for e.g. 1 hour, and kill them. Special care Simply killing processes and restarting the others: may be applicable in batch systems But need for restoring files and undo some side effects already produced (not trivial) 33
3.3.5 Deadlock Prevention Summary of approaches to deadlock prevention 34
3.3.6 Deadlock Avoidance E = Existing resources, P = Possessed resources, A = available resources Example of Banker's Algorithm with Multiple Resources 35
3.3.7 Other Issues Two Phase Locking Phase 1 : process tries to lock all records it needs, one at a time (no real work done in phase one) Phase 2 : If phase 1 succeeds, it starts second phase, performing updates and releasing locks at the end Non resource Deadlocks Possible for two processes to deadlock each is waiting for the other to do some task Can happen with semaphores e.g. each process required to do a down() on two semaphores (deadlock can happen if done in wrong order) Starvation e.g. shortest job first: May cause long job to be postponed indefinitely, even though not blocked 36
3.6 Disks Disk Hardware (1) Disk parameters for the original IBM PC floppy disk and a Western Digital WD 18300 hard disk 37
Disk Hardware (2) Physical geometry of a disk with two zones A possible virtual geometry for this disk 38
Disk Hardware (3) Raid levels 0 through 2 Backup and parity drives are shaded 39
Disk Hardware (4) Raid levels 3 through 5 Backup and parity drives are shaded 40
Disk Hardware (5) Recording structure of a CD or CD-ROM 41
Disk Hardware (6) Logical data layout on a CD-ROM 42
Disk Hardware (7) Cross section of a CD-R disk and laser not to scale Silver CD-ROM has similar structure without dye layer with pitted aluminum layer instead of gold 43
Disk Hardware (8) A double sided, dual layer DVD disk 44
Disk Formatting (1) A disk sector 45
Disk Formatting (2) An illustration of cylinder skew 46
Disk Formatting (3) No interleaving Single interleaving Double interleaving 47
Disk Arm Scheduling Algorithms (1) Time required to read or write a disk block determined by 3 factors Seek time Rotational delay Actual transfer time Seek time dominates Error checking is done by controllers 48
Disk Arm Scheduling Algorithms (2) Initial position Pending requests Shortest Seek First (SSF) disk scheduling algorithm 49
Disk Arm Scheduling Algorithms (3) The elevator algorithm for scheduling disk requests 50
Error Handling A disk track with a bad sector Substituting a spare for the bad sector Shifting all the sectors to bypass the bad one 51
Stable Storage Analysis of the influence of crashes on stable writes 52
3.8 Character Oriented Terminals RS-232 Terminal Hardware An RS-232 terminal communicates with computer 1 bit at a time Called a serial line bits go out in series, 1 bit at a time Windows uses COM1 and COM2 ports, first to serial lines Computer and terminal are completely independent 53
Input Software (1) Central buffer pool Dedicated buffer for each terminal 54
Input Software (2) Characters handled specially in canonical mode 55
Output Software The ANSI escape sequences accepted by terminal driver on output ESC is ASCII character (0x1B) n,m, and s are optional numeric parameters 56
Display Hardware (1) Parallel port Memory-mapped displays driver writes directly into display's video RAM 57
Display Hardware (2) A video RAM image simple monochrome display character mode Corresponding screen the xs are attribute bytes 58
Input Software Keyboard driver delivers a number driver converts to characters uses a ASCII table Exceptions, adaptations needed for other languages many OS provide for loadable keymaps or code pages 59
Output Software for Windows (1) Sample window located at (200,100) on XGA display 60
Output Software for Windows (2) Skeleton of a Windows main program (part 1) 61
Output Software for Windows (3) Skeleton of a Windows main program (part 2) 62
Output Software for Windows (4) An example rectangle drawn using Rectangle 63
Output Software for Windows (5) Copying bitmaps using BitBlt. before after 64
Output Software for Windows (6) Examples of character outlines at different point sizes 65
Network Terminals X Windows (1) Clients and servers in the MIT X Windows System 66
X Windows (2) Skeleton of an X Windows application program 67
The SLIM Network Terminal (1) The architecture of the SLIM terminal system 68
The SLIM Network Terminal (2) Messages used in the SLIM protocol from the server to the terminals 69
Clocks Clock Hardware A programmable clock 70
Clock Software (1) Three ways to maintain the time of day 71
Clock Software (2) Simulating multiple timers with a single clock 72
Soft Timers A second clock available for timer interrupts specified by applications no problems if interrupt frequency is low Soft timers avoid interrupts kernel checks for soft timer expiration before it exits to user mode how well this works depends on rate of kernel entries 73
Power Management (1) Power consumption of various parts of a laptop computer 74
Power management (2) The use of zones for backlighting the display 75
Power Management (3) Running at full clock speed Cutting voltage by two cuts clock speed by two, cuts power by four 76
Power Management (4) Telling the programs to use less energy may mean poorer user experience Examples change from color output to black and white speech recognition reduces vocabulary less resolution or detail in an image 77