Professional Documents
Culture Documents
Input-Output
• 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.
• 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.
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.
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.
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
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.
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)