![Page 1: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/1.jpg)
I/O Devices
CSC501 Operating Systems Principles
1
![Page 2: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/2.jpg)
Previous Lectures
q AbstractionQ ProcessQ Memory
q NowQ I/O Devices
![Page 3: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/3.jpg)
Basic Computer Architecture
CPU
memory
I/O Devices
![Page 4: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/4.jpg)
CPUSystem Bus &MMU/AGP/PCI
Controller
I/O Bus
IDE DiskController
USBController Another
I/O BusSerial & Parallel Ports
Keyboard & Mouse
Intel SR440BX MotherboardMemory
![Page 5: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/5.jpg)
q The largest, most complex subsystem in OSq Most lines of code with the highest rate of code
changesQ Where OS engineers most likely to workQ Difficult to test thoroughlyQ Big impact on performance and perceptionQ Bigger impact on acceptability in market
OS Designer’s Perspective
![Page 6: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/6.jpg)
Principles of I/O Software
q Efficiency - Do not allow I/O operations to become system bottleneck, especially for slow devices
q Generality - Provide a common API to access I/O devicesQ Uniform naming/interfacev open, close read, write…
Q Device independencev isolate OS and application programs from device
specific details and peculiaritiesv isolate the impact of device errors, retry where
possiblen Errors are abundant in I/O
6
![Page 7: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/7.jpg)
I/O Software “Stack”
User Level Software
Device IndependentSoftware
Device Drivers
Interrupt Handlers
Hardware
I/O API & libraries
Device Dependent
Device Dependent – as
short as possible
(Rest of the OS)
![Page 8: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/8.jpg)
Communication with I/O Devices
q Programmed I/O (a.k.a., polling)q Interrupt-Driven I/Oq DMA-based I/O
8
![Page 9: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/9.jpg)
Programmed I/O (Polling)
q Used when device and controller are relatively quick to process an I/O operationQ Device driverv Gains access to devicev Initiates I/O operationv Loops testing for completion of I/O operationv If there are more I/O operations, repeat
Q Used in following kinds of cases v Service interrupt time > Device response timev Device has no interrupt capabilityv Embedded systems where CPU has nothing else to do
Any Example?
![Page 10: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/10.jpg)
Interrupt-Driven I/Oq Interrupts occur on I/O events Q Operation completion Q Error or change of status
q Interrupt handlingQ Step 1: Save the current machine stateQ Step 2: Load the machine state for interrupt
handlingQ Step 3: Invoke the corresponding ISRQ Step 4: Resume the program execution
Any Example?
Question: Where do we save the current machine states?
![Page 11: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/11.jpg)
DMA-Based I/O
q Direct Memory AccessQ Device read/write directly from/to memoryQ Transfer from memory to device typically
initiated from CPUQ Transfer from device to memory can be
initiated by the device or the CPU
11
Any Example?
![Page 12: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/12.jpg)
DMA-Based I/O
12
![Page 13: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/13.jpg)
q Comparison: Polling, Interrupt, and DMAQ Polling and Interrupt = word at a timeQ DMA = block at a time
Communication with I/O Devices
CPU Memory
Disk
Interconnect
CPU Memory
Disk
Interconnect
CPU Memory
Disk
Interconnect
Polling Interrupt DMA
![Page 14: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/14.jpg)
14
Question:
How should the device driver be designed?
![Page 15: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/15.jpg)
Device Driver
q Hides the device specifics from the above layersQ Supporting a common API
q Translates logical I/O into device I/OQ E.g., logical disk blocks into {head, track, sector}Q Performs data buffering and scheduling of I/O
operationsq StructureQ Several synchronous entry points: device
initialization, queue I/O requests, state control, read/write
Q An asynchronous entry point to handle interrupts
![Page 16: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/16.jpg)
Device Driver – A Typical Design
q OS and driver communication: OS-dependentQ Commands and data between OS and driversv Meet the interface specs of the device independent
layerv Utilize the facilities supplied by the OS – buffers,
error codes, etc.q Driver and hardware comm.: Device-dependentQ Commands and data between driver and hardwarev Actions during interrupt handling
16
![Page 17: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/17.jpg)
Device Driver – A Typical Design
q Driver operationsQ Initialize devicesQ Interpret commands from OSQ Schedule multiple outstanding requestsQ Manage data transferQ Accept and process interruptsQ …
q Typically use open-close-read-write paradigmQ Support open, close, read, write, seek functionsQ Use ioctl function as escape mechanism
17
![Page 18: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/18.jpg)
Uniform API (e.g., in Linux)
18
![Page 19: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/19.jpg)
Case Study w/ Xinu
q Code divided into two partsQ Upper Halfv Defines abstract operationsv Executed by application to request I/O
Q Lower Halfv Device-specific interrupt routinev Executed by arbitrary (current) processv Invoked when operation completes
19
User
system call Device
specific upper-half
routine
Device specific
lower-half routine
Device
Device switch table devtab[ ]
![Page 20: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/20.jpg)
Case Study w/ Xinu
q Device switch tableQ One row per deviceQ One column per operationQ Entry points to a procedure
20
![Page 21: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/21.jpg)
21
int dvnum;
long int (*dvinit)();
long int (*dvshutdown)();
long int (*dvopen)();
long int (*dvclose)();
long int (*dvread)();
long int (*dvwrite)();
long int (*dvseek)();
long int (*dvgetc)();
long int (*dvputc)();
long int (*dvcntl)();
long int (*dviint)();
long int (*dvoint)();
long int dvcsr;
long int dvivec;
long int dvovec;
char *dvioblk;
int dvminor;
device switch table entry ( struct devsw)
Ptrs to device specific high-level
routines
The structure of the control block varies from device to device. The control block contains information associated with the device.
Ptr to device control block
Device Switch Table
![Page 22: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/22.jpg)
Case Study w/ Xinu
q Device switch tableQ Device-independent operations ‘‘generic’’Q Given operation may not apply to given devicev Example: seek on keyboard or network
Q All entries in device switch table must be valid
22
Any solution?
![Page 23: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/23.jpg)
Case Study w/ Xinu
q Device switch tableQ Device-independent operations ‘‘generic’’Q Given operation may not apply to given devicev Example: seek on keyboard ornetwork
Q All entries in device switch table must be validq Special EntriesQ ionullv Used for innocuous operation, always returns OK
Q ioerrv Used for incorrect operation, always returns
SYSERR
23
![Page 24: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/24.jpg)
Example Device Driver tty in Xinu
q Used for I /O to consoleq Separate input and output buffersq Use semaphores to synchronize
q Upper-half routinesQ ttyinit, ttyopen, ttyclose, ttyread, ttywrite,
ttyputc, ttygetc, ttypcntlq Lower-harf routinesQ ttyiin (input interrupt handler) Q ttyoin (output interrupt handler)
24
![Page 25: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/25.jpg)
Example Device Driver tty in Xinu
q Actions during a character outputQ Upper-halfv Waits for buffer spacev Deposits characterv Triggers device
Q Lower-halfv Extracts character and pass to devicev Signals space semaphore
25
![Page 26: lec15-io - NC State Computer Science€¦ · QManage data transfer QAccept and process interrupts Q… qTypically use open-close-read-write paradigm QSupport open, close, read, write,](https://reader035.vdocuments.site/reader035/viewer/2022071220/605a13df41e77b246c482edc/html5/thumbnails/26.jpg)
Next Lecture
q Hard Disk Q Scheduling and RAID
26