OPERATING SYSTEMS CS136 Jialiang LU Jialiang.lu@sjtu.edu.cn Based on Lecture Notes of Tanenbaum, Modern Operating Systems 3 e, 1
Chapter 5 INPUT/OUTPUT 2
Overview o OS controls I/O devices => o Issue commands, o handles interrupts, o handles errors o Provide easy to use interface to devices o Hopefully device independent o First look at hardware, then software o Emphasize software o Software structured in layers o Look at disks 3
I/O Devices Some typical device network, and bus data rates. 4
Overview o Two types of I/O devices: block, character o Block: can read blocks independently of one another o Hard disks, CD-ROMs, USB sticks o 512-32,768 bytes o Character: accepts characters without regard to block structure o Printers, mice, network interfaces o Not everything fits, e.g. clocks don t fit o Division allows for OS to deal with devices in device independent manner o File system deals with blocks 5
Disk geometry A is track, B is sector, C is geometrical sector, D is cluster 6
Device Controllers o I/O unit has 2 components: mechanical, electronic (controller) o Controller is a chip with a connector which plugs into cables to device o Controller assembles block from bit stream, does error correction, puts into buffer in controller o Blocks are what are sent from a disk o Disk o Disk might have 10,000 sectors of 512 bytes per track o Serial bit stream comes off drive o Has preamble, 4096 bits/sector, error correcting code o Preamble has sector number, cylinder number, sector size. 7
I/O Hardware Incredible variety of I/O devices Common concepts Port Bus (daisy chain or shared direct access) Controller (host adapter) I/O instructions control devices Devices have addresses, used by Direct I/O instructions Memory-mapped I/O 8
Memory Mapped I/O o Controller has registers which OS can write and read o Write-gives command to device o Read-learn device status o Devices have data buffer which OS can read/write (e.g. video RAM, used to display pixels on a screen) o How does CPU communicate with registers and buffers? 9
Memory-Mapped I/O (a) Separate I/O ports and memory space. (b) Memory-mapped I/O. (c) Memory mapped data buffers and separate ports (Pentium) 10
How CPU addresses registers and buffers o Direct I/O instructions o Puts read on control line o Put address on address line o Puts I/O space or memory space on signal line to differentiate o Read from memory or I/O space o Memory mapped approach o Put address on address line and let memory and I/O devices compare address with the ranges they serve 11
Memory mapped advantages o Don t need special instructions to read/write control registers=> can write a device driver in C o Don t need special protection to keep users from doing I/O directly. Just don t put I/O memory in any user space 12
Memory mapped disadvantage o Can cache memory words, which means that old memory value could remain in cache o => have to be able to disable caching when it is worthwhile o I/O devices and memory have to respond to memory references o Works with single bus because both memory and I/O look at address on bus and decide who it is for o harder with multiple buses because I/O devices can t see their addresses go by any more 13
Memory-Mapped I/O (a) A single-bus architecture. (b) A dual-bus memory architecture. 14
Solutions o Solution is for CPU to try memory first. If it does not get a response then it tries I/O device o Other fixes-snooping device, filter addresses o Main point-have to complicate hardware to make this work 15
Pentium System Buses The structure of a large Pentium system
DMA (Direct Memory Access) o CPU COULD request data one byte at a time from I/O controller o Big waste of time, use DMA o DMA controller on mother-board; normally one controller for multiple devices o CPU reads/writes to registers in controller o Memory address register o Byte count register o Control registers-i/o port, direction of transfer, transfer units (byte/word), number of bytes to transfer in a burst 17
When DMA is not used o Controller reads a block into its memory o Computes checksum o Interrupts OS o Sends byte at a time to memory 18
How does DMA work? Operation of a DMA transfer. 19
DMA controller modes o Cycle stealing mode-transfer goes on word at a time, competing with CPU for bus cycles. Idea is that CPU loses the occasional cycle to the DMA controller o Burst mode-dma controller grabs bus and sends a block o Fly by mode-dma controller tells device controller to send word to it instead of memory. Can be used to transfer data between devices. 20
Questions o Why buffer data in controllers? o Can do check-sum o Bus may be busy-need to store data someplace o Is DMA really worth it? Not if o CPU is much faster then DMA controller and can do the job faster o don t have too much data to transfer 21
PC interrupt structure The connections between the devices and the interrupt controller use interrupt lines on the bus rather than dedicated wires. 22
Interrupt processing o Controller puts number on address line telling CPU which device wants attention and interrupts CPU o Table (interrupt vector) points to interrupt service routine o Number on address line acts as index into interrupt vector o Interrupt vector contains PC which points to start of service routine 23
Interrupt processing o Interrupt service routine acks interrupt o Saves information about interrupted program o Where to save information o User process stack, kernel stack are both possibilities o Both have problems 24
The precise (ideal) interrupt 1.PC (Program Counter) is saved in a known place. 2.All instructions before the one pointed to by the PC have fully executed. 3.No instruction beyond the one pointed to by the PC has been executed. 4.Execution state of the instruction pointed to by the PC is known. 25
Precise and Imprecise Interrupts (2) (a) A precise interrupt. (b) An imprecise interrupt. 26
How to process an imprecise interrupt o With great difficulty o Either Need complicated hardware logic to re-start after interrupt (Pentium) o Or have complicated processing in the OS 27
Reading 5.1 28
Principles of I/O SOFTWARE o Goals of the I/O Software o Programmed I/O (polling) o Interrupt-Driven I/O o I/O Using DMA 29
I/O Software-Goals o Device independence: don t have to specify the device when accessing the device o Uniform naming: name should not depend on device type o Error handling: do it as close to the device as possible (e.g. controller should be the first to fix error, followed by the driver) o Buffering: e.g. when a packet arrives o Shared devices (disks) and un-shared devices must be handled 30
Programmed I/O Steps in printing a string. 31
Programmed I/O Polling or busy waiting Send data one character at a time 32
Good vs Bad o The good: simple idea, OK if the CPU is not bothered too much o The bad: CPU is bothered too much 33
Interrupt Driven I/O o Idea: block process which requests I/O, schedule another process o Return to calling process when I/O is done o Printer generates interrupt when a character is printed o Keeps printing until the end of the string o Re-instantiate calling process 34
Interrupt-Driven I/O (a) Code executed at the time the print system call is made. (b) Interrupt service procedure for the printer. 35
Comparison Programmed I/O Interrupt-driven I/O 36
DMA o Use DMA controller to send characters to printer instead of using the CPU o CPU is only interrupted when the buffer is printed instead of when each character is printed o DMA is worth it if (1) DMA controller can drive the device as fast as the CPU could drive it (2) there is enough data to make it worthwhile 37
I/O Using DMA (a) Code executed when the print system call is made. (b) Interrupt service procedure. 38
I/O Software Layers Layers of the I/O software system. 39
Interrupt Handlers o The idea: driver starting the I/O blocks until interrupt happens when I/O finishes o Handler processes interrupt o Wakes up driver when processing is finished o Drivers are kernel processes with their very own o Stacks o PCs o states 40
Interrupt processing details 1.Save registers not already saved by interrupt hardware. 2.Set up a context for the interrupt service procedure. 3.Set up a stack for the interrupt service procedure. 4.Acknowledge the interrupt controller. If there is no centralized interrupt controller, re-enable interrupts. 5.Copy the registers from where they were saved to the process table. 41
Interrupt processing details 6 Run the interrupt service procedure. 7 Choose which process to run next. 8 Set up the MMU context for the process to run next. 9 Load the new process registers, including its PSW. 10Start running the new process. 42
Device Drivers Figure 5-12. Logical positioning of device drivers. In reality all communication between drivers and device controllers goes over the bus. 43
Device Drivers-Act 1 o Driver contains code specific to the device o Supplied by manufacturer o Installed in the kernel o User space might be better place o Why? Bad driver can mess up kernel o Need interface to OS o block and character interfaces o procedures which OS can call to invoke driver (e.g. read a block) 44
Device drivers Act 2 o Checks input parameters for validity o Abstract to concrete translation (block number to cylinder, head, track, sector) o Check device status. Might have to start it. o Puts commands in device controller s registers o Driver blocks itself until interrupt arrives o Might return data to caller o Does return status information o The end o Drivers should be re-entrant o OS adds devices when system (and therefore driver) is running 45
Device-Independent I/O Software 46
Why the OS needs a standard interface Driver functions differ for different drivers Kernel functions which each driver needs are different for different drivers Too much work to have new interface for each new device type 47
Interface:Driver functions o OS defines functions for each class of devices which it MUST supply, e.g. read, write, turn on, turn off.. o Driver has a table of pointers to functions o OS just needs table address to call the functions o OS maps symbolic device names onto the right driver 48
Interface:Names and protection o OS maps symbolic device names onto the right driver o Unix: /dev/disk0 maps to an i-node which contains the major and minor device numbers for disk0 o Major device number locates driver, minor device number passes parameters (e.g. disk) o Protection: In Unix and Windows devices appear as named objects => can use file protection system 49
Buffering (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. 50
Buffering Networking may involve many copies of a packet. 51
More Functions of Independent Software o Error reporting: programming errors (the user asks for the wrong thing), hardware problems (bad disk) are reported if they can t be solved by the driver o Allocates and releases devices which can only be used by one user at a time (CD-ROM players) o Queues requests or o Lets open simply fail o Device independent block size: OS does not have to know the details of the devices o E.g. combine sectors into blocks 52
User Space I/O Software o Library routines are involved with I/O: printf,scanf,write for example. These routines makes system calls o Spooling systems: keep track of device requests made by users. o Think printing. o User generates file, puts it in a spooling directory. o Daemon process monitors the directory, printing the user file o File transfers also use a spooling directory 53
Example Flow through layers o User wants to read a block, asks OS o Device independent software looks for block in cache o If not there, invokes device driver to request block from disk o Transfer finishes, interrupt is generated o User process is awakened and goes back to work 54
Clock Hardware Figure 5-32. A programmable clock. 55
Typical duties of a clock driver 1. Maintaining the time of day. Clock Software (1) 2. Preventing processes from running longer than they are allowed to. 3. Accounting for CPU usage. 4. Handling alarm system call made by user processes. 5. Providing watchdog timers for parts of the system itself. 6. Doing profiling, monitoring, statistics gathering. 56
Clock Software (2) Figure 5-33. Three ways to maintain the time of day. 57
Clock Software (3) Figure 5-34. Simulating multiple timers with a single clock. 58
Soft Timers Soft timers succeed according to rate at which kernel entries are made because of: 1.System calls. 2.TLB misses. 3.Page faults. 4.I/O interrupts. 5.The CPU going idle. 59
Keyboard Software Characters that are handled specially in canonical mode. 60
The X Window System (1) The ANSI escape sequences accepted by the terminal driver on output. ESC denotes the ASCII escape character (0x1B), 61 and n, m, and s are optional numeric parameters.
The X Window System (2) Clients and servers in the M.I.T. X Window System. 62
The X Window System (3) Types of messages between client and server: 1.Drawing commands from the program to the workstation. 2.Replies by the workstation to program queries. 3.Keyboard, mouse, and other event announcements. 4.Error messages. 63
Graphical User Interfaces (1) Figure 5-38. A skeleton of an X Window application program.... 64
... Graphical User Interfaces (2) Figure 5-38. A skeleton of an X Window application program. 65
Graphical User Interfaces (3) A sample window located at (200, 100) on an XGA display. 66
Graphical User Interfaces (4)... A skeleton of a Windows main program. 67
... Graphical User Interfaces (5) A skeleton of a Windows main program. 68
Bitmaps (1) Figure 5-41. An example rectangle drawn using Rectangle. Each box represents one pixel. 69
Bitmaps (2) Figure 5-42. Copying bitmaps using BitBlt. (a) Before. (b) After. 70
Thin Clients The THINC protocol display commands. 71
Power Management Hardware Issues Figure 5-45. Power consumption of various parts of a notebook computer. 72
Power Management The Display The use of zones for backlighting the display. (a) When window 2 is selected it is not moved. (b) When window 1 is selected, it moves to reduce the number of zones illuminated. 73
Power Management The CPU (a) Running at full clock speed. (b) Cutting voltage by two cuts clock speed by two and power consumption by four. 74
Reading Chapter 5 75