Operating System - I/O
Hardware
I/O Hardware
• One of the important jobs of an Operating System is to
manage various I/O devices including mouse,
keyboards, touch pad, disk drives, display adapters,
USB devices, Bit-mapped screen, LED, Analog-to-
digital converter, On/off switch, network connections,
audio I/O, printers etc.
• An I/O system is required to take an application I/O
request and send it to the physical device, then take
whatever response comes back from the device and
send it to the application
I/O Hardware
• I/O devices can be divided into two categories −
• Block devices − A block device is one with which the
driver communicates by sending entire blocks of data.
For example, Hard disks, USB cameras, Disk-On-Key
etc.
• Character devices − A character device is one with
which the driver communicates by sending and
receiving single characters (bytes, octets). For
example, serial ports, parallel ports, sounds cards etc
Device Controllers
• Device drivers are software modules that can be
plugged into an OS to handle a particular device.
• Operating System takes help from device drivers to
handle all I/O devices.
• The Device Controller works like an interface between
a device and a device driver. I/O units (Keyboard,
mouse, printer, etc.) typically consist of a mechanical
component and an electronic component where
electronic component is called the device controller.
Device Controllers
Synchronous vs asynchronous I/O
• Synchronous I/O − In this scheme CPU execution
waits while I/O proceeds
• Asynchronous I/O − I/O proceeds concurrently with
CPU execution
Communication to I/O Devices
• The CPU must have a way to pass information to and
from an I/O device.
• There are three approaches available to communicate
with the CPU and Device.
– Special Instruction I/O
– Memory-mapped I/O
– Direct memory access (DMA)
Special Instruction I/O
• This uses CPU instructions that are specifically made
for controlling I/O devices.
• These instructions typically allow data to be sent to an
I/O device or read from an I/O device.
Memory-mapped I/O
• When using memory-mapped I/O, the same address
space is shared by memory and I/O devices.
• The device is connected directly to certain main
memory locations so that I/O device can transfer block
of data to/from memory without going through CPU.
• While using memory mapped IO, OS allocates buffer in
memory and informs I/O device to use that buffer to
send data to the CPU.
• I/O device operates asynchronously with CPU,
interrupts CPU when finished.
Direct Memory Access (DMA)
• Direct Memory Access (DMA) means CPU grants I/O
module authority to read from or write to memory
without involvement.
• DMA module itself controls exchange of data between
main memory and the I/O device.
• CPU is only involved at the beginning and end of the
transfer and interrupted only after entire block has
been transferred.
• Direct Memory Access needs a special hardware
called DMA controller (DMAC) that manages the data
transfers and arbitrates access to the system bus.
Polling vs Interrupts I/O
• A computer must have a way of detecting the arrival of
any type of input.
• There are two ways that this can happen, known as
polling and interrupts.
• Both of these techniques allow the processor to deal
with events that can happen at any time and that are
not related to the process it is currently running.
Polling I/O
• Polling is the simplest way for an I/O device to
communicate with the processor.
• The process of periodically checking status of the
device to see if it is time for the next I/O operation, is
called polling.
• The I/O device simply puts the information in a Status
register, and the processor must come and get the
information.
Interrupts I/O
• An alternative scheme for dealing with I/O is the
interrupt-driven method.
• An interrupt is a signal to the microprocessor from a
device that requires attention.
• A device controller puts an interrupt signal on the bus
when it needs CPU’s attention when CPU receives an
interrupt, It saves its current state and invokes the
appropriate interrupt handler using the interrupt vector
(addresses of OS routines to handle various events).
Operating System - I/O
Softwares
I/O Softwares
• I/O software is often organized in the following layers −
– User Level Libraries − This provides simple interface to
the user program to perform input and output. For
example, stdio is a library provided by C and C++
programming languages.
– Kernel Level Modules − This provides device driver to
interact with the device controller and device
independent I/O modules used by the device drivers.
– Hardware − This layer includes actual hardware and
hardware controller which interact with the device drivers
and makes hardware alive.
I/O Softwares
Device Drivers
• Device drivers are software modules that can be
plugged into an OS to handle a particular device.
• Operating System takes help from device drivers to
handle all I/O devices.
• Device drivers encapsulate device-dependent code
and implement a standard interface in such a way that
code contains device-specific register reads/writes.
• Device driver, is generally written by the device's
manufacturer and delivered along with the device on a
CD-ROM.
Device Drivers
• A device driver performs the following jobs −
– To accept request from the device independent software
above to it.
– Interact with the device controller to take and give I/O
and perform required error handling
– Making sure that the request is executed successfully
Interrupt handlers
• An interrupt handler, also known as an interrupt service
routine or ISR, is a piece of software or more
specifically a callback function in an operating system
or more specifically in a device driver, whose execution
is triggered by the reception of an interrupt.
• When the interrupt happens, the interrupt procedure
does whatever it has to in order to handle the interrupt,
updates data structures and wakes up process that
was waiting for an interrupt to happen.
Device-Independent I/O Software
• Following is a list of functions of device-independent I/O
Software −
– Uniform interfacing for device drivers
– Device naming - Mnemonic names mapped to Major and
Minor device numbers
– Device protection
– Providing a device-independent block size
– Buffering because data coming off a device cannot be
stored in final destination.
– Storage allocation on block devices
– Allocation and releasing dedicated devices
– Error Reporting
User-Space I/O Software
• These are the libraries which provide richer and
simplified interface to access the functionality of the
kernel or ultimately interactive with the device drivers.
• Most of the user-level I/O software consists of library
procedures with some exception like spooling system
which is a way of dealing with dedicated I/O devices in
a multiprogramming system.
Kernel I/O Subsystem
• Kernel I/O Subsystem is responsible to provide many services
related to I/O. Following are some of the services provided.
– Scheduling − Kernel schedules a set of I/O requests to determine a
good order in which to execute them.
– Buffering − Kernel I/O Subsystem maintains a memory area known
as buffer that stores data while they are transferred between two
devices or between a device with an application operation
– Caching − Kernel maintains cache memory which is region of fast
memory that holds copies of data.
– Spooling and Device Reservation − A spool is a buffer that holds
output for a device, such as a printer, that cannot accept interleaved
data streams.
– Error Handling − An operating system that uses protected memory
can guard against many kinds of hardware and application errors.
Operating System - File
System
File
• A file is a named collection of related information that is
recorded on secondary storage such as magnetic
disks, magnetic tapes and optical disks.
• In general, a file is a sequence of bits, bytes, lines or
records whose meaning is defined by the files creator
and user.
File Structure
• A File Structure should be according to a required
format that the operating system can understand.
– A file has a certain defined structure according to its
type.
– A text file is a sequence of characters organized into
lines.
– A source file is a sequence of procedures and functions.
– An object file is a sequence of bytes organized into
blocks that are understandable by the machine.
– When operating system defines different file structures,
it also contains the code to support these file structure.
File Type
• File type refers to the ability of the operating system to
distinguish different types of file such as text files
source files and binary files etc.
• Many operating systems support many types of files.
• Operating systems have the following types of files −
– Ordinary files
– Directory files
– Special files
Special files
• These are the files that contain user information.
• These may have text, databases or executable
program.
• The user can apply various operations on such files
like add, modify, delete or even remove the entire file.
Directory files
• These files contain list of file names and other
information related to these files.
Special files
• These files are also known as device files.
• These files represent physical device like disks,
terminals, printers, networks, tape drive etc.
• These files are of two types −
– Character special files − data is handled character by
character as in case of terminals or printers.
– Block special files − data is handled in blocks as in the
case of disks and tapes.
File Access Mechanisms
• File access mechanism refers to the manner in which
the records of a file may be accessed.
• There are several ways to access files −
– Sequential access
– Direct/Random access
– Indexed sequential access
Sequential access
• A sequential access is that in which the records are
accessed in some sequence, i.e., the information in the
file is processed in order, one record after the other.
• This access method is the most primitive one.
• Example: Compilers usually access files in this fashion.
Direct/Random access
• Random access file organization provides, accessing
the records directly.
• Each record has its own address on the file with by the
help of which it can be directly accessed for reading or
writing.
• The records need not be in any sequence within the file
and they need not be in adjacent locations on the
storage medium.
Indexed sequential access
• This mechanism is built up on base of sequential
access.
• An index is created for each file which contains
pointers to various blocks.
• Index is searched sequentially and its pointer is used to
access the file directly.
Space Allocation
• Files are allocated disk spaces by operating system.
• Operating systems deploy following three main ways to
allocate disk space to files.
– Contiguous Allocation
– Linked Allocation
– Indexed Allocation
Contiguous Allocation
• Each file occupies a contiguous address space on
disk.
• Assigned disk address is in linear order.
• Easy to implement.
• External fragmentation is a major issue with this type of
allocation technique.
Linked Allocation
• Each file carries a list of links to disk blocks.
• Directory contains link / pointer to first block of a file.
• No external fragmentation
• Effectively used in sequential access file.
• Inefficient in case of direct access file.
Indexed Allocation
• Provides solutions to problems of contigous and linked
allocation.
• A index block is created having all pointers to files.
• Each file has its own index block which stores the
addresses of disk space occupied by the file.
• Directory contains the addresses of index blocks of
files.