You are on page 1of 27

Operating Systems

ECEG-5202

I/O MANAGEMENT
Outline
▪I/O Devices
▪Principles of I/O software
▪I/O Buffering
▪Disk cache

January 2, 2023 IO MANAGEMENT 2


I/O devices
Categories of external devices that engage in I/O
◦ Human readable
◦ Suitable for communication with the computer user
◦ Example: Printer, keyboard, and mouse
◦ Machine readable
◦ Suitable for communicating with electronic equipment
◦ Example: disk drives, USB sticks, sensors, …
◦ Communication
◦ Suitable for communicating with remote devices
◦ Example: modem

January 2, 2023 IO MANAGEMENT 3


I/O devices…
Differences in I/O devices
◦ Data rate
◦ Data transfer rate

January 2, 2023 IO MANAGEMENT 4


I/O devices…
Differences in I/O devices…
◦ Application
◦ The use to which a device is put has an influence on the software and policies in the OS and supporting utilities
◦ E.g., disk used for files vs. disk used for backup
◦ Complexity of control
◦ Disk requires more complex I/O module than printer (for example)
◦ Unit of transfer
◦ Data may be transferred as a stream of bytes or characters (e.g., terminal I/O) or in larger blocks (e.g., disk I/O)
◦ Data representation
◦ Different data encoding schemes are used by different devices, including differences in character code and parity
conventions
◦ Error conditions
◦ The nature of errors, the way in which they are reported, their consequences, and the available range of
responses differ widely from one device to another

=> Uniform and consistent approach to I/O from the point of view of both the OS and
the user processes is difficult to achieve

January 2, 2023 IO MANAGEMENT 5


I/O devices…
Recap: I/O techniques
◦ Programmed I/O
◦ The processor issues an I/O command, on behalf of a process, to an I/O module
◦ The process then busy waits for the operation to be completed before proceeding
◦ Interrupt-driven I/O
◦ I/O module will interrupt the processor and request to exchange data
◦ Requires active intervention of the processor to transfer data between memory and an I/O module
◦ Direct memory access (DMA)
◦ A DMA module controls the exchange of data between main memory and an I/O module
◦ The processor sends a request for the transfer of a block of data to the DMA module and is interrupted
only after the entire block has been transferred

January 2, 2023 IO MANAGEMENT 6


Principles of I/O software
Goals of I/O software design
◦ Device independence
◦ Allows to be able to write programs that can access I/O devices without having to specify the device in
advance
◦ Uniform naming
◦ Name of a file or a device should simply be a string or an integer and not depend on the device in any way
◦ Error handling
◦ Errors should be handled as close to the hardware as possible
◦ Example
◦ If controller discovers a read error, it should try to correct the error itself if it can. If it cannot, then the
device driver should handle it

January 2, 2023 IO MANAGEMENT 7


Principles of I/O software…
Goals of I/O software design…
◦ Synchronous (blocking) vs. asynchronous (interrupt-driven)
◦ Most physical I/O is asynchronous
◦ Asynchronous—the CPU starts the transfer and goes off to do something else until the interrupt arrives
◦ User programs are easier to write if I/O operations are blocking
◦ Blocking —after a read system call the program is automatically suspended until the data are available
in the buffer
◦ OS makes operations that are interrupt-driven look blocking to the user programs
◦ Buffering
◦ Often data that come off a device cannot be stored directly in their final destination (e.g., packets)
◦ Some devices have several real-time constraints
◦ Example
◦ Digital audio devices try to avoid data overruns
◦ Data must be put into an output buffer in advance to decouple the rate at which the buffer is filled
from the rate at which it is emptied
◦ Involves a major copying and often has a major impact on I/O performance

January 2, 2023 IO MANAGEMENT 8


Principles of I/O software…
Goals of I/O design…
◦ Shareable vs. dedicated devices
◦ Some I/O devices are used by many users at the same time
◦ E.g., disks
◦ Some I/O devices have to be dedicated to a single user until that user is finished
◦ E.g., printer
◦ OS should be able to handle both types of devices

January 2, 2023 IO MANAGEMENT 9


Principles of I/O software…
Logical structure of I/O function
◦ I/O facility could be organized using
layers
◦ Most common logical structures

January 2, 2023 IO MANAGEMENT 10


Principles of I/O software…
Logical structure of I/O function
◦ Local peripheral device (a)
◦ Communicates in a simple fashion such as a stream of bytes or records
◦ Logical I/O
◦ Deals with the device as a logical resource and is not concerned with the details of actually controlling
the device
◦ Concerned with managing general I/O functions on behalf of user processes
◦ Allows them to deal with the device in terms of a device identifier and simple commands such as
open, close, read, and write
◦ Device I/O
◦ Requested operations and data (buffered characters, records, etc.) are converted into appropriate
sequences of I/O instructions, channel commands, and controller order
◦ Buffering technique may be used to improve utilization

January 2, 2023 IO MANAGEMENT 11


Principles of I/O software…
Logical structure of I/O function…
◦ Local peripheral device (a)…
◦ Scheduling and control
◦ Queuing, controlling and scheduling of I/O operations occur at this layer,
◦ Interrupts are handled at this layer
◦ Is the layer of the software that interacts with the device hardware
◦ Communication port (b)
◦ Same as the local peripheral device
◦ Logical I/O module is replaced by a communications architecture which contains different layers
(e.g., TCP/IP layer)

January 2, 2023 IO MANAGEMENT 12


Principles of I/O software…
Logical structure of I/O function…
◦ File system (c)
◦ In addition to the layers discussed above
◦ Directory management
◦ Symbolic file names are converted to identifiers that reference the file
◦ Concerned with user operations that affect the directory of files (e.g., add, delete, …)
◦ File system
◦ Deals with logical structure of files and with the operations that can be specified by users
(e.g., open, close, read, …)
◦ Access right is managed at this layer
◦ Physical organization
◦ Allocation of secondary storage space and main storage buffers
◦ Address translation from logical to physical

January 2, 2023 IO MANAGEMENT 13


I/O buffering
Type of I/O devices
◦ Block-oriented
◦ Information is stored in fixed sized blocks
◦ Transfers are made a block at a time
◦ Used in disks
◦ Stream-oriented
◦ Transfer information as a stream of bytes
◦ Used for terminals, printers, communication ports, mouse, and most other devices that are not
secondary storage

January 2, 2023 IO MANAGEMENT 14


I/O buffering…
Problems during an I/O operation
◦ Processes must wait for I/O to complete before proceeding
◦ I/O is usually slow
◦ Certain pages must remain in main memory during I/O
◦ Used while transferring data
◦ Portion of the process containing the target location must be locked into main memory

How to avoid these overheads and inefficiencies?


◦ Perform input transfers in advance of requests being made
◦ Perform output transfers some time after the request is made

=> Buffering

January 2, 2023 IO MANAGEMENT 15


I/O buffering…
Single buffer
◦ Operating system assigns a buffer in main memory for an I/O request
◦ Buffering scheme for block-oriented I/O
◦ Input transfers are made to the system buffer
◦ When transfer is completed, the process moves the block to user space and another block is moved into
the buffer
=> Called Read ahead (anticipated input)
◦ Done in expectation that the next block will eventually be needed
◦ Usually data is read sequentially

January 2, 2023 IO MANAGEMENT 16


I/O buffering…
Single buffer…
◦ Buffering scheme for block-oriented I/O…
◦ Block-oriented output
◦ When data is transmitted to a device
1. Copied from the user space into system buffer
2. Write the data from the system buffer
◦ Advantage
◦ User process can be processing one block of data while the next block is being read in
◦ OS could swap the process out because the input operation is taking place in system memory
rather than process memory
◦ Disadvantage
◦ Complicates the logic in the OS – The OS must keep track of the assignment of system buffers
to user processes
◦ If the I/O operation involves the same disk that is used for swapping, it hardly makes sense to
queue disk writes to the same device for swapping the process out

January 2, 2023 IO MANAGEMENT 17


I/O buffering…
Single buffer…
◦ Buffering scheme for stream-oriented I/O
◦ Can be used in a
◦ Line-at-a-time fashion
◦ Buffer can be used to hold a single line
◦ User process is suspended during input, awaiting the arrival of the entire line
◦ User process places a line of output in the buffer and continue processing
◦ User process need not be suspended unless it has a second line of output to send before
the buffer is emptied
◦ E.g., line printer, one line user input with a carriage return
◦ Byte-at-a-time fashion
◦ Used when each keystroke is significant
◦ E.g., peripherals such as sensors and controllers
◦ Interaction between the OS and the user process follows the producer-consumer model

January 2, 2023 IO MANAGEMENT 18


I/O buffering…
Double buffering (buffer swapping)
◦ Improvement over the single buffer approach
◦ Uses two system buffers instead of one
◦ A process transfers data to (or from) one buffer while the OS empties (or fills) the
other
◦ Ensures that the process doesn’t have to wait on I/O
◦ Achieved at the cost of increased complexity
◦ For block oriented I/O there is improvement
◦ For stream oriented I/O the improvement is observed only for line-at-a-time I/O

January 2, 2023 IO MANAGEMENT 19


I/O buffering…
Circular buffering
◦ Double buffering may be inadequate if the process performs rapid bursts of I/O
◦ Solution
◦ Use more than two buffers, which are referred to as a circular buffer
◦ Each individual buffer is one unit in a circular buffer
◦ Used when I/O operation must keep up with process

January 2, 2023 IO MANAGEMENT 20


Disk cache
Recap
◦ What is cache memory?
◦ Memory that is smaller and faster than main memory
◦ Located between main memory and the processor
◦ Reduces average memory access time by exploiting the principle of locality

Same concept goes for disk cache


Disk cache
◦ Is a buffer in main memory for disk sectors
◦ Contains a copy of some of the sectors on the disk
◦ When I/O request is made for a particular sector, a check is made to
determine if the sector is in the disk cache

January 2, 2023 IO MANAGEMENT 21


Disk cache…
Design considerations
◦ When an I/O request is satisfied, from where should the process access the
data?
◦ Transfer the block of data within main memory from disk cache to memory assigned to the user
process
◦ Use a shared memory capability and passing a pointer to the appropriate slot in the disk cache
◦ Saves the time of memory-to-memory transfer
◦ Allows shared access by other processes

January 2, 2023 IO MANAGEMENT 22


Disk cache…
Design considerations…
◦ Which existing block in the cache to replace with a new block?
◦ When a new sector is brought into the disk cache, one of the existing blocks must be replaced
◦ Least recently used (LRU)
◦ Most commonly used algorithm
◦ Replace the block that has been in the cache longest with no reference to it
◦ Removes the block at the bottom of the stack and pushes a new (recently accessed) block on the top
◦ Least frequently used (LFU)
◦ Replaces the block in the set that has experienced the fewest references
◦ Implemented by associating a counter with each block
◦ Weakness
◦ Certain blocks could be referenced relatively infrequently, but when they are referenced, there are
short intervals of repeated references
=> Doesn’t reflect the probability that the block will soon be referenced again, resulting in poor
replacement algorithm

January 2, 2023 IO MANAGEMENT 23


Disk cache…
Design considerations…
◦ Which existing block in the cache to replace with a new block...
◦ Least frequently used (LFU) …
◦ Improvement
◦ Logically organized similar to LRU
◦ The stack is divided into two sections: New section and Old section
◦ Reference count is incremented only when a block is moved from old section to new section
◦ On a miss, a block with small reference count and in the old section will be replaced

January 2, 2023 IO MANAGEMENT 24


Disk cache…
Design considerations…
◦ Which existing block in the cache to replace with a new block...
◦ Least frequently used (LFU) …
◦ Problem
◦ May not have sufficiently long interval for blocks aging out of the new section to build up
their reference counts
◦ Replacement could take place
◦ On demand
◦ Sector replace only when the slot is needed
◦ Preplanned
◦ Number of slots are released at a time
◦ If the sector is updated, there is a need to write back sectors
◦ Clusters the writing and order the writing to minimize seek time

January 2, 2023 IO MANAGEMENT 25


Disk cache…
Performance consideration
◦ Focus
◦ Whether a given miss ratio can be achieved
◦ Depends on
◦ Locality behavior of the disk references
◦ Replacement algorithm
◦ Other design considerations such as block size
◦ Miss ratio is a function of the size of the
disk cache
◦ Example
◦ Some disk cache performance results using LRU

January 2, 2023 IO MANAGEMENT 26


Acknowledgment
These slides are adopted from the slides of
Surafel Lemma Abebe (Ph. D.)

Here, I would like to acknowledge and thank him for allowing me to


customize and use the slides for this course.

January 2, 2023 IO MANAGEMENT 27

You might also like