OSes: 12. IOSys Operating Systems Operating Systems Objectives Objectives – discuss how an OS manages and discuss how an OS manages and controls I/O operations and I/O controls I/O operations and I/O devices devices Certificate Program in Software D evelopment CSE-TC and CSIM, AIT September--November, 2003 12. I/O Systems (Ch. 12, S&G)
66
Embed
OSes: 12. IOSys 1 Operating Systems v Objectives –discuss how an OS manages and controls I/O operations and I/O devices Certificate Program in Software.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
OSes: 12. IOSys 1
Operating SystemsOperating Systems
ObjectivesObjectives– discuss how an OS manages and controls discuss how an OS manages and controls
I/O operations and I/O devicesI/O operations and I/O devices
Certificate Program in Software DevelopmentCSE-TC and CSIM, AITSeptember--November, 2003
5.5. From I/O Requests to HardwareFrom I/O Requests to Hardware
6.6. PerformancePerformance
OSes: 12. IOSys 3
1. Issues1. Issues
I/O is the main job in most computersI/O is the main job in most computers– processing is secondaryprocessing is secondary
An OS must deal with a wide variety of I/O An OS must deal with a wide variety of I/O devices with different properties:devices with different properties:– mouse, hard disk, CD-ROM,mouse, hard disk, CD-ROM,
joystick, keyboard, etc.joystick, keyboard, etc.
continued
OSes: 12. IOSys 4
Increase the standardization of the I/O Increase the standardization of the I/O software and hardware interfaces.software and hardware interfaces.
Support many types of devices.Support many types of devices.
Performance.Performance.
One solutionOne solution::– device driver modules with standard interfacesdevice driver modules with standard interfaces
Principal Design AimsPrincipal Design Aims
OSes: 12. IOSys 5
2. I/O Hardware2. I/O Hardware Some terminology:Some terminology:
– portport: a device’s connection point to the main : a device’s connection point to the main processor (computer)processor (computer)
– busbus: a connection line allowing several devices : a connection line allowing several devices to access the processorto access the processor
– controllercontroller: a chip or circuit board in the device : a chip or circuit board in the device that manages interaction between the device that manages interaction between the device and processorand processor
OSes: 12. IOSys 6
2.1. Typical PC Bus Structure2.1. Typical PC Bus StructureFig. 12.1, p.399
diskdisk
disk
disk
monitor
graphicscontroller SCSI controller
processor
bridge/memorycontroller
cache
memory
disk disk
IDE disk controller
disk disk
expansion bus interface keyboard
parallel port serial port
PCI Bus
Expansion Bus
SC
SI
bus
OSes: 12. IOSys 7
2.2. Controller Features2.2. Controller Features Runs microcode for specific tasksRuns microcode for specific tasks
– e.g. a hard disk controller may do bad-sector e.g. a hard disk controller may do bad-sector avoidance, bufferingavoidance, buffering
Use registers for holding data, control signalsUse registers for holding data, control signals– the processor communicates with the controller by the processor communicates with the controller by
reading/writing to these registersreading/writing to these registers
Registers may be visible to a processor as I/O Registers may be visible to a processor as I/O port addresses.port addresses.
OSes: 12. IOSys 8
2.3. Some PC I/O Port Addresses2.3. Some PC I/O Port Addresses
I/O Address Range (Hex)I/O Address Range (Hex) DeviceDevice000-00F000-00F DMA controllerDMA controller040-043040-043 timertimer320-32F320-32F hard disk controllerhard disk controller378-37F378-37F parallel portparallel port3D0-3DF3D0-3DF graphics controllergraphics controller3F0-3F73F0-3F7 floppy drive controllerfloppy drive controller : : ::
Fig. 12.2, p.400
OSes: 12. IOSys 9
2.4. Registers <--> I/O Port2.4. Registers <--> I/O Port
Typically, an I/O port address is made up Typically, an I/O port address is made up of 4 registers.of 4 registers.
One register is usually 1- 4 bytesOne register is usually 1- 4 bytes
Different bits/bytes of a register are used Different bits/bytes of a register are used for manipulating different parts of the for manipulating different parts of the device.device.
OSes: 12. IOSys 10
statusstatus: read by a processor (host) to see if a : read by a processor (host) to see if a device is ready to execute a command, or has device is ready to execute a command, or has finished a command, etc.finished a command, etc.
control/commandcontrol/command: written by a processor (host) : written by a processor (host) to tell the device to start/change its settings, etc.to tell the device to start/change its settings, etc.
data-indata-in: read by a procesor (host): read by a procesor (host)
data-outdata-out: written by a processor (host): written by a processor (host)
Register NamesRegister Names
OSes: 12. IOSys 11
2.5. Handshaking2.5. Handshaking
HandshakingHandshaking is one possible protocol is one possible protocol between a host (processor) and a controller.between a host (processor) and a controller.
ExampleExample: the host writes a byte to the : the host writes a byte to the device:device:– 1. The host repeatedly reads the busy bit of the 1. The host repeatedly reads the busy bit of the
device’s device’s statusstatus register until is it clear/false register until is it clear/false busy-waiting (polling)busy-waiting (polling)
continued
OSes: 12. IOSys 12
– 2. The host sets the write bit of the device’s 2. The host sets the write bit of the device’s commandcommand register and writes a byte into the register and writes a byte into the data-outdata-out register. register.
– 3. The host sets the ready bit of the 3. The host sets the ready bit of the commandcommand register.register.
– 4. When the device’s controller notices the 4. When the device’s controller notices the ready bit, it sets the busy bit of its ready bit, it sets the busy bit of its statusstatus register. register.
continued
OSes: 12. IOSys 13
– 5. The controller sees the write bit of its 5. The controller sees the write bit of its commandcommand register. It then reads the byte from register. It then reads the byte from the the data-outdata-out register, and does the I/O. register, and does the I/O.
– 6. The controller clears the 6. The controller clears the commandcommand ready ready bit, and clears the bit, and clears the statusstatus busy bit. busy bit.
OSes: 12. IOSys 14
Polling a DevicePolling a Device
Polling often requires only 3 CPU Polling often requires only 3 CPU instructions:instructions:– readread the status register the status register
– use a use a logical-andlogical-and operation to extract the operation to extract the busy bitbusy bit
– branchbranch (goto) is not zero (goto) is not zero
continued
OSes: 12. IOSys 15
Polling becomes inefficient if done too Polling becomes inefficient if done too often.often.
An alternative is to have the device An alternative is to have the device interrupt the host (CPU) when it is free.interrupt the host (CPU) when it is free.
CPU receives interrupt,transfers control tointerrupt handler
interrupt handler: processes data, returns
CPU resumesinterruped task
CPU
initiates I/O
input ready, or outputcomplete, or error
generate interrupt signal
I/O Controller12
3
4
56
7
OSes: 12. IOSys 17
More SophisticationMore Sophistication
Defer interrupt handling during critical Defer interrupt handling during critical processing.processing.
Use more efficient ways of finding the Use more efficient ways of finding the right interrupt handler.right interrupt handler.
Multi-level interrupts.Multi-level interrupts.
Multiple interrupt request lines:Multiple interrupt request lines:– nonmaskablenonmaskable and and maskablemaskable lines lines
OSes: 12. IOSys 18
Interrupt Vector TableInterrupt Vector Table
An An interrupt vector tableinterrupt vector table is used to map an is used to map an interrupt to a routineinterrupt to a routine– done by treating the interrupt number as an offset in done by treating the interrupt number as an offset in
the tablethe table– called an called an interrupt vectorinterrupt vector
Each entry in the table may be a list (chain) of Each entry in the table may be a list (chain) of handlershandlers– allows overloading of the interrupt numberallows overloading of the interrupt number
OSes: 12. IOSys 19
Part of a Pentium Vector TablePart of a Pentium Vector Table
Vector NumberVector Number DescriptionDescription00 Divide errorDivide error33 BreakpointBreakpoint55 Bound range errorBound range error77 Device not availableDevice not available1212 Stack faultStack fault1313 General protectionGeneral protection1414 Page faultPage fault19-3119-31 Intel reservedIntel reserved32-25532-255 Maskable interruptsMaskable interrupts
Fig. 12.4, p.404
OSes: 12. IOSys 20
Threads and InterruptsThreads and Interrupts
A threaded kernel is useful for A threaded kernel is useful for implementing multiple interrupt priorities.implementing multiple interrupt priorities.
The Solaris thread scheduler allows low The Solaris thread scheduler allows low priority interrupt handlers (threads) to be priority interrupt handlers (threads) to be pre-empted by high priority interrupt pre-empted by high priority interrupt handlers (threads).handlers (threads).
OSes: 12. IOSys 21
2.7. Direct Memory Access (DMA)2.7. Direct Memory Access (DMA)
When the host’s CPU does device When the host’s CPU does device handshaking, it is called handshaking, it is called Programmed I/OProgrammed I/O (PIO)(PIO)– a waste of CPU resourcesa waste of CPU resources
Instead, offload the work to a DMA controllerInstead, offload the work to a DMA controller– the CPU sends the I/O command to the DMA, the CPU sends the I/O command to the DMA,
then goes on with other workthen goes on with other work
continued
OSes: 12. IOSys 22
Higher CPU performance since:Higher CPU performance since:– less kernel communicationless kernel communication– less context switchingless context switching
AdvantagesAdvantages
OSes: 12. IOSys 23
DMA DiagramDMA Diagram Fig. 12.5, p.407
disk disk disk disk
CPU
cache
CPU/memory busDMA controller memory buffer
IDE disk controller
PCI Bus
OSes: 12. IOSys 24
Cycle StealingCycle Stealing
Cycle stealingCycle stealing occurs when the DMA occurs when the DMA controller uses the CPU memory buscontroller uses the CPU memory bus– the CPU is prevented from accessing memorythe CPU is prevented from accessing memory
– this can slow down the CPU’s computationthis can slow down the CPU’s computation
OSes: 12. IOSys 25
Direct Virtual Memory Access (DVMA)Direct Virtual Memory Access (DVMA)
In DVMA, data is transferred between two In DVMA, data is transferred between two memory-mapped devices (i.e. from memory-mapped devices (i.e. from controller to controller) without requiring controller to controller) without requiring main memorymain memory– no cycle stealing requiredno cycle stealing required
OSes treat I/O devices in a standard way OSes treat I/O devices in a standard way using:using:– abstraction, encapulation, software layeringabstraction, encapulation, software layering
The user/programmer sees a standardized The user/programmer sees a standardized set of I/O functions.set of I/O functions.
Differences are hidden inside kernel Differences are hidden inside kernel modules called modules called device driversdevice drivers..
OSes: 12. IOSys 27
A Kernel I/O StructureA Kernel I/O StructureFig. 12.6, p.409
SCSIdevicedriver
keyboarddevicedriver
mousedevicedriver
PCI busdevicedriver
floppydevicedriver
ATAPIdevicedriver
SCSIdevice
controller
keyboarddevice
controller
mousedevice
controller
PCI busdevice
controller
floppydevice
controller
ATAPIdevice
controller
SCSIdevices
keyboard mouse PCI busfloppy
disk drivesdisk &
tape drives
kernel I/O subsystem
kernel
OSes: 12. IOSys 28
AdvantagesAdvantages
Simplifies OS developer’s work.Simplifies OS developer’s work. Benefits hardware manufacturersBenefits hardware manufacturers
But…But…– different OSes have different ‘standard’ different OSes have different ‘standard’
Memory mapping:Memory mapping:– useful for executing kernel modules/servicesuseful for executing kernel modules/services
OSes: 12. IOSys 32
3.3. Network Devices3.3. Network Devices
Distinguished due to differences in Distinguished due to differences in addressing and performance (reliablility, addressing and performance (reliablility, data sequencing, speed).data sequencing, speed).
Standard interface:Standard interface:– sockets, message packets, streamssockets, message packets, streams– select() for monitoring collections of socketsselect() for monitoring collections of sockets
continued
OSes: 12. IOSys 33
Other UNIX (inter-process) communication Other UNIX (inter-process) communication features:features:– half-duplex pipeshalf-duplex pipes– full-duplex FIFOs (queues)full-duplex FIFOs (queues)– message queuesmessage queues
OSes: 12. IOSys 34
3.4. Clocks and Timers3.4. Clocks and Timers
Basic uses:Basic uses:– get the current time/the elapsed timeget the current time/the elapsed time– set a timer to trigger an operation at time Tset a timer to trigger an operation at time T
(e.g. time-slice a process)(e.g. time-slice a process)
Timers have coarse resolution (20-60 Timers have coarse resolution (20-60 ticks/sec), which limits the precision of ticks/sec), which limits the precision of triggerstriggers– a CPU can execute ~100M instructions/seca CPU can execute ~100M instructions/sec
Blocking (synchronous) I/OBlocking (synchronous) I/O– the application is suspended until the I/O is the application is suspended until the I/O is
completedcompleted– easy to understand/implementeasy to understand/implement
Nonblocking (asynchronous) I/ONonblocking (asynchronous) I/O– the application does not waitthe application does not wait
continued
OSes: 12. IOSys 36
Some software requires nonblocking I/OSome software requires nonblocking I/O– most games software has to be able to receive most games software has to be able to receive
user input from the keyboard at any time while user input from the keyboard at any time while updating the screen continuouslyupdating the screen continuously
ThreadsThreads– place the blocking I/O in one thread, and let the place the blocking I/O in one thread, and let the
other behaviour execute in other threadsother behaviour execute in other threads
Use nonblocking I/O system callsUse nonblocking I/O system calls– they return immediately with whatever was they return immediately with whatever was
availableavailable– e.g. select() called with a time-out of 0e.g. select() called with a time-out of 0
continued
OSes: 12. IOSys 38
Use asynchronous I/O system callsUse asynchronous I/O system calls– they return immediately, but the completed they return immediately, but the completed
I/O result is ‘available’ some time laterI/O result is ‘available’ some time later
– the result can be made ‘available’ in the result can be made ‘available’ in different ways:different ways:
a special variablea special variable raising a signal (software interrupt)raising a signal (software interrupt) calling a callback functioncalling a callback function
(e.g. used in Java’s event model)(e.g. used in Java’s event model)
OSes: 12. IOSys 39
4. Kernel I/O Subsystems4. Kernel I/O Subsystems
These are features built on top of the These are features built on top of the hardware and device-driver infrastructure, hardware and device-driver infrastructure, including:including:– I/O schedulingI/O scheduling– bufferingbuffering– cachingcaching– spoolingspooling
OSes: 12. IOSys 40
4.1. I/O Scheduling4.1. I/O Scheduling
Scheduling (re-ordering) is carried out to:Scheduling (re-ordering) is carried out to:– improve overall system performanceimprove overall system performance
– reduce average waiting timereduce average waiting time
OSes: 12. IOSys 41
Scheduling Example Scheduling Example
Three jobs want to read from a hard disk. Three jobs want to read from a hard disk. The read arm is currently near the The read arm is currently near the beginning of the disk.beginning of the disk.– job 1: requests a block at the end of the diskjob 1: requests a block at the end of the disk– job 2: requests a block near the startjob 2: requests a block near the start– job 3: requests a block in the middlejob 3: requests a block in the middle
New scheduleNew schedule: jobs 2, 3, 1: jobs 2, 3, 1
OSes: 12. IOSys 42
4.2. Buffering4.2. Buffering
A A bufferbuffer is a memory area that stores data is a memory area that stores data while it is being transferred between two while it is being transferred between two devices, or between a device and a process.devices, or between a device and a process.
OSes: 12. IOSys 43
Three Uses for BufferingThree Uses for Buffering
Cope with speed mismatchesCope with speed mismatches– e.g. between a modem and hard diske.g. between a modem and hard disk– double bufferingdouble buffering may be used may be used
Cope with different data-transfer sizesCope with different data-transfer sizes– e.g. packet transfer over a networke.g. packet transfer over a network
Support Support copy semanticscopy semantics for application I/O for application I/O– e.g. so write() data cannot be changede.g. so write() data cannot be changed
A A cachecache is a region of ‘fast memory’ that is a region of ‘fast memory’ that holds copies of data:holds copies of data:– used to speed-up reuse and updatesused to speed-up reuse and updates– e.g. recently read instructions copied from the e.g. recently read instructions copied from the
hard diskhard disk
Cache and buffer difference:Cache and buffer difference:– a buffer may hold the a buffer may hold the onlyonly copy of a data item copy of a data item
OSes: 12. IOSys 48
4.4. Spooling4.4. Spooling
A A spoolspool is a buffer (or buffers) used to hold is a buffer (or buffers) used to hold output for a device while the device is output for a device while the device is processing the current job.processing the current job.
The device can only process one job at onceThe device can only process one job at once– e.g. a printer, tape readere.g. a printer, tape reader
Spool management is often done by a dedicated Spool management is often done by a dedicated system system daemondaemon (process) (process)
OSes: 12. IOSys 49
Spooling DiagramSpooling Diagram VUW, cs305
Program
Program
Program
Printerdriver
spooldaemon
Printerdriver
OSes: 12. IOSys 50
4.5. UNIX I/O Kernel Data Structures4.5. UNIX I/O Kernel Data StructuresFig. 12.9, p.419
file-system record
inode pointerpointers to functions:read(), write(), select(),...
network record
socket d.s. pointerpointers to functions:read(), write(), select(),...
filedescriptor
process open-file
table
user process
system open-file table
activeinode table
network infotable
kernel
OSes: 12. IOSys 51
A variety of I/O components are A variety of I/O components are controlled/monitored through the open controlled/monitored through the open file table.file table.
Details are hidden from the user by Details are hidden from the user by standard abstractions:standard abstractions:– e.g. read() works with files and network e.g. read() works with files and network
socketssockets
OSes: 12. IOSys 52
5. From I/O Requests to Hardware5. From I/O Requests to Hardware
How does the OS convert an I/O request How does the OS convert an I/O request into hardware commands?into hardware commands?
Consider how UNIX reads from a file on Consider how UNIX reads from a file on disk.disk.
OSes: 12. IOSys 53
Lifecycle of an I/O RequestLifecycle of an I/O RequestFig. 12.10, p.422
Request I/O
can satisfyalready?
Send request todevice driver.Block process.
system call
I/O completed
Transfer datato process
userprocess
yes
no
kernelI/O subsystem
return from call
kernelI/O subsystem
continuedtime
OSes: 12. IOSys 54
Carry out request.Send commands tocontroller. Block.
Determine whenI/O completed.
Tell I/O subsystem.
device controller commands
Monitor device.Interrupt when I/O completed
Receive interrupt.Store data in dd buffer.
Signal dd.
I/O completed.Generate interrupt.
devicedriver
interrupthandler
keyboarddevice
controller
time
OSes: 12. IOSys 55
Filename to Disk ControllerFilename to Disk Controller
A UNIX path name has no clear separation A UNIX path name has no clear separation of the device portion:of the device portion:/home/fidji/ad/public_html/index.html/home/fidji/ad/public_html/index.html
UNIX stores a mount table (in UNIX stores a mount table (in /etc/mnttab/etc/mnttab) ) of device name to pathname details:of device name to pathname details:
The name (e.g. The name (e.g. /dev/dsk/c1t1d0s0/dev/dsk/c1t1d0s0) is ) is looked up in the file-system, and UNIX looked up in the file-system, and UNIX finds:finds:– <major, minor> device numbers<major, minor> device numbers
The The major device numbermajor device number identifies the identifies the device driver type (e.g. character/block device driver type (e.g. character/block transfer).transfer).
continued
OSes: 12. IOSys 57
The The minor device numberminor device number can be used to can be used to index into a device table to find the device index into a device table to find the device controller’s I/O port address or its memory controller’s I/O port address or its memory mapped address.mapped address.
OSes: 12. IOSys 58
Stream ModulesStream Modules UNIX System V allows pipelines of driver UNIX System V allows pipelines of driver
code to be assembled dynamically for an code to be assembled dynamically for an application.application.
module 1
module 2
module N
hardware
Applicationstream head
driver end
OSes: 12. IOSys 59
6. Performance6. Performance
I/O is a major factor in system I/O is a major factor in system performance:performance:– it uses the CPU heavily to execute device it uses the CPU heavily to execute device
drivers, schedule processesdrivers, schedule processes– cycle stealingcycle stealing– costs of interrupt processingcosts of interrupt processing– network trafficnetwork traffic
OSes: 12. IOSys 60
Network Comms.Network Comms. Fig. 12.11, p.425
char typed
interrupt generated
interrupt handled
device driver
kernel
user process kernel
device driver
network adapter
interrupt generated
interrupt handled
system call completes
contextswitch
state saved
hardware
state saved
context switch
network
contextswitch Sending
System
OSes: 12. IOSys 61
network
packet receive
d
network adapter
interrupt generated
device driver
kernel
networkdaemon
kernel
networksubdaemon
contextswitch
state saved
hardware
network
contextswitch
context switch
ReceivingSystem
OSes: 12. IOSys 62
Ways to Improve I/O EfficiencyWays to Improve I/O Efficiency
Reduce the number of context switches.Reduce the number of context switches.
Reduce the number of data copys to/from Reduce the number of data copys to/from memory.memory.
Reduce the number of interrupts.Reduce the number of interrupts.
continued
OSes: 12. IOSys 63
Offload I/O work to other processors.Offload I/O work to other processors.
Move more processing into hardware.Move more processing into hardware.
Balance the performance of CPU, memory, Balance the performance of CPU, memory, bus, and the I/O.bus, and the I/O.
OSes: 12. IOSys 64
Efficiency ExamplesEfficiency Examples
The telnet daemon in Solaris runs inside a The telnet daemon in Solaris runs inside a kernel thread:kernel thread:– that eliminates context switching between the that eliminates context switching between the
daemon and the kerneldaemon and the kernel
Front-end processorsFront-end processors– e.g. terminal concentratorse.g. terminal concentrators
continued
OSes: 12. IOSys 65
DMA controllersDMA controllers
I/O channelsI/O channels– an I/O channel is a dedicated CPU for an I/O channel is a dedicated CPU for
offloading I/O work from the main CPUoffloading I/O work from the main CPU
OSes: 12. IOSys 66
Where to put I/O Functionality?Where to put I/O Functionality?Fig. 12.12, p.426