NETW 3005 I/O Systems
Reading For this lecture, you should have read Chapter 13 (Sections 1-4, 7). NETW3005 (Operating Systems) Lecture 10 - I/O Systems2
This Lecture: I/O systems Hardware: ports, buses, controllers Application I/O interface Kernel I/O services. NETW3005 (Operating Systems) Lecture 10 - I/O Systems3
Central issues How are I/O commands actually implemented? How does the O/S manage the wide range of I/O devices that are available? –Disks, CD/DVD drives, tapes … –Modems, network cards … –Screens, keyboards, mice, joysticks … NETW3005 (Operating Systems) Lecture 10 - I/O Systems4
What is a Device Driver? Each device has its own set of specialized commands that only its driver knows. A device driver is a system program that converts the system calls of the operating system into device specific commands. printers, video adapters, network cards, sound cards A device that controls the transfer of data from a computer to a peripheral device and vice versa. Example Graphics controller SCSI ( Small Computer system Interface) Controller– IDE ( Integrated Drive Electronics) Controller-
I/O Hardware – some background An I/O device is linked to a machine via a port. The link is normally a set of wires called a bus. At the end of the link, there’s a device controller, (basically, a processor.) A signal on a bus is basically a temporal sequence of voltages for each wire. NETW3005 (Operating Systems) Lecture 10 - I/O Systems7
I/O Hardware NETW3005 (Operating Systems) Lecture 10 - I/O Systems8 I/O device device controller port CPU bus voltage time
The PCI bus NETW3005 (Operating Systems) Lecture 10 - I/O Systems9 monitor graphics controller CPU memory/ controller cache memory disc PCI bus SCSI bus IDE disc controller disc expansion bus interface parallel port keyboard serial port disc
Device controller registers How does the CPU give commands to a device via a bus? –A device controller has a number of registers for holding signals. –The CPU can effectively write to and read from these. –Some registers hold data; some hold control signals. NETW3005 (Operating Systems) Lecture 10 - I/O Systems10
Device controller registers How can the CPU specify which device it’s giving commands to? –Each different port in the system has an address range. –The CPU can issue I/O instructions to particular addresses. NETW3005 (Operating Systems) Lecture 10 - I/O Systems11
Device I/O Port Locations on PCs (partial)
Some example controller registers A controller might have the following registers: –data-out: contains a byte of data. –status: contains a busy bit (indicating the controller’s state). –command: contains a command-ready bit (indicating CPU has sent some data) and a write bit (indicating direction of data flow). NETW3005 (Operating Systems) Lecture 10 - I/O Systems13
Programmed I/O and handshaking For each byte to be transferred to device: 1.The CPU reads the busy bit until it is clear. 2.The CPU sets the write bit and writes a byte to the data-out register. 3.The CPU sets the command-ready bit NETW3005 (Operating Systems) Lecture 10 - I/O Systems14
Programmed I/O and handshaking 4.The controller reads the command-ready bit until it is set. Then it sets the busy bit. 5.The controller reads the write command from the command register and then copies the data-out register to the device. 6.The controller clears the command-ready bit and then the busy bit. NETW3005 (Operating Systems) Lecture 10 - I/O Systems15
Polling and its Problems The step where the CPU cycles waiting for the busy bit to be clear is known as polling or busy waiting. If the CPU is polling, then its wastes CPU time. An alternative to polling is interrupts. NETW3005 (Operating Systems) Lecture 10 - I/O Systems16
Interrupts The CPU hardware contains a wire called the interrupt request line. The CPU tests this line after each instruction it executes. If a signal is detected, the CPU saves its current state and transfers control to interrupt handler. Interrupt handler process the interrupt and transfers the control to the CPU. If devices uses interrupt to communicate, then CPU doesn't need to poll. NETW3005 (Operating Systems) Lecture 10 - I/O Systems17
Types of interrupt Errors (exceptions) –divide by zero, invalid memory access, … Device-generated interrupts –device has read a byte; modem buffer full. Software interrupts (traps) –system call, context switch Some of these are more urgent than others. NETW3005 (Operating Systems) Lecture 10 - I/O Systems18
Interrupt priority levels One way of dealing with differing degrees of urgency is to provide two interrupt request lines: –maskable interrupts – device-generated interrupts and traps. These can be disabled. –nonmaskable interrupts: for signalling error messages. Never disabled. NETW3005 (Operating Systems) Lecture 10 - I/O Systems19
Direct Memory Access Programmed I/O (sending data byte-by- byte to a device) is laborious. For large chunks of data, direct memory access (DMA) is used. A DMA controller is a kind of processor that transfers data between I/O devices and memory without involving main processor (CPU) NETW3005 (Operating Systems) Lecture 10 - I/O Systems20
Direct Memory Access NETW3005 (Operating Systems) Lecture 10 - I/O Systems21 DMA operation needs the 1.channel number (which I/O device requires the DMA), 2.a beginning address in memory for the transfer, 3.the number of bytes to transfer, and 4.the direction of transfer (I/O to memory, or vice-versa) The processor starts the DMA activity with an ordinary I/O write to a control register in the DMA controller, then continues its work as usual When DMA activity is finished, DMA controller interrupts the processor
What is a Device Driver? Each device has its own set of specialized commands that only its driver knows. A device driver is a system program that converts the system calls of the operating system into device specific commands. printers, video adapters, network cards, sound cards A device that controls the transfer of data from a computer to a peripheral device and vice versa. Example Graphics controller SCSI ( Small Computer system Interface) Controller– IDE ( Integrated Drive Electronics) Controller-
The kernel-device interface How does the O/S manage the huge range of possible I/O devices? Clearly, we don’t want to rewrite the kernel every time we add a new device. We therefore need to impose a standard interface protocol on devices. NETW3005 (Operating Systems) Lecture 10 - I/O Systems23
The kernel-device interface The method for doing this is to encapsulate device-specific information in special kernel modules called device drivers. These device drivers translate system calls into device-specific commands. NETW3005 (Operating Systems) Lecture 10 - I/O Systems24
Device drivers and controllers There’s a one-to-one mapping between device drivers and device controllers. –Device drivers are software. –Device controllers are hardware. NETW3005 (Operating Systems) Lecture 10 - I/O Systems25
Types of I/O device I/O devices vary widely, along several dimensions. –Character stream versus block. –Sequential versus random access. –Synchronous versus asynchronous. –Shareable versus dedicated. OSs work with a small set of device types which can execute a standard set of commands. NETW3005 (Operating Systems) Lecture 10 - I/O Systems26
Character devices character devices relate to devices through which the system transmits data one character at a time. (Get and Put) Example Key boards serial modem
Block devices Block devices correspond to devices through which the system moves data in the form of blocks. Example : addressable devices hard disks, CD-ROM drives, or memory-regions. The standard commands necessary for a block device are: –Read block, –Write block, –Seek block (for random-access block devices). NETW3005 (Operating Systems) Lecture 10 - I/O Systems28
Network devices, e.g. sockets The standard commands for a socket: –Create socket –Connect local socket to remote socket –Listen for remote applications to local socket –Send information to a socket –Receive information from a socket Select – monitors a set of sockets. NETW3005 (Operating Systems) Lecture 10 - I/O Systems29 A socket is one endpoint of a two-way communication link between two programs running on the network.
Ip address +port Of server Ip address +port Of client
Blocking and nonblocking I/O There’s an important distinction between blocking and nonblocking I/O system calls. –If a process issues a blocking I/O system call, it waits until the I/O is completed. –If a process issues a nonblocking I/O call, it waits for a fixed interval, and then returns. –If a process issues an asynchronous I/O call, the I/O operation occurs in full, but the process doesn’t wait for it. NETW3005 (Operating Systems) Lecture 10 - I/O Systems31
Differences? In nonblocking I/O, you know how long the I/O is going to take, but you don’t know if all the data will be transferred. In asynchronous I/O, you know all the data will be transferred but you don’t know how long it will take, so you don’t know where you’ll be in your program when it comes back. NETW3005 (Operating Systems) Lecture 10 - I/O Systems32
Examples? Blocking I/O –disk reads. (The standard situation.) Non-blocking I/O –reads from a mouse, joystick, real-time video. Asynchronous I/O –printing something. NETW3005 (Operating Systems) Lecture 10 - I/O Systems33
Kernel I/O services So far, we have demonstrated the use of device drivers for the kernel. But we haven’t really explained why there should be an I/O subsystem. Why not just interface straight to the device drivers? NETW3005 (Operating Systems) Lecture 10 - I/O Systems34
Kernel I/O services In fact, there are several services performed by the I/O subsystem before the device drivers are called. –I/O Scheduling, –I/O Buffering, –Spooling. NETW3005 (Operating Systems) Lecture 10 - I/O Systems35
I/O scheduling At any one time, there might be several requests for I/O waiting to be serviced. We could just do these in first-come- first-served order. Disadvantages with this scheme? –Let’s say the disk arm is currently near the beginning of a disk, and there are 5 I/O requests: 3 for places near the beginning and 2 for places near the end. NETW3005 (Operating Systems) Lecture 10 - I/O Systems36
I/O buffering A buffer is used to provide communication between two processes with different speeds. In the consumer/producer problem, assume that the consumer process is faster than the producer process. –Producer fills up the buffer, switches to a second buffer and signals the consumer; –Consumer reads full buffer, then waits. NETW3005 (Operating Systems) Lecture 10 - I/O Systems37
Spooling A spool is a buffer that holds output for a device that cannot accept interleaved data streams - e.g. a printer. Rather than sending data straight to the printer, the O/S stores the data in buffers. The printer can then process the output from one buffer at a time. NETW3005 (Operating Systems) Lecture 10 - I/O Systems38