You are on page 1of 15

UNIT 4

Input-Output

Q.1 Write a note on device controllers.


Device controller‐
• A Device controller/adapter is an electronic part of a computer system that
senses the signals going to, and coming from the CPU.
• Many controllers can handle two, four, or even eight identical devices.
• It is placed in between CPU and a Device. It communicates with CPU by
interrupts.
• Tasks of device controller‐
o Provide interface between device and CPU.
o Error checking and correction.

• Each device controller has few registers that are used for communicating
with the CPU.
• By writing into these registers, the operating system can command the
device to deliver data, accept data, switch itself on or off, or otherwise
perform some action.
• By reading from these registers, the operating system can learn what the
device’s state is, whether it is prepared to accept a new command, and so
on.
• Registers are-
o Control register‐ used by the CPU to configure and control the
device.
o Status register‐provide status information to the CPU about the I/O
device.
o Data register‐ used to read data from or send data to the I/O device.

Q. 2 Write a note on device drivers.


• For each type of device, controller is different, so different software is needed to
control each one.
• Each I/O device attached to a computer needs some device-specific code for
controlling it.
• The software that talks to a controller, giving it commands and accepting responses,
is called a device driver.

• Device driver, is generally written by the device’s manufacturer and delivered along
with the device.
• Since each operating system needs its own drivers, device manufacturers commonly
supply drivers for several popular operating systems.
• To activate the controller, the driver gets a command from the operating system.
Then driver translates it into the appropriate values and write into the device
registers.
• It Initializes the device, manages its power requirements.
• Driver accepts read and write requests from the device-independent software.
• Driver then checks if the device is currently in use. If it is, the request will be queued
for later processing. If the device is idle, the hardware status will be examined to see
if the request can be handled now.
Q.3 Write short note on memory mapped IO
• Each control register is assigned an I/O port number. The set of all the I/O
ports form the I/O port space, as shown in fig (a).
• Using special instructions IN and OUT these ports can be accessed.
• There is no way to execute an IN or OUT instruction in C or C++, we require
assembly code for that.
• The second approach is to map all the control registers into the memory
space, as shown in Fig (b).
• Each control register is assigned a unique memory address to which no
memory is assigned. This system is called memory-mapped I/O.
• These memory addresses are reserved, no user can access them. Only
operating system can access these addressees.
• In most systems, the assigned addresses are at the top of the address space.
• With memory-mapped I/O, device control registers are just variables in
memory and can be addressed in C the same way as any other variables.
• Thus with memory-mapped I/O, an I/O device driver can be written entirely
in C. Without memory-mapped I/O, some assembly code is needed.
Q.4 Write a short note on direct memory access(DMA).
• DMA permits data transfer directly between I/O device and main memory,
without involvement by the CPU.
• The transfer of data is first initiated by the CPU.
• During the data transfer, the CPU performs other tasks. When the data
transfer is completed, the CPU receives an interrupt request from the DMA
controller.
• The operating system can use only DMA if the hardware has a DMA
controller.
• Consider a data transferred from disk drive to main memory with DMA as
shown in fig. below-
Steps-
1. CPU initiates data transfer by programming DMA controller’s registers.
Control- transfer data from disk to main memory
Count- n Number of bytes to transfer
Address- Starting address location in main memory
2. DMA controller then requests disk controller to add data byte to be
transferred from the disk to its buffer.
3. The disk controller then sends the 1 data byte from buffer to the specified
memory location in main memory.
4. When the write is complete, the disk controller sends an acknowledgement
signal to the DMA controller. DMA controller then increments the address
value by 1 (next empty location to write the data) and decrements the byte
count value by one (1 byte is sent out of n bytes). Then steps 2,3,4 are
repeated till the count reaches 0.
5. When count=0, the DMA controller interrupts the CPU to let it know that
the transfer is now complete.
Q.5 What is interrupt? Explain its types.
• Data transfer between CPU and I/O devices is done through interrupts.
• I/O device which wants to communicate with CPU, gives a signal on
interrupt request line.
• When CPU detects an interrupt, suspends its current operation, saves its
current state (PC) on stack and then jumps to interrupt handler routine.
• Then Interrupt handler finds out- source and reason of interrupt and carries
out required steps.
• After this, CPU returns from interrupt handler routine to the previous
executing state before the interrupt.
Types-
1. Precise Interrupt-
• An interrupt that leaves the machine in a well-defined state is called a
precise interrupt.
• Such an interrupt has four properties:
1. The PC (Program Counter) is saved in a stack.
2. All instructions before the arrival of interrupt are completed.
3. No instruction beyond the interrupt has finished.
4. The execution state of the instruction pointed by the PC is known.

2. Imprecise Interrupt-
• An interrupt that does not meet these requirements is called an imprecise
interrupt.
• In pipelining, several instructions are executed together. So all instructions
are in various stages of execution. (when 3rd instruction is in fetch stage, 2nd
is in decode stage and 1st one is in execution stage)
• So many instructions may have been partially executed, with different
instructions being more or less complete.
• When the interrupt occurs, the value of the program counter may not
reflect the correct boundary between executed instructions and non-
executed instructions.
Q.6 What are the goals of I/O software?
Device Independence-
• It should be possible to write programs that can access any I/O device
without knowing device in advance.
• For example, a program that reads a file as input should be able to read a file
on a hard disk, a DVD, or on a USB stick without having to be modified for
each different device.
Uniform Naming-
• The 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-
• In general, errors should be handled as close to the hardware as possible. If
the 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. If driver is unable to
handle the error, then only upper layers should be involved.
• In many cases, error recovery can be done transparently at a low level
without the upper levels even knowing about the error.
Buffering-
• Data that come from a device or going to the device cannot be stored
directly in their final destination. So data must be put in input/output buffer.
• Buffering involves considerable copying and often has a major impact on I/O
performance.
Sharable vs. dedicated devices(Unshared)-
• Disks can be used by many users at a time but not printer or tape drivers.
Q.7 Explain I/O software layers.


1. Hardware‐ CPU, memory, I/O devices

2. Interrupt Handler‐
• I/O device which wants to communicate with CPU, gives a signal on interrupt
request line.
• When CPU detects an interrupt, suspends its current operation, saves its current
state on stack and then jumps to interrupt handler routine.
• Interrupt handler finds out- source and reason of interrupt and carries out
required steps.
• After this, CPU returns from interrupt handler routine to the previous executing
state before the interrupt.
• Modern OS supports following interrupt handler features‐
Multilevel Interrupts‐ Priority assigning to interrupts. Execute interrupt if it is of
higher priority. Postpone the interrupt handling if critical processing is going on.
Maskable Interrupt‐ Interrupt requested which can be avoided/ disabled.
Unmaskable Interrupt‐ Interrupt requested which cannot be avoided/disabled.
Vectored Interrupts‐ Fixed memory address of interrupt handler routine.
Non‐vectored Interrupts‐ Memory address of interrupt handler routine is not
fixed. Address is specified by user/OS.

3. Device Drivers‐
• Each I/O device attached to a computer needs some device-specific code for
controlling it.
• The software that talks to a controller, giving it commands and accepting
responses, is called a device driver.
• To activate the controller, the driver gets a command from the operating
system. Then driver translates it into the appropriate values and write into the
device registers.
• It Initializes the device, manages its power requirements.
• Driver accepts read and write requests from the device-independent software.
• Driver then checks if the device is currently in use. If it is, the request will be
queued for later processing. If the device is idle, the hardware status will be
examined to see if the request can be handled now.

4. Device‐Independent I/O software‐


Following are the functions handled by Device‐Independent I/O software layer‐
• Uniform interfacing for device drivers.
• Buffering‐ Input and output buffers are used to store the data temporarily.
• Error reporting.
• Allocating and releasing dedicated devices‐ Some devices are not sharable and
can be used by only single process at a time e.g. printer.
• Provide device independent block‐size.

Q.8 Explain RAID in details with its different levels (any four)
RAID‐ Redundant Array of Independent Disks
Multiple are disks are connected together to provide large storage capacity, faster
access to data and improved reliability. RAID should look like a single large disk to
the operating
 system but have better performance and better reliability.
In RAIDs,
 the data are distributed over the drives, to allow parallel operation.
There are seven standard configurations - RAID level 0 to RAID level 6.
RAID 0 ‐ Striping
• It splits data among disks as shown in figure (a).
• Provides fast read-write performance and efficiency.
• There is no fail over support with this configuration.
RAID
 1 – Mirroring
• It duplicates all the disks, so there are four primary disks and four backup
disks. It is ‘data mirroring’.
• Two copies of the data are held on two physical disks, and the data is always
identical.
• Twice as many disks are required to store the same data.
• Fault tolerance is excellent: if a drive crashes, the copy is simply used
instead.

  
 RAID
 2‐
• Unlike levels 0 and 1, which work with strips of sectors, RAID level 2 works
bit wise.
• File is divided into nibbles. So 4 disks to store 4 bits of nibble. Additional 3
disks are used to store Parity bits P3, P2, P1 calculated by Hamming code to
form a 7‐bit word.
• 7 disks are used (Nibble + P3P2P1) which provides error correcting
mechanism.

 RAID
 3‐
 • RAID level 3 is a simplified version of RAID level 2.
• Here a single parity bit is computed for each data word and written to a
parity drive.
• Single parity bit gives only error detection, not error correction.

RAID 4,5 and 6‐


• Work with strips again.
• RAID level 4 is like RAID level 0, with a strip‐for‐strip parity written onto an
extra drive. If a drive crashes, the lost bytes can be recomputed from the
parity drive.
• To reduce the heavy load on the parity drive, in RAID level 5 parity bits are
distributed uniformly over all the drives.
• Raid level 6 is similar to RAID level 5, except that an additional parity block is
used. In other words, the data is striped across the disks with two parity
blocks in‐ stead of one.

Q.9 Write a short note on Touch Screen.


• A touch device is not necessarily a screen.
• Touch devices fall into two categories: opaque and transparent.
• A typical opaque touch device is the touchpad on a notebook computer.
• An example of a transparent device is the touch screen on a smartphone or
tablet.
• Most modern touch screens are either resistive or capacitive.
• Resistive screens have a flexible plastic surface on top with more scratch
resistant .
• Capacitive Screens have two hard surfaces, typically glass, each coated with
• ITO. A typical configuration is to have ITO added to each surface in parallel
lines,
Q.10 Explain power management in detail.
Display‐
• Consumes more power and OS turns off the display when no activity is
• observed for some time.
• Display wakes up when mouse is moved and key is pressed.
Hard Disk‐
• Takes high energy for spinning at high speed. Hibernating state‐ spinning is
• stopped.
• Disk energy can be saved by having disk cache
CPU‐
• If CPU is idle or waiting for I/O, CPU goes to sleep state.
• CPU power usage is related to CPU voltage and speed.
• CPU power is proportional to square of voltage.
• Reducing voltage saves energy, but reduces speed of operation.
Memory‐
• Sleep state‐ Cache is turned off. Reloading of cache form main memory is
• quick and dynamic.
• Hibernate‐ write contents of main memory to disk and then turn off the
main
• memory.
Wireless communication‐
• Radio transmitter and receiver consume more power.
• Radio receiver is put on sleep when idle.
• Incoming messages are dumped on disk.
Thermal Management‐
• High speed CPU‐ Heat is released
• Internal electric fan, sleep mode, power saving mode
Battery management‐ Battery saver mode.

1 List two types of resources. Explain with suitable example


A preemptable : resource is one that can be taken away from the process owning it
with no ill effects.
Eg:
• Memory is an example of a preemptable resource.
• for example, a system with 1 GB of user memory, one printer, and two 1-GB
processes that each want to print something.
• Process A requests and gets the printer, then starts to compute the values to
print. Before it has finished the computation, it exceeds its time quantum and is
swapped out to disk.
• Process B now runs and tries, unsuccessfully as it turns out, to acquire the
printer. Potenially, we now have a deadlock situation, because A has the printer
• and B has the memory, and neither one can proceed without the resource.
• Fortunately, it is possible to preempt (take away) the memory from B by
swapping it out and swapping A in. Now A can run, do its printing, and then
release
• the printer. No deadlock occurs.

A nonpreemptable resource: is one that cannot be taken away from its current owner
without potentially causing failure.
Eg
• If a process has begun to burn a Blu-ray, suddenly taking the Blu-ray recorder
away from it and giving it to another process will result in a garbled Blu-ray.
• Blu-ray recorders are not preemptable at an arbitrary moment.
2 Define Deadlock and explain the same with an example
1. Deadlock : A set of processes is deadlocked if each process in the set is waiting
for an event that only another process in the set can cause.
Example :
2. Two processes each want to record a scanned document on a Blu-ray disc.
3. Process A requests permission to use the scanner and is granted it.
4. Process B is programmed differently and requests the Blu-ray recorder first and
is also granted it.
5. Now A asks for the Blu-ray recorder, but the request is suspended until B
releases it. Unfortunately, instead of releasing the Bluray recorder, B asks for the
scanner.
6. At this point both processes are blocked and will remain so forever.
7. This situation is called a deadlock.
8. Each member of the set of deadlocked processes is waiting for a resource that is
owned by a deadlocked process.
9. None of the processes can run, none of them can release any resources, and
none of them can be awakened.
10. This result holds for any kind of resource, including both hardware and software.
11. This kind of deadlock is called a resource deadlock.
3 Define deadlock and Explain the conditions for deadlock
1. Deadlock : A set of processes is deadlocked if each process in the set is waiting
for an event that only another process in the set can cause.
2. Mutual exclusion condition.
Each resource is either currently assigned to exactly one process or is available.

3. Hold-and-wait condition. Processes currently holding resources that were


granted earlier can request new resources.

4. No-preemption condition. Resources previously granted cannot be forcibly


taken away from a process. They must be explicitly released by the process
holding them.
5. Circular wait condition. There must be a circular list of two or more processes,
each of which is waiting for a resource held by the next member of the chain.

4 Deadlock Detection using one resource of each type


a) Detection with one resource of each type‐
Consider a system with one resource of each type.
E.g. System with 1 printer, 1 scanner, 1 plotter and 1 CD recorder.

Steps to detect deadlock-


1. Construct a resource graph.
2. If this graph contains one or more cycles, a deadlock exists.
3. If no cycle exists, the system is not deadlocked.

Consider following example-


Process A holds R and wants S.
Process B holds nothing but wants T.
Process C holds nothing but wants S.
Process D holds U and wants S and T.
Process E holds T and wants V.
Process F holds W and wants S.
Process G holds V and wants U.
We can conclude that processes D,E and G are deadlocked.
Processes A,C and F are not deadlocked as S can be allotted to any one of them

5 Deadlock recovery
Recovery through Preemption :
• Temporarily take a resource away from its current owner and give it to another
process. Manual intervention may be required, especially in batch-processing
operating systems running on mainframes.
• The ability to take a resource away from a process, have another process use it,
and then give it back without the process noticing it i
• Its highly dependent on the nature of the resource.
• Choosing the process to suspend depends largely on which ones have resources
that can easily be taken back
• For example, to take a laser printer away from its owner, the operator can
collect all the sheets already printed and put them in a pile. Then the process
can be suspended (marked as not runnable). At this point the printer can be
assigned to another process

Recovery through Rollback


• Process can be arranged to have processes checkpointed periodically.
• Checkpointing a process means that its state is written to a file so that it can be
restarted later
• Check which resources are needed to recover a deadlock.
• A process that has a needed resource is rolled back in time to the point before it
has taken that resource.
• In effect, the process is reset to an earlier moment when it did not have the
resource.
Recovery through killing processes:
• The crudest but simplest way to break a deadlock is to kill one or more
processes.
• Kill one process in the cycle.
• If this does not help, it can be repeated until the cycle is broken.
• In this approach, the process to be killed is carefully chosen
• because it is holding resources that some process in the cycle needs

6 Deadlock Prevention
1.Attacking The Mutual Exclusion Condition‐
• Make resources sharable.
• If no resource were ever assigned exclusively to a single process, we would
never hav e deadlocks.
• E.g read only file can be accessed by many users at a time.
• But printer cannot be shared, because its dedicated device.
• The only process that actually requests the physical printer is the printer
daemon. Since the daemon never requests any other resources, we can
eliminate deadlock for the printer
2.Attacking The Hold and Wait Condition‐
• To avoid hold and wait, allocate all the resources needed for a process before
execution.
• If all resources are available, process will execute.
• If one or more resources are busy, then process will go into waiting state.
• Problem ‐ many processes do not know how many resources they will need until
they have started running.
• Resources will not be used optimally with this approach.

3.Attacking The No Preemption Condition‐


• Resources assigned to the process can be taken away to prevent deadlock
situation.
• If a process is holding some resources and requests another resource that
cannot be immediately allocated to it (that is, the process must wait), then all
resources the process is
• currently holding are preempted.
• If a process has been assigned the printer and is in the middle of printing its
output, forcibly taking away the printer because a needed plotter is not
available.
• some resources can be virtualized to avoid this situation.

4.Attacking The Circular Wait Condition‐


• One way is to have a rule that a process can have only one resource at any
moment. If it needs a second one, it must release the first one.
• Second way is assign a global number to each resource type. Each process
request the resources in increasing order of assigned numbers.
• It should release any resources with number less than the requested value.
7 Explain the term Two Phase locking
Two-Phase locking-
• If multiple processes try to access and update the database at the same time,
there is a possibility of deadlock.
• To avoid that, OS uses the concept of LOCKING.
• The process which wants to update the database, first it locks all the records so
that no other process can access it at the same time. Once the updating is done,
process unlocks the records.
• The process is carried out in two phases-
• Phase 1- Process locks all the records it needs, one at a time.
• Phase 2- Process updates the locked records and then release the lock one by
one
• In real time system, it is not acceptable to just terminate a process partway
through because a resource is not available and start all over again

8 Communication Deadlock
• A set of processes is communication-deadlocked if each process in the set is
waiting to communicate with another process in the set and no process ever
initiates any further communication until it receives the communication for
which it is waiting
• Communication deadlocks cannot be prevented by ordering the resources since
there are no resources) or avoided by careful scheduling
• The technique employed for dealing with communication deadlock is Timeout
• In most network communication systems, whenever a message is sent to which
a reply is expected, a timer is started.
• If the timer goes off before the reply arrives, the sender of the message assumes
that the message has been lost and sends it again (and again and again if
needed).
• In this way, the deadlock is broken
9 Livelock
• Livelock is a special case of resource starvation where two processes follow an
algorithm for resolving a deadlock that results in a cycle of different locked
states because each process is attempting the same strategy to avoid the lock.
• In some situations, a process tries to be polite by giving up the locks it already
• acquired whenever it notices that it cannot obtain the next lock it needs.
• Then it waits a millisecond, say, and tries again.
• In principle, this is good and should help to detect and avoid deadlock. However,
if the other process does the same thing at exactly the same time, they will be in
the situation of two people trying to pass each other on the street when both of
them politely step aside, and yet no progress is possible, because they keep
stepping the same way at the same time.
10 Starvation
In computer system, Multiple processes request for the same resource at same time. So
some algorithm is used to decide which process must get the resource.
But this algorithm may lead starvation of some processes- i.e. some process will never
get the resource even though it is not deadlocked.
Starvation is closely related to deadlock and livelock
Starvation can be avoided by using a first-come, first-served resource allocation policy.
With this approach, the process waiting the longest gets served next
Example- Suppose multiple processes have requested to access the Printer.
Algorithm used- print smallest file first
If one process has a huge file, every time when printer if free it will choose a process
with shortest file.
The process with huge file will never get printer and will starve to death. (it wont be
blocked but postponed every time)

You might also like