Chapter 5 input output management
Chapter 5 input output management
Besides providing abstractions like processes, address spaces and files, and OS also controls all
of the computer’s I/O devices.
I/O devices
I/O devices are the pieces of hardware used by a human (or other system) to communicate with a
computer. For instance, a keyboard or computer mouse is an input device for a computer, while
monitors and printers are output devices. Devices for communication between computers, such
as modems and network cards, typically perform both input and output operations.
I/O devices can be roughly divided into:
i. Block devices
stores information in a fixed sized block, each one with its own address.
common block size ranges from 512 bytes to 32,768 bytes
data transfer takes place in blocks
block addressable not byte addressable
e.g. Hard disks, CD-ROMs, USB sticks etc.
DEVICE CONTROLLERS
• A device controller is a piece of hardware that receives commands from the system bus,
translates them into device actions and reads/writes the data onto the system bus.
• I/O devices typically consists of two components: electrical and mechanical
• The electronic component is called the device controller or the adapter
• A device controller is a part of a computer system that makes sense of the signals going to,
and coming from the CPU
• There are many device controllers in a computer system
• Any device connected to the computer is connected by a plug and socket, and the socket is
connected to a device controller
• In personal computer, device controller usually takes the form of a chip on the parent board
• Many controllers can handle two, four or even eight identical devices
Memory-mapped I/O
Memory-mapped I/O uses the same address bus to address both memory and I/O devices
The memory and registers of the I/O devices are mapped to (associate with) address values.
When an address is accessed by the CPU, it may refer to a portion of physical RAM, but it
can also refer to memory of the I/O device.
The CPU instructions used to access the memory can also be used for accessing devices.
Each I/O device monitors the CPU's address bus and responds to any CPU access of an
address assigned to that device, connecting the data bus to the desired device's hardware
register.
To accommodate the I/O devices, areas of the addresses used by the CPU must be reserved
Compiled by : Ravi Nandan Karn
for I/O and must not be available for normal physical memory
Direct Memory Access (DMA) is a method that allows an input/output (I/O) device to send or
receive data directly to or from the main memory, bypassing the CPU to speed up memory
operations. CPU is only involved at the beginning and end of the transfer and interrupted only
after entire block has been transferred.
Slow devices like keyboards will generate an interrupt to the main CPU after each byte is
transferred. If a fast device such as a disk generated an interrupt for each byte, the operating
system would spend most of its time handling these interrupts. So a typical computer uses direct
memory access (DMA) hardware to reduce this overhead.
Direct Memory Access needs a special hardware called DMA controller (DMAC) that manages
the data transfers and arbitrates access to the system bus. The controllers are programmed with
source and destination pointers (where to read/write the data), counters to track the number of
transferred bytes, and settings, which includes I/O and memory types, interrupts and states for
the CPU cycles.
Advantage
Free up CPU
high transfer rates
fewer CPU cycles for each transfer
Disadvantages
DMA transfer requires a DMA controller to carry out the operation, hence
more expensive system
synchronization mechanisms must be provided in order to avoid accessing non-updated
information from RAM
Here is another figure shows all the layers of the input/output software system along with their
principal functions.
Device specific code for controlling the device is called device driver.
Written by device’s manufacturer and delivered along with the device.
Device driver is the software part that communicates to the device controller, giving it
commands and accepting response
A driver provides the software interface to hardware devices, enabling the OS and
other programs to access hardware functions without having to now the precise details
of the hardware being used.
i. Accept read/write request from device independent I/O software above it.
ii. Initialize the device if needed.
iii. Manage its power requirements and log events
iv. Check whether the input parameters are valid
v. Translate parameters from abstract to concrete terms (e.g, linear block number to
CHS(Cylinder Head, Sector) for disk)
vi. Check if the device is currently in use. If it is request will be queued for later
processing.
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
Compiled by : Ravi Nandan Karn
happen.
Although some of the I/O software is device specific, other parts of it are device independent.
The basic function of device-independent software is to perform the I/O functions that are
common to all devices and provide uniform interface to user-level 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.
I/O Libraries (e.g., stdio) are in user-space to provide an interface to the OS resident device-
independent I/O SW. For example putchar(), getchar(), printf() and scanf() are example of user
level I/O library stdio available in C programming.
Most input and output devices are much slower than the CPU—so much slower that it would be
a terrible waste of the CPU to make it wait for the input devices. For example, compare the speed
you can type with the speed the CPU can execute instructions. Even a very fast typist could
probably type no more than 10 characters per second, while a modern CPU can execute more
than two billion instructions in that same second!
If the CPU simply waits for the next input character, we call this polled I/O. In this method, the
CPU continuously polls the I/O device in a program loop, waiting for the next input. Think of a
game where a basketball player is asked to make as many free-throws as possible in one minute,
Interrupt driven
An alternative scheme for dealing with I/O is the interrupt-driven method. Here the CPU works
on its given tasks continuously. When an input is available, such as when someone types a key
on the keyboard, then the CPU is interrupted from its work to take care of the input data. In our
example, the basketball player would take shots one after another, while a second person
watched the clock down the hall. When the clock reached one minute, the person watching the
clock would yell down the hallway for the player to stop. This allows the player to take many
more shots, but at the expense of needing someone to watch the clock. Similarly, the CPU can
work continuously on a task without checking the input devices, allowing the devices themselves
to interrupt it as necessary. This requires some extra "smarts" in the form of electronic circuitry
at the I/O devices so that they can interrupt the CPU.
GUI and CUI are two types of User Interfaces. GUI stands for Graphical User Interface while
CUI stands for Character User Interface. In this article we will be discussing the differences
between these two interfaces and which one has advantages over the other.
User Interface: User Interface comprises of everything the user can use to interact with the
computer. It is basically the means by which the user and computer system can interact using
input and output devices.
GUI: GUI stands for Graphical User Interface. This is a type of user interface where user
interacts with the computer using graphics. Graphics include icons, navigation bars, images etc.
Mouse can be used while using this interface to interact with the graphics. It is a very user
friendly interface and requires no expertise. Eg: Windows has GUI.
CUI: CUI stands for Character User Interface. This is a type of user interface where user
interacts with computer using only keyboard. To perform any action a command is required. CUI
is precursor of GUI and was used in most primitive computers. Most modern computers use GUI
and not CUI. Eg: MS-DOS has CUI.
Deadlock modeling
• The four Coffman conditions can be modeled using directed graphs
• The graph has two kind of nodes: circles for processes and squares for resources
• A directed arc from a resource node (square) to a process node (circle) means that the
resource is being held by the process
• A directed arc from a process to a resource means that the process is currently
requesting that resource
In this case, we use a matrix based algorithm for detecting deadlocks among n processes (P1,…Pn)
E: Existing resource vector (E1,E2….Em) ; we have m different resource
For example: if class 1 is printer then E1=2 means we have 2 printers
A: Available resource vectors
For example: if A1=0, no printers are available, both printers have been assigned
C: Current Allocation matrix
Cij is the number of instances of resource j that are held by process
i R: Request matrix
Rij is the number of instance of resource j that process i wants.
Deadlock Recovery
Traditional operating system such as Windows doesn’t deal with deadlock recovery as it is time
and space consuming process. Real time operating systems use Deadlock recovery.
i. Recovery through preemption
To eliminate deadlocks using resource preemption, we successively preempt some resources
from processes and give these resources to other processes until the deadlock cycle is broken.
Issues to be addresses:
a. Selecting a victim
b. Rollback
c. Starvation
ii. Recovery through rollback
Processes are checkpoint periodically
Checkpoint refers to save the state of a process by writing the state to a file
Checkpoint contains not only the memory image but also the resource state (i.e.
Compiled by : Ravi Nandan Karn
the resource allocated to the process)
The Banker’s algorithm is a resource allocation and deadlock avoidance algorithm developed by
Edsger Dijkstra. Resource allocation state is defined by the number of available and allocated
resources and the maximum demand of the processes. When a process requests an available
resource, system must decide if immediate allocation leaves the system in a safe state.
Algorithm
1) Find a row in the Need matrix which is less than the Available vector. If such a row exists,
then the process represented by that row may complete with those additional resources. If
no such row exists, eventual deadlock is possible.
2) Double check that granting these resources to the process for the chosen row will result in a
safe state. Looking ahead, pretend that that process has acquired all its needed resources,
executed, terminated, and returned resources to the Available vector. Now the value of the
Available vector should be greater than or equal to the value it was previously.
3) Repeat steps 1 and 2 until
a) all the processes have successfully reached pretended termination (this implies that
the initial state was safe); or
b) deadlock is reached (this implies the initial state was unsafe).
Some terminologies
a) Available
It represents the number of available resources of each type.
b) Max
It represents the maximum number of instances of each resource that a process can request.
c) Allocation
It represents the number of resources of each type currently allocated to each process.
d) Need
It indicates the remaining resource needs of each process.
Example 1: State whether the given processes are in deadlock or not. Given that resource
instance is 10.
process Allocated Maximum
A 3 9
B 2 4
C 2 7
Solution,
Calculating need resources, using
Need = Maximum - Allocated we get,
process Allocated Maximum Need
A 3 9 6
B 2 4 2
C 2 7 5
Here currently total allocation = 3+2+2 = 7
So free = total available – current allocation = 10 – 7 = 3
Step 1
With current free resources process B can be executed, since need of B ≤ Free i.e 2 ≤ 3 So B
executes. After execution of B it release the resources allocated by it.
Total free resource becomes, free = current free + Allocation by B = (1+2+2) = 5
Here all the process runs successfully hence they are is safe state and occurs no deadlock.
Safe sequence is: B→C→A
Example 2: State whether the given processes are in deadlock or not. Given that resource
instance is 10.
process Allocated Maximum
A 4 9
B 2 4
C 2 7
Solution,
Calculating need resources, using
Need = Maximum - Allocated we get,
process Allocated Maximum Need
A 4 9 5
B 2 4 2
C 2 7 5
Here currently total allocation = 4+2+2 = 8
So free = total available – current allocation = 10 – 8 = 2
Step 1
With current free resources process B can be executed, since need of B ≤ Free i.e 2 ≤ 2 So B
executes. After execution of B it release the resources allocated by it.
Total free resource becomes, free = current free + Allocation by B = (2+2) = 4
With current free resources none of the processes can be further be executed hence process are
unsafe and occurs deadlock.
Example 1: A system has four process P1, P2, P3 and P4 and three resources R1, R2 and R3
with existing resources E = (15, 9, 5). After allocating resources to all the processes available
resources becomes A = ( 3, 2, 0). State whether the process is safe or not using banker’s
algorithm. If safe, write the safe sequence.
large CPU burst might never get the CPU to complete its execution and
starve.
In Priority Scheduling, a constant stream of high priority processes might
starve one or more lower priority process(es) as the CPU will always be
allocated to the process with highest priority.
RAID
RAID or redundant array of independent disks is a data storage virtualization technology that
combines multiple physical disk drive components into one or more logical units for data
redundancy, performance improvement, or both.
It is a way of storing the same data in different places on multiple hard disks or solid-state drives
to protect data in the case of a drive failure. A RAID system consists of two or more drives
working in parallel. These can be hard discs, but there is a trend to use SSD technology (Solid
State Drives).
RAID combines several independent and relatively small disks into single storage of a large size.
The disks included in the array are called array members. The disks can combine into the array
in different ways, which are known as RAID levels. Each of RAID levels has its own
characteristics of:
o Fault-tolerance is the ability to survive one or several disk failures.
o Performance shows the change in the read and writes speed of the entire array compared
to a single disk.
o The array's capacity is determined by the amount of user data written to the array. The
array capacity depends on the RAID level and does not always match the sum of the
RAID member disks' sizes. To calculate the particular RAID type's capacity and a set of
member disks, you can use a free online RAID calculator.
There is rarely a situation where you should use RAID 0 in a server environment. You can use it
for cache or other purposes where speed is essential, and reliability or data loss does not matter at
all.
2. RAID 1 (mirrored disks)
It duplicates data across two disks in the array, providing full redundancy. Both disks are store
exactly the same data, at the same time, and at all times. Data is not lost as long as one disk
survives. The total capacity of the array equals the capacity of the smallest disk in the array. At
any given instant, the contents of both disks in the array are identical.
RAID 1 is capable of a much more complicated configuration. The point of RAID 1 is primarily
for redundancy. If you completely lose a drive, you can still stay up and running off the other
drive.
RAM DISK
A RAM disk (or RAM drive) is a virtual disk drive created using a portion of a computer's
RAM (Random Access Memory). It mimics the functionality of a traditional storage device like
an HDD or SSD but operates at much higher speeds due to the nature of RAM.
Key Features of a RAM Disk:
1. High Speed: RAM is significantly faster than traditional storage devices, so read and
write operations on a RAM disk are exceptionally fast.
2. Volatility: RAM is volatile, meaning the data stored in a RAM disk is lost when the
computer is powered off or restarted.
3. Configurable Size: You can allocate a portion of your system's RAM to act as a disk, but
this reduces the RAM available for other tasks.
Uses of RAM Disk: