1 Operating System
1 Operating System
• Whatever used as an interface between the user and the core machine is OS.
• E.g. steering of car, switch of the fan etc., Buttons over electronic devices.
• There is no exact or precise definition for OS but we can say, “A program or System
software”
1
• Resource Manager/Allocator - Manage system resources in an unbiased fashion both h/w
(mainly CPU time, memory, system buses) & s/w (access, authorization, semaphores) and
provide functionality to application programs.
• OS controls and coordinates the use of resources among various application programs.
• OS provides platform on which other application programs can be installed, provides the
environment within which programs are executed.
• For personal computers - Microsoft windows (82.7%), Mac (13.23), Linux (1.57)
2
• Computer hardware – CPU, memory units, i/o devices, system bus, registers etc. provides the
basic computing resources.
• OS - Control and coordinates the use of the hardware among the various applicationsprograms.
• System and Applications programs - Defines the way in which these resources are used tosolve the
computing problems of the user.
• User
• Goals are the ultimate destination, but we follow functions to implement goals.
• Goals
• Process management
• Memory management
• File management
• Network management
• Early computers were not interactive device, there user use to prepare a job which consist three parts
• Program
• Control information
• Input data
• Only one job is given input at a time as there was no memory, computer will take the input then
process it and then generate output.
3
• Common input/output device were punch card or tape drives.
• So these devices were very slow, and processor remain ideal most of the time.
• In some system grouping is done by the operator while in some systems it is performed by the 'Batch
Monitor' resided in the low end of main memory)
• Then jobs (as a deck of punched cards) are bundled into batches with similar requirement.
• Then the submitted jobs were 'grouped as FORTRAN jobs, COBOL jobs etc.
4
Advantage
• The batched jobs were executed automatically one after another
saving its time by performing the activities (like loading of
compiler) only for once. It resulted in improved system utilization
due to reduced turnaround time.
Disadvantage
• Memory limitation – memory was very limited, because of which
interactive process or multiprogramming was not possible
Spooling
• ("Spool" is technically an acronym for simultaneous peripheral operations online.)
• In a computer system peripheral equipment, such as printers and punch card readers etc, are very slow relative
to the performance of the rest of the system. Spooling is useful because devices access data at different rates.
• Spooling is a process in which data is temporarily held to be used and executed by a device, program or the
system. Data is sent to and stored in memory or other volatile storage until the program or computer requests it
for execution.
• Generally, the spool is maintained on the computer’s physical memory, buffers or the I/O device-specific
interrupts.
• The most common implementation of spooling can be found in typical input/output devices such as the
keyboard, mouse and printer. For example, in printer spooling, the documents/files that are sent to the printer
are first stored in the memory. Once the printer is ready, it fetches the data and prints it.
• A spooler works by intercepting the information going to the printer, parking it temporarily on disk or in memory.
The computer can send the document information to the spooler at full speed, then immediately return control
of the screen to you.
• The spooler, meanwhile, hangs onto the information and feeds it to the printer at the slow speed the printer
needs to get it. So if your computer can spool, you can work while a document is being printed.
5
• Even experienced a situation when suddenly for some seconds your mouse or keyboard stops working?
Meanwhile, we usually click again and again here and there on the screen to check if its working or not.
When it actually starts working, what and wherever we pressed during its hang state gets executed very
fast because all the instructions got stored in the respective device’s spool.
• Spooling is capable of overlapping I/O operation for one job with processor operations for another job.
i.e. multiple processes can write documents to a print queue without waiting and resume with their
work.
(a) The terminal used to enter the input data for the C program being executed
• Eventually, the first job finishes waiting and gets the CPU back. So, conclusion is as long as at least one job
needs to execute, the CPU is never idle.
Advantage
• In most of the applications multiple tasks are running and multiprogramming systems better handle
these type of applications
Disadvantage
(a) More than one program may be loaded into main memory at the same time.
(b) If a program waits for certain event another program is immediately scheduled.
(A) (a) only (B) (a) and (b) only (C) (a) and (c) only (D) (a), (b) and (c)
• For multitasking to take place, firstly there should be multiprogramming i.e. presence of multiple
programs ready for execution. And secondly the concept of time sharing.
• Time sharing (or multitasking) is a logical extension of multiprogramming, it allows many users to share
the computer simultaneously. the CPU executes multiple jobs (May belong to different user) by
switching among them, but the switches occur so frequently that, each user is given the impression that
the entire computer system is dedicated to his/her use, even though it is being shared among many
users.
7
Q If you want to execute more than one program at a time, the systems software that are used
(A) word processing (B) virtual memory (C) compiling (D) multitasking
• Multiprocessor Operating System refers to the use of two or more central processing units (CPU) within a
single computer system. These multiple CPUs are in a close communication sharing the computer bus, memory
and other peripheral devices. These systems are referred as tightly coupled systems
• So in multiprocessing, multiple concurrent processes each can run on a separate CPU, here we achieve a true
parallel execution of processes. Parallel processing is the ability of the CPU to simultaneously process incoming
jobs.
• Multiprocessing becomes most important in computer system, where the complexity of the job is more,
and CPU divides and conquers the jobs. Generally, the parallel processing is used in the fields like
artificial intelligence and expert system, image processing, weather forecasting etc.
• Most of the multi-processing PC motherboards utilize symmetric multiprocessing. Here each processor runs
an identical copy of operating system and these copies communicate with each other.
• SMP means that all processors are peers; no boss–worker relationship exists between processors. Windows,
Mac OSX and Linux.
• Asymmetric - This scheme defines a master-slave relationship, where one processor behaves as a
master and control other processor which behaves as slaves.
• For e.g. It may require that only one particular CPU respond to all hardware interrupts, whereas all
other work in the system may be distributed equally among CPUs;
• Or execution of kernel-mode code may be restricted to only one particular CPU, whereas user-mode
code may be executed in any combination of processors.
• Multiprocessing systems are often easier to design if such restrictions are imposed, but they tend to be
less efficient than systems in which all CPUs are utilized.
8
• A boss processor controls the system, other processors either look to the boss for instruction or have
predefined tasks. This scheme defines a boss–worker relationship. The boss processor schedules and
allocates work to the worker processors.
Advantage of multiprocessing
• Increased Throughput - By increasing the number of processors, we expect to get more work done in
less time. The speed-up ratio with N processors is not N, however; rather, it is less than N. When
multiple processors cooperate on a task, a certain amount of overhead is incurred in keeping all the
parts working correctly. This overhead, plus contention for shared resources, lowers the expected gain
from additional processors. These types of systems are used when very high speed is required to
process a large volume of data.
• Economy of Scale - Multiprocessor systems can cost less than equivalent multiple single-processor
systems, because they can share peripherals, mass storage, and power supplies. If several programs
operate on the same set of data, it is cheaper to store those data on one disk and to have all the
processors share them than to have many computers with local disks and many copies of the data.
• Increased Reliability (fault tolerance) - If functions can be distributed properly among several
processors, then the failure of one processor will not halt the system, only slow it down. If we have
ten processors and one fail, then each of the remaining nine processors can pick up a share of the
work of the failed processor. Thus, the entire system runs only 10 percent slower, rather than failing
altogether
Disadvantages
9
Real time Operating system
• A real time system is a time bound system which has well defined fixed time constraints. Processing must be done within the
defined constraints or the system will fail. Very fast and quick respondent systems.
• RTOS intended to serve real-time applications that process data as it comes in, typically without buffer delays. Are used in an
environment where a large number of events (generally external) must be accepted and processed in a short time.
• For example, a measurement from a petroleum refinery indicating that temperature is getting too high and might demand for
immediate attention to avoid an explosion. Airlines reservation system, Air traffic control system, Systems that provide
immediate updating, Systems that provide up to the minute information on stock prices, Defense application systems like as
RADAR.
• Valued more for how quickly or how predictably it can respond than for the amount of work it can perform
in a given period of time.
• Little swapping of programs between primary and secondary memory. Most of the time, processes remain
in primary memory in order to provide quick response, therefore, memory management in real time system is
less demanding compared to other systems.
Hard real-time operating system - This is also a type of OS and it is predicted by a deadline. The predicted
deadlines will react at a time t = 0. Some examples of this operating system are air bag control in cars, anti-lock
brake, and engine control system etc.
10
Distributed OS
• A distributed operating system is a software over a collection of independent, networked, communicating, loosely coupled
nodes and physically separate computational nodes.
• These modes do not share memory or a clock. Instead, each node has its own local memory. The nodes communicate with
one another through various networks, such as high-speed buses and the Internet.
• They handle jobs which are serviced by multiple CPUs. Each individual node holds a specific software subset of the global
aggregate operating system.
• Each subset is a composite of two distinct service provisioners. The first is a ubiquitous minimal kernel,
or microkernel, that directly controls that node's hardware.
• Second is a higher-level collection of system management components that coordinate the node's
individual and collaborative activities. These components abstract microkernel functions and support
user applications.
• The microkernel and the management components collection work together. They support the system's
goal of integrating multiple resources and processing functionality into an efficient and stable system.
• This seamless integration of individual nodes into a global system is referred to as transparency, or
single system image; describing the illusion provided to users of the global system's appearance as a
single computational entity.
11
• There are four major reasons for building distributed systems: resource sharing, computation speedup,
reliability, and communication.
List - I List - II
(1) Spooling (i) Allows several jobs in memory to improve CPU utilization
(2) Multiprogramming (ii)Access to shared resources among geographically dispersed
computers in a transparent way
(3)Time sharing (iii) Overlapping I/O and computations
(4)Distributed computing (iv)Allows many users to share a computer simultaneously by
switching processor frequently
• Command Interpreters - Some operating systems include the command interpreter in the
kernel.
• Others, such as Windows and UNIX, treat the command interpreter as a special program that
is running when ajob is initiated or when a user first logs on (on interactive systems).
• On systems with multiple command interpreters to choose from, the interpreters are known
as shells. For example, on UNIX and Linux systems, a user may choose among several different
shells, including the Bourne shell, C shell, Bourne-Again shell, Korn shell, and others.
12
• Graphical User Interfaces - A second strategy for interfacing with the operating system is through a user- friendly
graphical user interface, or GUI.
• The user moves the mouse to position its pointer on images, or icons, on the screen (the desktop) that represent
programs, files, directories, and system functions.
• Depending on the mouse pointer’s location, clicking a button on the mouse can invoke a program, select a file or
directory—known as a folder —or pull down a menu that contains commands.
• Because a mouse is impractical for most mobile systems, smartphones and handheld tablet computers
typically use a touchscreen interface.
• Simple Structure - Many operating systems do not have well-defined structures. Frequently, such
systems started as small, simple, and limited systems and then grew beyond their original scope. MS-
DOS is an example of such a system.
13
Micro-Kernel approach
• In the mid-1980s, researchers at Carnegie Mellon University developed an operating system called
Mach that modularized the kernel using the microkernel approach.
• This method structures the operating system by removing all nonessential components from the kernel
and implementing them as system and user-level programs. The result is a smaller kernel.
• One benefit of the microkernel approach is that it makes extending the operating system easier. All
new services are added to user space and consequently do not require modification of the kernel.
• When the kernel does have to be modified, the changes tend to be fewer, because the microkernel is a
smaller kernel.
• The MINIX 3 microkernel, for example, has only approximately 12,000 lines of code.
Mode bit
• In order to ensure the proper execution of the operating system, we must be able to distinguish between the
execution of operating-system code and user- defined code.
14
• At the very least, we need two separate modes of operation: user mode and kernel mode (also called
supervisor mode, system mode, or privileged mode). A bit, called the mode bit, is added to the hardware of
the computer to indicate the current mode: kernel (0) or user (1).
• When the computer system is executing on behalf of a user application, the system is in user mode. However,
when a user application requests a service from the operating system (via a system call), the system must
transition from user to kernel mode to fulfill the request.
• At system boot time, the hardware starts in kernel mode. The operating system is then loaded and starts user
applications in user mode. Whenever a trap or interrupt occurs, the hardware switches from user mode to
kernel mode (that is, changes the state of the mode bit to 0).
• The hardware allows privileged instructions to be executed only in kernel mode. If an attempt is made to
execute a privileged instruction in user mode, the hardware does not execute the instruction but rather
treats it as illegal and traps it to the operating system.
• The instruction to switch to kernel mode is an example of a privileged instruction. Some other examples
include I/O control, timer management, and interrupt management.
15
System call
• System calls provide the means for a user program to ask
the operating system to perform tasks reserved for the
operating system on the user program’s behalf.
• Three of the most common APIs available to application programmers are the
• Windows API for Windows systems
• POSIX API for POSIX-based systems (which include virtually all versions of UNIX, Linux, and Mac
OS X)
• The Java API for programs that run on the Java virtual machine.
The caller need know nothing about how the system call is implemented or what it does during execution.
Rather, the caller need only obey the API and understand what the operating system will do as a result of
the execution of that system call
16
• Types of System Calls - System calls can be grouped roughly into six major categories: process control,
file manipulation, device manipulation, information maintenance, communications, and protection.
• Process control
o end, abort
o load, execute
o create process, terminate process
o get process attributes, set process attributes
o wait for time
o wait event, signal event
o allocate and free memory
• File management
o create file, delete file
o open, close
o read, write, reposition
o get file attributes, set file attributes.
• Device management
o request device, release device
o read, write, reposition
o get device attributes, set device attributes
o logically attach or detach devices
• Information maintenance
17
o get time or date, set time or date
o get system data, set system data
o get process, file, or device attributes
o set process, file, or device attributes
• Communications
o create, delete communication connection
o send, receive messages transfer status information
Process
• In general, a process is a program in execution.
• A Program is not a Process by default. A program is a passive entity, i.e. a file containing a list of instructions
stored on disk (secondary memory) (often called an executable file).
• A program becomes a Process when an executable file is loaded into main memory and when it’s PCB is
created.
• A process on the other hand is an Active Entity, which require resources like main memory, CPU time, registers,
system buses etc.
• Even if two processes may be associated with same program, they will be considered as two
separate execution sequences and are totally different process.
• For instance, if a user has invoked many copies of web browser program, each copy will be
treated as separate process. even though the text section is same but the data, heap and
stack sections can vary.
18
Process Control Block (PCB)
• Each process is represented in the operating system by a process control
block (PCB) — also called a task control block.
• PCB simply serves as the repository for any information that may vary
from process to process. It contains many pieces of information
associated with a specific process, including these:
• Process state: The state may be new, ready, running, waiting, halted,
and so on.
19
Process States
• A Process changes states as it executes. The state of a process is defined in parts by the
current activity of that process. A process may be in one of the following states:
• New: The process is being created.
• Waiting (Blocked): The process is waiting for some event to occur (such as an I/O
completion or reception of a signal).
20
Q What is the minimum and maximum number of processes that can be
in the ready, run, and blocked states, if total number of process is n?
Min Max
Ready
Run
Block
Min Max
Ready
Run
Block
Q There are three processes in the ready queue. When the currently
running process requests for I/O how many process switches take place?
(ISRO 2011)
a) 1 b) 2 c) 3 d) 4
21
Q What is the ready state of a process?
c) when process is unable to run until some task has been completed
a) is executable
b) is running
(b) If a program waits for certain events such as I/O, another program is
immediately scheduled for execution.
22
Q The process state transition diagram in below figure is representative of Untitled
Diagram (GATE-1996) (1 Marks)
a) a batch operating system
23
Q In the following process state transition diagram for a uniprocessor system, assume that there are always some processes in
the ready state:
Now consider the following statements:
I) If a process makes a transition D, it would result in another process making transition A immediately.
II) A process P2 in blocked state can make transition E while another process P1 is in running state.
III) The OS uses preemptive scheduling.
IV) The OS uses non-preemptive scheduling.
Which of the above statements are TRUE? (GATE-2009) (2 Marks)
a) I and II b) I and III c) II and III d) II and IV
Q A computer handles several interrupt sources of which the following are relevant for this
question.
• Interrupt from CPU temperature sensor (raises interrupt if CPU temperature is too high)
• Interrupt from Mouse (raises interrupt if the mouse is moved or a button is pressed)
• Interrupt from Hard Disk (raises interrupt when a disk read is completed)
Which one of these will be handled at the HIGHEST priority? (GATE - 2011) (1 Marks)
(C) Interrupt from Keyboard (D) Interrupt from CPU temperature sensor
24
a b c d
a) iii i ii iv
b) iv i iii ii
c) iv iii i ii
d) Iii iii ii i
• The objective of time sharing is to switch the CPU among processes so frequently that
users can interact with each program while it is running.
• To meet these objectives, the process scheduler selects an available process (possibly
from a set of several available processes) for execution on the CPU. Note: For a single-
processor system, there will never be more than one running process.
• The system also includes other queues. When a process is allocated the CPU, it executes for a
while and eventually quits, is interrupted, or waits for the occurrence of a particular event,
such as the completion of an I/O request.
25
• Suppose the process makes an I/O request to a shared device, such as a disk. Since there are
many processes in the system, the disk may be busy with the I/O request of some other
process. The process therefore may have to wait for the disk. The list of processes waiting for
a particular I/O device is called a device queue. Each device has its own device queue.
• Each rectangular box represents a queue. Two types of queues are present: the ready queue
and a set of device queues. The circles represent the resources that serve the queues. A new
process is initially put in the ready queue. It waits there until it is selected for execution, or
dispatched.
• Once the process is allocated the CPU and is executing, one of several events could occur:
• The process could issue an I/O request and then be placed in an I/O queue.
• The process could create a new child process and wait for the child’s termination.
• Time slice expired, the process could be removed forcibly from the CPU, as a result of an
interrupt, and be put back in the ready queue.
• In the first two cases the process eventually switches from the waiting state to the ready state
and is then put back in the ready queue.
• A process continues this cycle until it terminates, at which time it is removed from all queues
and has its PCB and resources deallocated.
26
Schedulers
• Schedulers: A process migrates among the various scheduling queues throughout its lifetime.
The operating system must select, for scheduling purposes, processes from these queues in
some fashion. The selection process is carried out by the appropriate scheduler.
• Types of Schedulers
• Long Term Schedulers (LTS)/Spooler: In multiprogramming os, more processes are
submitted than can be executed immediately. These processes are spooled to a mass-
storage device (typically a disk), where they are kept for later execution. The long-term
scheduler, or job scheduler, selects processes from this pool and loads them into memory
for execution.
• Short Term Scheduler (STS): The short-term scheduler, or CPU scheduler, selects from
among the processes that are ready to execute and allocates the CPU to one of them.
• Difference between LTS and STS - The primary distinction between these two schedulers lies
in frequency of execution.
• The short-term scheduler must select a new process for the CPU frequently. A process may
execute for only a few milliseconds before waiting for an I/O request. Often, the short-term
scheduler executes at least once every 100 milliseconds.
• Because of the short time between executions, the short-term scheduler must be fast. The
long-term scheduler on the other hand executes much less frequently; minutes may separate
the creation of one new process and the next.
• The long-term scheduler controls the degree of multiprogramming as it is responsible for bringing in the
processes to main memory.
• If the degree of multiprogramming is stable, then the average rate of process creation must be equal to the
average departure rate of processes leaving the system. So, this means the long-term scheduler may need to
be invoked only when a process leaves the system. Because of the longer interval between executions, the
long-term scheduler can afford to take more time to decide which process should be selected for execution.
Medium-term scheduler: The key idea behind a medium-term scheduler is that sometimes it can be
advantageous to remove a process from memory (and from active contention for the CPU) and thus
reduce the degree of multiprogramming.
Later, the process can be reintroduced into memory, and its execution can be continued where it left
off. This scheme is called swapping.
27
The process is swapped out, and is later swapped in, by the medium-term scheduler. Swapping may
be necessary to improve the process mix or because a change in memory requirements has
overcommitted available memory, requiring memory to be freed up.
• Dispatcher - The dispatcher is the module that gives control of the CPU to the process
selected by the short-term scheduler.
• This function involves the following: Switching context, switching to user mode, jumping to
the proper location in the user program to restart that program.
• The dispatcher should be as fast as possible, since it is invoked during every process switch.
The time it takes for the dispatcher to stop one process and start another running is known as
the dispatch latency.
c) Driver d) Spooler
Q Which module gives control of the CPU to the process selected by the short -
term scheduler? (NET-NOV-2017)
a) Dispatcher b) Interrupt
c) Scheduler d) Threading
28
CPU Bound and I/O Bound Processes
• A process execution consists of a cycle of CPU execution or wait and i/o execution or wait.
• Process execution begin with the CPU burst that may be followed by a i/o burst, then another
CPU and i/o burst and so on.
• Eventually in the last will end up on CPU burst. So, process keep switching between the CPU
and i/o during execution.
• I/O Bound Processes: An I/O-bound process is one that spends more of its time doing I/O than
it spends doing computations.
• CPU Bound Processes: A CPU-bound process, generates I/O requests infrequently, using more
of its time doing computations.
• It is important that the long-term scheduler select a good process mix of I/O-bound and CPU-
bound processes.
• If all processes are I/O bound, the ready queue will almost always be empty, and the short-
term scheduler will have little to do.
• Similarly, if all processes are CPU bound, the I/O waiting queue will almost always be empty,
devices will go unused, and again the system will be unbalanced.
• So, to have the best system performance LTS needs to select a good combination of I/O and
CPU Bound processes.
Context Switch
• When an interrupt occurs, the system needs to save the
current context of the process running on the CPU so
that it can restore that context when its processing is
done.
29
Q Which is the correct definition of a valid process transition in an operating
system?
a) Wake up: ready → running b) Dispatch: ready → running
30
Pre-emptive
• Under Pre-emptive scheduling, once the CPU has been allocated to a process, A process will
leave the CPU willingly or it can be forced out. So it will leave the CPU
1. When a process completes its execution
2. When a process leaves CPU voluntarily to perform some i/o operations
3. If a new process enters in the ready states (new, waiting), in case of high priority
4. When process switches from running to ready state because of time quantum expire.
31
• Throughput: If the CPU is busy executing processes, then work is being done. One measure of
work is the number of processes that are completed per time unit, called throughput.
• Waiting time: Waiting time is the sum of the periods spent waiting in the ready queue.
32
• Response Time: Is the time it takes to start responding, not the time it
takes to output the response.
• Note: The CPU-scheduling algorithm does not affect the amount of time during
which a process executes I/0; it affects only the amount of time that a process
spends waiting in the ready queue.
Terminology
• Burst Time (BT): Amount of CPU time required by the process to finish its execution.
• Turn Around Time (TAT): Completion Time (CT) – Arrival Time (AT), Waiting Time + Burst Time (BT)
33
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 2 4
P1 1 2
P2 0 3
P3 4 2
P4 3 1
Average
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 0 3
P1 2 2
P2 6 4
Average
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 6 4
P1 2 5
P2 3 3
P3 1 1
P4 4 2
P5 5 6
Average
34
Q Consider the following table of arrival time and burst time for three processes P0,
P1, P2, P3, P4 What is the average Waiting Time and Turnaround Time for the five
processes?
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 0 14
P1 1 3
P2 2 1
P3 3 2
P4 4 5
Average
Advantage
• Easy to understand, and can easily be implemented using Queue data structure.
P0 0 100
P1 1 2
Average
P0 1 100
P1 0 2
Average
• Whenever we make a decision of selecting the next process for CPU execution,
out of all available process, CPU is assigned to the process having smallest burst
time requirement. When the CPU is available, it is assigned to the process that
has the smallest next CPU burst. If there is a tie, FCFS is used to break tie.
• It supports both version non-pre-emptive and pre-emptive (purely greedy
approach)
• This version (SRTF) is also called optimal is it guarantee minimal average waiting
time.
36
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 1 7
2 5
P1
P2 3 1
P3 4 2
P4 5 8
Average
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 0 6
P1 1 4
P2 2 3
P3 3 1
P4 4 2
P5 5 1
Average
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 3 4
P1 4 2
P2 5 1
P3 2 6
P4 1 8
P5 2 4
Average
37
Q Consider an arbitrary set of CPU-bound processes with unequal CPU burst lengths submitted at
the same time to a computer system. Which one of the following process scheduling algorithms
would minimize the average waiting time in the ready queue? (GATE - 2016) (1 Marks)
(b) Round-robin with time quantum less than the shortest CPU burst
Q For the processes listed in the following table, which of the following scheduling
schemes will give the lowest average turnaround time? (GATE-2015) (2 Marks)
Process Arrival Time Processing Time
A 0 3
B 1 6
C 4 4
D 6 2
38
Q Consider the following four processes with arrival times (in milliseconds) and
their length of CPU bursts (in milliseconds) as shown below: (GATE - 2019) (2
Marks)
Process Arrival Time CPU Time CT TAT WT
P1 0 3
P2 1 1
P3 3 3
P4 4 Z
Advantage
• Pre-emptive version guarantees minimal average waiting time so some time also referred as
optimal algorithm.
Disadvantage
• This algo cannot be implemented as there is no way to know the length of the next CPU burst.
• Here process with the longer CPU burst requirement goes into starvation.
39
Q Consider the following three processes with the arrival time and CPU burst time
given in milliseconds: (NET-July-2018)
Process Arrival Time Burst Time
P1 0 7
P2 1 4
P3 2 8
The Gantt Chart for pre-emptive SJF scheduling algorithm is .
Q Consider the following four processes with the arrival time and length of CPU
burst given in milliseconds: (NET-Nov-2018)
Process Arrival Time Burst Time CT TAT WT
P1 0 8
P2 1 4
P3 2 9
P4 3 5
40
Q Consider the following set of processes, with the arrival times and the CPU-burst
times given in milliseconds (GATE - 2017) (2 Marks)
Process Arrival CPU Time CT TAT WT
Time
P1 0 5
P2 1 3
P3 2 3
P4 4 1
What is the average turnaround time for these processes with the pre-emptive
shortest remaining processing time first (SRPT) algorithm?
(A) 5.50 (B) 5.75 (C) 6.00 (D) 6.25
Q Consider the following CPU processes with arrival times (in milliseconds) and length of CPU
bursts (in milliseconds) as given below: (GATE - 2017) (1 Marks)
Process Arrival Time CPU Time CT TAT WT
P1 0 7
P2 3 3
P3 5 5
P4 6 2
If the pre-emptive shortest remaining time first scheduling algorithm is used to schedule the
processes, then the average waiting time across all processes is milliseconds.
41
Q Consider three CPU intensive processes P1, P2, P3 which require 20,10 and 30 units of time,
arrive at times 1,3 and 7 respectively. Suppose operating system is implementing Shortest
Remaining Time first (pre-emptive scheduling) algorithm, then context switches are
required (suppose context switch at the beginning of Ready queue and at the end of Ready
queue are not counted). (NET-Aug-2016)
Process Arrival Time Burst Time
P1 1 20
P2 3 10
P3 7 30
a) 3 b) 2 c) 4 d) 5
Q Consider the following processes, with the arrival time and the length
of the CPU burst given in milliseconds. The scheduling algorithm used is
pre-emptive shortest remaining-time first.
Process Arrival CPU Time CT TAT WT
Time
P1 0 10
P2 3 6
P3 7 1
P4 8 3
The average turnaround time of these processes is milliseconds. (GATE - 2016) (2 Marks)
42
Q Consider the following set of processes that need to be scheduled on a single
CPU. All the times are given in milliseconds? (GATE-2014) (2 Marks)
Process Name Arrival Time Execution Time CT TAT WT
A 0 6
B 3 2
C 5 4
D 7 6
E 10 3
Using the shortest remaining time first scheduling algorithm, the average process
turnaround time (in msec) is _.
Q An operating system uses shortest remaining time first scheduling algorithm for
pre-emptive scheduling of processes. Consider the following set of processes with
their arrival times and CPU burst times (in milliseconds) (GATE-2014) (2 Marks)
Process Arrival Time Burst Time CT TAT WT
P1 0 12
P2 2 4
P3 3 6
P4 8 5
43
Q Consider the following table of arrival time and burst time for three processes P0,
P1 and P2.
Process Arrival Time Burst Time CT TAT WT
P0 0 9
P1 1 4
P2 2 9
The pre-emptive shortest job first scheduling algorithm is used. Scheduling is carried out only at
arrival or completion of processes. What is the average waiting time for the three processes?
(GATE-2011) (2 Marks)
(A) 5.0 ms (B) 4.33 ms (C) 6.33 (D) 7.33
Q An operating system uses Shortest Remaining Time first (SRT) process scheduling
algorithm. Consider the arrival times and execution times for the following
processes:
Process Arrival Time CPU Time CT TAT WT
P1 0 20
P2 15 25
P3 30 10
P4 45 15
What is the total waiting time for process P2? (GATE - 2007) (2 Marks)
(A) 5 (B) 15 (C) 40 (D) 55
44
Q Consider three CPU-intensive processes, which require 10, 20 and 30 time units
and arrive at times 0, 2 and 6, respectively. How many context switches are needed
if the operating system implements a shortest remaining time first scheduling
algorithm? Do not count the context switches at time zero and at the end? (GATE-
2006) (1 Marks)
Process Arrival Time CPU Time
P1 0 10
P2 2 20
P3 6 30
• As SJF is not implementable, we can use the one technique where we try to
predict the CPU burst of the next coming process.
Process t tau
Tau(n+1) = alpa tn + (1- alpa) taun
P1 10 20
P2 12
P3 14
P4
• This idea is also more of theoretical importance as most of the time the burst
requirement of the coming process may vary by a large extent and if the burst
time requirement of all the process is approximately same then there is no
advantage of using this scheme.
45
Priority scheduling
• Here a priority is associated with each process. At any instance of time out of all
available process, CPU is allocated to the process which possess highest priority (may
be higher or lower number).
• Tie is broken using FCFS order. No importance to senior or burst time. It supports both
non-pre-emptive and pre-emptive versions.
• In Priority (non-pre-emptive) once a decision is made and among the available process,
the process with the highest priority is scheduled on the CPU, it cannot be pre-empted
even if a new process with higher priority more than the priority of the running process
enter in the system.
• In Priority (pre-emptive) once a decision is made and among the available process, the
process with the highest priority is scheduled on the CPU.
• if it a new process with priority more than the priority of the running process enter in
the system, then we do a context switch and the processor is provided to the new
process with higher priority.
• Priorities are generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to
4,095. There is no general agreement on whether 0 is the highest or lowest priority, it
can vary from systems to systems.
P1 2 2 5
P2 2 3 7
P3 3 5 8(H)
P4 3 1 5
P5 4 2 6
Average
46
P. No AT BT Priority CT TAT = CT - AT WT = TAT - BT
P0 0 50 4
P1 20 20 1(h)
P2 40 100 3
P3 60 40 2
Average
P0 1 4 5
P1 2 5 2
P2 3 6 6
P3 0 1 4
P4 4 2 1
P5 5 3 8(H)
Average
• Advantage
• Gives a facility specially to system process.
Ageing: - a technique of gradually increasing the priority of processes that wait inthe
system for long time. E.g. priority will increase after every 10 min
47
Q Consider the set of processes with arrival time (in milliseconds), CPU burst time (in
milliseconds), and priority (0 is the highest priority) shown below. None of the processes have
I/O burst time.
Process Arrival Burst Time Priority CT TAT WT
Time
P1 0 11 2
P2 5 28 0
P3 12 2 3
P4 2 10 1
P5 9 16 4
The average waiting time (in milliseconds) of all the processes using preemptive priority
scheduling algorithm is . (GATE-2017) (2 Marks)
48
Q The problem of indefinite blockage of low-priority jobs in general priority
scheduling algorithm can be solved using: (NET-Dec-2012)
a) Parity bit b) Aging c) Compaction d) Timer
Q We wish to schedule three processes P1, P2 and P3 on a uniprocessor system. The priorities, CPU time
requirements and arrival times of the processes are as shown below.
P2 9 10 sec 00:00:03
We have a choice of pre-emptive or non-pre-emptive scheduling. In pre-emptive scheduling, a late-arriving higher priority
process can pre-empt a currently running process with lower priority. In non-pre-emptive scheduling, a late-arriving higher
priority process must wait for the currently executing process to complete before it can be scheduled on the processor.
What are the turnaround times (time from arrival till completion) of P2 using pre-emptive and non-pre-emptive scheduling
respectively. (GATE-2005) (2 Marks)
(A) 30 sec, 30 sec (B) 30 sec, 10 sec (C) 42 sec, 42 sec (D) 30 sec, 42 sec
49
Round robin
Round robin
• This algo is designed for time sharing systems, where it is not, necessary to complete one
process and then start another, but to be responsive and divide time of CPU among the
process in the ready state. Here ready queue is treated as a circular queue (FIFO).
• It is similar to FCFS scheduling, but pre-emption is added to enable the system to switch
between processes.
• The ready queue is treated as a circular queue. The CPU scheduler goes around the ready
queue, allocating the CPU to each process for a time interval equivalent 1 Time quantum
(where value of TQ can be anything).
• We fix a time quantum, up to which a process can hold the CPU in one go, with in which either
a process terminates or process must release the CPU and enter the ready queue and wait for
the next chance.
• The process may have a CPU burst of less than given time quantum. In this case, the process
itself will release the CPU voluntarily.
• CPU Scheduler will select the next process for execution. OR The CPU burst of the currently
running process is longer than 1-time quantum, the timer will go off and will cause an
interrupt to the operating system.
• A context switch will be executed, and the process will be put at the tail of the ready queue.
The CPU scheduler will then select the next process in the ready queue.
• If there are n processes in the ready queue and the time quantum is q, then each process gets
1/n of the CPU time in chunks of at most q time units.
• Each process must wait no longer than (n - 1) x q time units until its next time quantum.
50
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 0 4
P1 1 5
P2 2 2
P3 3 1
P4 4 6
P5 6 3
Average
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 5 5
P1 4 6
P2 3 7
P3 1 9
P4 2 2
P5 6 3
Average
• If the time quantum is extremely large, the RR policy is the same as the FCFS policy.
• If the time quantum is extremely small (say, 1 millisecond), the RR approach is called processor
sharing and (in theory) creates the appearance that each of n processes has its own processor
running at 1/n the speed of the real processor. We also need also to consider the effect of
context switching on the performance of RR scheduling.
51
• Advantage
• Perform best in terms of average response time
• Works will in case of time-sharing systems, client server architecture and interactive
system
• Disadvantage
• Longer process may starve
• Performance depends heavily on time quantum - If value of the time quantum is very less,
then it will give lesser average response time (good but total no of context switches will be
more, so CPU utilization will be less), If time quantum is very large then average response
time will be more bad, but no of context switches will be less, so CPU utilization will be
good.
• No idea of priority
Q consider the following set of processes and the length of CPU burst time given in milliseconds:
P1 5
P2 7
P3 6
P4 4
Assume that processes being scheduled with round robin scheduling algorithm with time
quantum 4ms. Then the waiting for p4 is . (NET-DEC-2018)
a) 0 b) 4 c) 6 d) 12
52
Q Consider the following set of processes with the length of CPU burst time in milliseconds (ms):
Process Burst Time Priority CT TAT WT
A 6 3
B 1 1
C 2 3
D 1 4
E 5 2
Q A scheduling algorithm assigns priority proportional to the waiting time of a process. Every
process starts with priority zero (the lowest priority). The scheduler re-evaluates the process
priorities every T time units and decides the next process to schedule. Which one of the
following is TRUE if the processes have no I/O operations and all arrive at time zero? (GATE-2013)
(1 Marks)
(A) This algorithm is equivalent to the first-come-first-serve algorithm
(B) Decreases
53
Q Consider n processes sharing the CPU in a round-robin fashion. Assuming that each process
switch takes s seconds, what must be the quantum size q such that the overhead resulting from
process switching is minimized but at the same time each process is guaranteed to get its turn at
the CPU at least every t seconds? (GATE-1998) (1 Marks) (NET-Dec-2012)
a) q ≤ (t−ns)/(n−1)
b) q ≥ (t−ns)/(n−1)
c) q ≤ (t−ns)/(n+1)
d) q ≥ (t−ns)/(n+1)
Q If the time-slice used in the round-robin scheduling policy is more than the
maximum time required to execute any process, then the policy will (GATE-2008)
(1 Marks)
(A) degenerate to shortest job first
(B) degenerate to priority scheduling
(C) degenerate to first come first serve
(D) none of the above
Q In processor management, round robin method essentially uses the pre-emptive version of
................... (NET-JUNE-2006)
(A) FILO (B) FIFO (C) SJF (D) Longest time first
Q Four jobs to be executed on a single processor system arrive at time 0+ in the order A, B, C, D.
their burst CPU time requirements are 4, 1, 8, 1 time units respectively. The completion time of A
under round robin scheduling with time slice of one-time unit is. (GATE-1996) (2 Marks) (ISRO-
2008)
(a) 10 (b) 4 (c) 8 (d) 9
54
Q Which scheduling policy is most suitable for a time-shared operating system?
(GATE-1995) (1 Marks)
(a) Shortest Job First
(d) Elevator
Q Assume that the following jobs are to be executed on a single processor system
The jobs are assumed to have arrived at time 0+ and in the order p, q, r, s, t. calculate the
departure time (completion time) for job p if scheduling is round robin with time slice 1. (GATE-
1993) (2 Marks)
Job Id CPU Burst Time
P 4
Q 1
R 8
S 1
T 2
Q In which of the following scheduling criteria, context switching will never take
place? (NET-July-2018)
a) Round Robin b) Pre-emptive SJF
55
Q Which of the following scheduling algorithms may cause starvation? (NET-Jan-2017)
a. First-come-first-served b. Round Robin c. Priority
d. Shortest process next e. Shortest remaining time first
a) a, c and e
b) c, d and e
c) b, d and e
d) b, c and d
d) FCFS
Q Three processes A, B and C each execute a loop of 100 iterations. In each iteration of the loop, a process performs
a single computation that requires tc CPU milliseconds and then initiates a single I/O operation that lasts for ti/o
milliseconds. It is assumed that the computer where the processes execute has sufficient number of I/O devices and
the OS of the computer assigns different I/O devices to each process. Also, the scheduling overhead of the OS is
negligible. The processes have the following characteristics:
Process Id Tc Ti/o
A 100 500
B 350 500
C 200 500
The processes A, B, and C are started at times 0, 5 and 10 milliseconds respectively, in a pure time-sharing system
(round robin scheduling) that uses a time slice of 50 milliseconds. The time in milliseconds at which process C would
complete its first I/O operation is _. (GATE-2014) (2 Mark)
56
Q Consider the following processes with time slice of 4 milliseconds (I/O requests are ignored):
The average turnaround time of these processes will be (NET-June-2013)
Process Arrival Time Processing Time CT TAT WT
A 0 8
B 1 4
C 2 9
D 3 5
57
Q Consider the 3 processes, P1, P2 and P3 shown in the table. (GATE-2012) (2 Marks)
Process Arrival Time Unit Required
Time
P1 0 5
P2 1 7
P3 3 4
The completion order of the 3 processes under the policies FCFS and RR2 (round robin
scheduling with CPU quantum of 2-time units) are
(A) FCFS: P1, P2, P3 RR: P1, P2, P3 (B) FCFS: P1, P3, P2 RR: P1, P3, P2
(C) FCFS: P1, P2, P3 RR: P1, P3, P2 (D) FCFS: P1, P3, P2 RR: P1, P2, P3
(A) 1 only (B) 1 and 3 only (C) 2 and 3 only (D) 1, 2 and 3
58
Q Consider three processes, all arriving at time zero, with total execution time of 10, 20 and 30 units,
respectively. Each process spends the first 20% of execution time doing I/O, the next 70% of time doing
computation, and the last 10% of time doing I/O again. The operating system uses a shortest remaining
compute time first scheduling algorithm and schedules a new process either when the running process
gets blocked on I/O or when the running process finishes its compute burst. Assume that all I/O
operations can be overlapped as much as possible. For what percentage of time does the CPU remain
idle? (GATE-2006) (2 Marks)
(A) 0% (B) 10.6% (C) 30.0% (D) 89.4%
Q The arrival time, priority, and duration of the CPU and I/O bursts for each of three processes P1, P2 and P3 are
given in the table below. Each process has a CPU burst followed by an I/O burst followed by another CPU burst.
Assume that each process has its own I/O resource. (GATE-2006) (2 Marks)
Process Arrival Time Priority Burst duration, CPU, I/O CPU
P1 0 2 1,5,3
P2 2 3(L) 3,3,1
P3 3 1(H) 2,3,1
The programmed operating system uses preemptive priority scheduling. What are the finish
times of the processes P1, P2 and P3 ?
(A) 11, 15, 9 (B) 10, 15, 9 (C) 11, 16, 10 (D) 12, 17, 11
59
Q A uniprocessor computer system only has two processes, both of which alternate 10 ms CPU
bursts with 90 ms I/O bursts. Both the processes were created at nearly the same time. The I/O
of both processes can proceed in parallel. Which of the following scheduling strategies will result
in the least CPU utilization (over a long period of time) for this system? (GATE-2003) (2 Marks)
(A) First come first served scheduling
(C) Static priority scheduling with different priorities for the two processes
P. No AT BT
P1 0 3
P2 1 2
P3 2 4
P4 3 5
P5 4 6
60
Q Consider three processes (process id 0, 1, 2 respectively) with compute time bursts 2, 4 and 8
time units. All processes arrive at time zero. Consider the longest remaining time first (LRTF)
scheduling algorithm. In LRTF ties are broken by giving priority to the process with the lowest
process id. The average turnaround time is? (GATE-2006) (2 Marks)
P. No AT BT CT TAT
P0 0 2
P1 0 4
P2 0 8
(A) 13 units (B) 14 units (C) 15 units (D) 16 units
• Jobs gain higher priority the longer they wait, which prevents indefinite waiting or in other
words what we say starvation. In fact, the jobs that have spent a long time waiting compete
against those estimated to have short run times.
• Response Ratio = (W + S)/S
• Here, W is the waiting time of the process so far and S is the Burst time of the process.
• So, the conclusion is it gives priority to those processes which have less burst time (or
execution time) but also takes care of the waiting time of longer processes, thus preventing
starvation.
Q Consider a set of n tasks with known runtimes r1, r2, …. rn to be run on a
uniprocessor machine. Which of the following processor scheduling algorithms will
result in the maximum throughput? (GATE-2001) (1 Marks)
(A) Round-Robin
(B) Shortest-Job-First
(C) Highest-Response-Ratio-Next
(D) First-Come-First-Served
Q highest response ratio next scheduling policy favours ------- jobs, but it
also limits the waiting time of ---------- jobs. (GATE-1990) (1 Marks)
61
Multi Level-Queue Scheduling
• Another class of scheduling algorithm has been created for situations in which processes are
easily classified into different groups. For example, a common division is made between
foreground (interactive) processes and background (batch) processes. These two types of
processes have different response-time requirements and so may have different scheduling
needs.
• A multilevel queue scheduling algorithm partitions the ready queue into several separate
queues. The processes are permanently assigned to one queue, generally based on some
property of the process, such as memory size, process priority, or process type.
• Each queue has its own scheduling algorithm. For example, separate queues might be used for
foreground and background processes. The foreground queue might be scheduled by an RR
algorithm, while the background queue is scheduled by an FCFS algorithm.
• In addition, there must be scheduling among the queues, which is commonly implemented as
fixed-priority preemptive scheduling. For example, the foreground queue may have absolute
priority over the background queue.
• Each queue has absolute priority over lower-priority queues. No process in the batch queue,
for example, could run unless the queues for system processes, interactive processes, and
interactive editing processes were all empty. If an interactive editing process entered the ready
queue while a batch process was running, the batch process would be preempted.
• Another possibility is to time-slice among the queues. Here, each queue gets a certain portion
of the CPU time, which it can then schedule among its various processes. For instance, in the
foreground– background queue example, the foreground queue can be given 80 percent of the
CPU time for RR scheduling among its processes, while the background queue receives 20
percent of the CPU to give to its processes on an FCFS basis.
62
Q Consider the following justifications for commonly using the two-level CPU scheduling:
I. It is used when memory is too small to hold all the ready processes.
II. Because its performance is same as that of the FIFO.
III. Because it facilitates putting some set of processes into memory and a choice is made from
that.
IV. Because it does not allow to adjust the set of in-core processes.
Which of the following is true? (NET-Dec-2014)
(A) I, III and IV (B) I and II (C) III and IV (D) I and III
• The multilevel feedback queue scheduling algorithm, in contrast, allows a process to move
between queues. The idea is to separate processes according to the characteristics of their
CPU bursts. If a process uses too much CPU time, it will be moved to a lower-priority queue.
This scheme leaves I/O-bound and interactive processes in the higher-priority queues.
• In addition, a process that waits too long in a lower-priority queue may be moved to a higher-
priority queue. This form of aging prevents starvation.
• A process entering the ready queue is put in queue 0.A process in queue 0 is
given a time quantum of 8 milliseconds. If it does not finish within this time, it
is moved to the tail of queue 1. If queue 0 is empty, the process at the head of
queue 1 is given a quantum of 16 milliseconds. If it does not complete, it is
preempted and is put into queue 2.
• Processes in queue 2 are run on an FCFS basis but are run only when queues
0 and 1 are empty.
• This scheduling algorithm gives highest priority to any process with a CPU
burst of 8 milliseconds or less. Such a process will quickly get the CPU, finish
its CPU burst, and go off to its next I/O burst.
63
• Processes that need more than 8 but less than 24 milliseconds are also
served quickly, although with lower priority than shorter processes. Long
processes automatically sink to queue 2 and are served in FCFS order with any
CPU cycles left over from queues 0 and 1.
Q Which of the following statements is not true for Multi-Level Feedback Queue processor
scheduling algorithm? (NET-June-2015)
a) Queues have different priorities
a b c d
(A) i iii ii iv
(B) iv iii ii i
(C) iii i iv i
(D) ii iii iv i
64
Q An example of a non-pre-emptive scheduling algorithm is: (NET-Dec-2008)
(A) Round Robin (B) Priority Scheduling
(C) Shortest job first (D) 2 level scheduling
Q An Operating System (OS) crashes on the average once in 30 days, that is, the Mean Time
Between Failures (MTBF) = 30 days. When this happens, it takes 10 minutes to recover the OS,
that is, the Mean Time To Repair (MTTR) = 10 minutes. The availability of the OS with these
reliability figures is approximately: (NET-AUG-2016)
a) 96.97% b) 97.97% c) 99.009% d) 99.97%
65
Process Synchronization
• Concurrent access to shared data at same time may result in data inconsistency
for e.g.
Race Condition
66
Q The following two functions P1 and P2 that share a variable B with an initial
value of 2 execute concurrently.
P1() P2()
{ {
C = B – 1; D = 2 * B;
B = 2 * C; B = D - 1;
} }
The number of distinct values that B can possibly take after the execution is
(GATE-2015) (1 Mark)
P1() P2()
{ {
(I11) C = B – 1; (I21) D = 2 * B;
(I12) B = 2 * C; (I22) B = D - 1;
} }
P1() P2()
{ {
(I11) C = B – 1; (I21) D = 2 * B;
(I12) B = 2 * C; (I22) B = D - 1;
} }
67
Q Consider three concurrent processes P1 and P2 and P3 P1 P2 P3
68
Criterion to Solve Critical Section Problem
• Mutual Exclusion: No two processes should be present inside the critical section
at the same time, i.e. only one process is allowed in the critical section at an
instant of time.
• Progress: If no process is executing in its critical section and some processes
wish to enter their critical sections, then only those processes that are not
executing in their remainder sections can participate in deciding which will enter
its critical section next(means other process will participate which actually wish
to enter).
Q Which of the following conditions does not hold good for a solution to a critical
section problem? (NET-DEC-2014)
a) No assumptions may be made about speeds or the number of CPUs.
c) Processes running outside its critical section may block other processes.
69
Q Part of a program where the shared memory is accessed and which
should be executed indivisibly, is called: (NET-JUNE-2007)
a) Semaphores
b) Directory
c) Critical Section
d) Mutual exclusion
3. Hardware Solution
1. Test and Set Lock
2. Disable interrupt
• There are 3 Different idea to achieve valid solution, in which some are invalid
while some are valid.
• 3- Peterson’s Solution
70
• Here we will use a Boolean variable turn, which is initialize randomly(0/1).
P0 P1
while (1) while (1)
{ {
while (turn! = 0); while (turn! = 1);
Critical Section Critical Section
turn = 1; turn = 0;
Remainder section Remainder Section
} }
• The solution does not follow the Progress, as it is suffering from the
strict alternation. Because we never asked the process whether it
wants to enter the CS or not?
• Here we will use a Boolean array flag with two cells, where each cell is initialized to 0
P0 P1
while (1) while (1)
{ {
flag [0] = T; flag [1] = T;
while (flag [1]); while (flag [0]);
Critical Section Critical Section
flag [0] = F; flag [1] = F;
Remainder section Remainder Section
} }
• Peterson's solution is a classic Software-based solution to the critical-section problem for two
process.
71
• Peterson's solution is a classic Software-based solution to the critical-section problem for two
process.
• We will be using both: turn and Boolean flag.
P0 P1
while (1) while (1)
{ {
flag [0] = T; flag [1] = T;
turn = 1; turn = 0;
while (turn = = 1 && flag [1] = = T); while (turn = = 0 && flag [0] = =T);
Critical Section Critical Section
flag [0] = F; flag [1] = F;
Remainder section Remainder Section
} }
• This solution ensures Mutual Exclusion, Progress and Bounded Wait.
Q Consider the methods used by processes P1 and P2 for accessing their critical sections
whenever needed, as given below. The initial values of shared Boolean variables S1 and S2 are
randomly assigned. (GATE-2010) (1 Marks) (NET-JUNE-2012)
P1() P2()
While(S1==S2); While(S1!=S2);
Critical section Critical section
S1=S2; S1=not(S2);
72
Q Consider the following two-process Process 0 Process 1
synchronization solution. (GATE-2016) (2 Entry: loop while (turn == 1); Entry: loop while (turn == 0);
Marks)
The shared variable turn is initialized to zero. (critical section) (critical section)
Which one of the following is TRUE? Exit: turn = 1; Exit: turn = 0;
(a) This is a correct two-process
synchronization solution.
73
Q Two processes, P1 and P2, need to access a critical section of code. Consider the following
synchronization construct used by the processes: Here, wants1 and wants2 are shared variables,
which are initialized to false. Which one of the following statements is TRUE about the above
construct? (GATE-2007) (2 Mark)
P1() P2()
(A) It does not ensure mutual exclusion. While(t) While(t)
{ {
(B) It does not ensure bounded waiting.
wants1 = T wants2 = T
(C) It requires that processes enter the While (wants2== T); While (wants1== T);
critical section in strict alternation.
Critical section Critical section
(D) It does not prevent deadlocks, but wants1 = F wants2 = F
ensures mutual exclusion. Remainder section Remainder section
} }
flag[i] = f;
Perform other non-critical section actions
Until false;
74
Operation System Solution
• Semaphores are synchronization tools using which we will attempt n-process solution.
• A semaphore S is a simple integer variable that, but apart from initialization it can be accessed only
through two standard atomic operations: wait(S) and signal(S).
• The wait(S) operation was originally termed as P(S) and signal(S) was originally called V(S).
• The definition of wait () is as follows:
Wait(S) Signal(S)
{ {
while(s<=0);
s++;
s--;
} }
• Peterson’s Solution was confined to just two processes, and since in a general system can have n
processes, Semaphores provides n-processes solution.
• While solving Critical Section Problem only we initialize semaphore S = 1.
Pi()
{
While(T)
{
Initial Section
wait(s)
Critical Section
signal(s)
Remainder Section
}
}
• Semaphores are going to ensure Mutual Exclusion and Progress but does not ensures bounded waiting.
• We must guarantee that no two processes can execute wait() and signal() operations
on the same semaphore at the same time. This is a critical-section problem; and in a
single-processor environment, we can solve it by simply inhibiting interrupts during the
time the wait() and signal() operations are executing.
75
• In a multiprocessor environment, interrupts must be disabled on every processor.
Otherwise, instructions from different processes (running on different processors) may
be interleaved in some arbitrary way.
• Disabling interrupts on every processor can be a difficult task and furthermore can
seriously diminish performance. Therefore, SMP systems must provide alternative
locking techniques —such as compare and swap() or spinlocks —to ensure that wait()
and signal() are performed atomically
76
Q Semaphores are used to: (NET-DEC-2004)
a) Synchronise critical resources to prevent deadlock
c) Do I/o
Ordering
Q Using Semaphores ensure that the order of execution of there
concurrent process p1, p2 and p3 must be p2→ p3→ p1?
77
Q A certain computation generates two arrays a and b such that a[i]=f(i) for 0 ≤ i < n and b[i]=g(a[i]) for 0 ≤ i < n.
Suppose this computation is decomposed into two concurrent processes X and Y such that X computes the array a
and Y computes the array b. The processes employ two binary semaphores R and S, both initialized to zero. The
array a is shared by the two processes. The structures of the processes are shown below. (GATE-2013) (2 Marks)
Process X: Process X: ExitX(R, S) { ExitX(R, S) {
private i; private i; P(R); V(R);
for (i=0; i < n; i++) { for (i=0; i < n; i++) { V(S); V(S);
} }
a[i] = f(i); EntryY(R, S); a) EntryY(R, S) { b) EntryY(R, S) {
ExitX(R, S); b[i]=g(a[i]); P(S); P(R);
} } V(R); P(S);
} }
ExitX(R, S) {
ExitX(R, S) { V(R);
P(S); P(S);
V(R); }
} EntryY(R, S) {
c) EntryY(R, S) { d) V(S);
V(S); P(R);
}
Q Two concurrent processes P1 and P2 use four shared resources R1, R2, R3
and R4, as shown below. P1 P2
Both processes are started at the same time, and each resource can be
accessed by only one process at a time The following scheduling constraints
exist between the access of resources by the processes: Compute: Compute;
i) P2 must complete use of R1 before P1 gets access to R1
ii) P1 must complete use of R2 before P2 gets access to R2. Use R1; Use R1;
iii) P2 must complete use of R3 before P1 gets access to R3.
iv) P1 must complete use of R4 before P2 gets access to R4. Use R2; Use R2;
There are no other scheduling constraints between the processes. If only
binary semaphores are used to enforce the above scheduling constraints,
what is the minimum number of binary semaphores needed? (GATE-2005) (2 Use R3; Use R3;.
Marks)
(A) 1 (B) 2 (C) 3 (D) 4 Use R4; Use R4;
78
P2 must complete use of R1 before P1 gets access to R1
P1 must complete use of R2 before P2 gets access to R2. P1 P2
P2 must complete use of R3 before P1 gets access to R3.
P1 must complete use of R4 before P2 gets access to R4. R1
P1 P2
P(X) R1
R1 V(X)
R2 P(Y)
V(Y) R2
P(X) R3
R3 V(X)
R4 P(Y)
V(Y) R4
79
Q Semaphores S and T. The code for the processes P and Q is shown below. Synchronization
statements can be inserted only at points W, X, Y and Z.
Process P Process Q
While(t) While(t)
{ {
W: Y:
print ‘0’; print ‘1’;
print ‘0’; print ‘1’;
X: Z:
} }
Which of the following will always lead to an output staring with ‘001100110011’ ? (GATE-
2004) (2 Marks)
(A) P(S) at W, V(S) at X, P(T) at Y, V(T) at Z, S and T initially 1
(B) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S initially 1, and T initially 0
(C) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S and T initially 1
(D) P(S) at W, V(S) at X, P(T) at Y, V(T) at Z, S initially 1, and T initially 0
Q Semaphores S and T. The code for the processes P and Q is shown below. Synchronization
statements can be inserted only at points W, X, Y and Z.
Process P Process Q
While(t) While(t)
{ {
W: Y:
print ‘0’; print ‘1’;
print ‘0’; print ‘1’;
X: Z:
} }
Which of the following will ensure that the output string never contains a substring of the form
01n0 or 10n1 where n is odd? (GATE-2004) (2 Marks)
(A) P(S) at W, V(S) at X, P(T) at Y, V(T) at Z, S and T initially 1
(B) P(S) at W, V(T) at X, P(T) at Y, V(S) at Z, S and T initially 1
(C) P(S) at W, V(S) at X, P(S) at Y, V(S) at Z, S initially 1
DeadLock
80
Q which of the following will ensure deadlock free execution, if both s1 and s2 both
are initialized to 1?
81
Q Three concurrent processes X, Y, and Z execute three different code segments that access and update certain
shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P
operation on semaphores b, c and d; process Z executes the P operation on semaphores c, d, and a before entering
the respective code segments. After completing the execution of its code segment, each process invokes the V
operation (i.e., signal) on its three semaphores. All semaphores are binary semaphores initialized to one. Which one
of the following represents a deadlock-free order of invoking the P operations by the processes? (GATE-2013) (1
Marks)
(A) X: P(a)P(b)P(c) Y: P(b)P(c)P(d) Z: P(c)P(d)P(a) X Y Z
(B) X: P(b)P(a)P(c) Y: P(b)P(c)P(d) Z: P(a)P(c)P(d)
(C) X: P(b)P(a)P(c) Y: P(c)P(b)P(d) Z: P(a)P(c)P(d)
(D) X: P(a)P(b)P(c) Y: P(c)P(b)P(d) Z: P(c)P(d)P(a)
CS CS CS
Q Consider two processes P1 and P2 accessing the shared variables X and Y protected by two
binary semaphores SX and SY respectively, both initialized to 1. P and V denote the usual
semaphone operators, where P decrements the semaphore value, and V increments the
semaphore value. The pseudo-code of P1 and P2 is as follows: (GATE-2004) (2 Marks)
In order to avoid deadlock, the correct operators at L1, L2, L3 and L4 are respectively
(A) P(SY), P(SX); P(SX), P(SY)
P1 P2
(B) P(SX), P(SY); P(SY), P(SX) While true do { While true do {
L1 : ……….. L3 : ………..
(C) P(SX), P(SX); P(SY), P(SY) L2 : ……….. L4 : ………..
82
Q A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as
follows. Each of the processes W and X reads x from memory, increments by one, stores it to
memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements
by two, stores it to memory, and then terminates. Each process before reading x invokes the P
operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the
semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum
possible value of x after all processes complete execution? (GATE-2013) (2 Marks)
(A) -2 (B) -1 (C) 1 (D) 2
W X Y Z
Wait(S) Wait(S) Wait(S) Wait(S)
R(x) R(x) R(x) R(x)
X=x+1 X=x+1 X=x-2 X=x-2
W(x) W(x) W(x) W(x)
Signal(S) Signal(S) Signal(S) Signal(S)
Barrier is a synchronization construct where a set of processes synchronizes globally i.e. each void barrier (void) {
process in the set arrives at the barrier and waits for all others to arrive and then all 1: P(S);
processes leave the barrier. Let the number of processes in the set be three and S be a 2: process_arrived++;
binary semaphore with the usual P and V functions. Consider the following C 3. V(S);
implementation of a barrier with line numbers shown on left.
4: while (process_arrived !=3);
The variables process_arrived and process_left are shared among all processes and are 5: P(S);
initialized to zero. In a concurrent program all the three processes call the barrier function 6: process_left++;
when they need to synchronize globally. 7: if (process_left==3) {
8: process_arrived = 0;
Q The above implementation of barrier is incorrect. Which one of the following is true? 9: process_left = 0;
(GATE-2006) (2 Marks)
10: }
a) The barrier implementation is wrong due to the use of binary semaphore S.
11: V(S);
b) The barrier implementation may lead to a deadlock if two barrier in invocations are used
}
in immediate succession.
c) Lines 6 to 10 need not be inside a critical section.
d) The barrier implementation is correct if there are only two processes instead of three.
83
Q Given below is a program which when executed spawns two
concurrent processes: P1 P2
semaphore X : = 0 ;
/* Process now forks into concurrent processes P1 & P2 */
repeat forever repeat forever
Consider the following statements about processes P1 and P2:
i) It is possible for process P1 to starve. V (X) ; P(X) ;
ii) It is possible for process P2 to starve.
Which of the following holds? (GATE-2005) (2 Marks)
Compute ; Compute ;
(A) Both I and II are true
P(X) ; V(X) ;
(B) I is true but II is false
• Reader-Writer problem
84
Producer-Consumer Problem
• Problem Definition – There are two process Producer and Consumers, producer produces
information and put it into a buffer which have n cell, that is consumed by a consumer.
• Both Producer and Consumer can produce and consume only one article at a time.
• Similarly, a consumer needs to check for an underflow before accessing the buffer and then consume an item.
• Also, the producer and consumer must be synchronized, so that once a producer and consumer it accessing the
buffer the other must wait.
Producer() Consumer()
Semaphore S =
Semaphore E =
Semaphore F =
85
Producer() Consumer()
Producer() Consumer()
Semaphore S = { {
while(T) while(T)
{ {
Semaphore E =
Semaphore F =
} }
Producer() Consumer()
Producer() Consumer()
{ {
Semaphore S =
while(T) while(T)
{ {
Semaphore E = // Produce an item
// Consume item
} }
Producer() Consumer()
Producer() Consumer()
{ {
Semaphore S =
while(T) while(T)
{ {
Semaphore E = // Produce an item
wait(S)
wait(S) // Pick item from buffer
Semaphore F =
// Add item to buffer signal(S)
signal(S)
// Consume item
} }
86
Total three resources Producer() Consumer()
are used { {
• semaphore E take
while(T) while(T)
count of empty cells
{ {
and over flow
• semaphore F take // Produce an item wait(F)//UnderFlow
87
Q Consider the procedure below for the Producer-Consumer Void Producer () Void Consumer ()
problem which uses semaphores: { {
Semaphore n = 0; While(true) While(true)
Semaphore s = 1; { {
Produce (); semWait(s);
Which one of the following is TRUE? (GATE-2014) (2 Marks) SemWait(s); semWait(n);
(A) The producer will be able to add an item to the buffer, but addToBuffer(); RemovefromBuffer();
the consumer can never consume it.
semSignal(s); semSignal(s);
SemSignal(n); consume();
(B) The consumer will remove no more than one item from
the buffer. } }
} }
(C) Deadlock occurs if the consumer succeeds in acquiring
semaphore s when the buffer is empty.
(D) The starting value for the semaphore n must be 1 and not
0 for deadlock-free operation.
Q Process P1repeatedly adds one item at a time to a buffer of size n, and process
P2 repeatedly removes one item at a time from the same buffer using the programs
given below. In the programs, K, L, M and N are unspecified statements. (GATE-
2004) (2 Marks) P1() p2()
The statements K, L, M and N are respectively while(T) { while(T) {
(B) P(full), V(empty), P(empty), V(full) Add an item to the buffer; Remove an item to the buffer;
V(mutex); V(mutex);
(C) P(empty), V(full), P(empty), V(full)
L; N;
} }
(D) P(empty), V(full), P(full), V(empty)
88
Reader-Writer Problem
• Suppose that a database is to be shared
among several concurrent processes. Some
of these processes may want only to read the
database (readers), whereas others may
want to update (that is, to read and write)
the database(writers). The former are
referred to as readers and to the latter as
writers.
Writer() Reader()
Mutex =
Wrt =
Readcount =
89
Writer() Reader()
Three resources are used
• Semaphore Wrt is
used for
synchronization
between WW, WR,
RW
• Semaphore reader is
used to synchronize Wait(wrt)
between RR
• Readcount is simple CS //Write CS //Read
int variable which
keep counts of Signal(wrt)
number of readers
Writer() Reader()
Three resources are used
• Semaphore Wrt is
used for Readcount++
synchronization
between WW, WR,
RW
• Semaphore reader is
used to synchronize Wait(wrt)
between RR
• Readcount is simple CS //Write CS //Read
int variable which
keep counts of Signal(wrt)
number of readers
Readcount--
90
Q To overcome difficulties in Readers-Writers problem, which of the following
statement/s is/are true? (NET-DEC-2018)
1) Writers are given exclusive access to shared objects
2) Readers are given exclusive access to shared objects
3) Both readers and writers are given exclusive access to shared objects.
Choose the correct answer from the code given below:
a) 1 b) 2 c) 3 d) 2 and 3 only
91
Q Let Pi and Pj two processes, R be the set of variables read from memory, and W
be the set of variables written to memory. For the concurrent execution of two
processes pi and Pj which of the conditions are not true? (NET-JUNE-2015)
a) R(Pi)∩W(Pj)=Φ b) W(Pi)∩R(Pj)=Φ
c) R(Pi)∩R(Pj)=Φ d) W(Pi)∩W(Pj)=Φ
Q Synchronization in the classical readers and writers problem can be achieved through use of
semaphores. In the following incomplete code for readers-writers problem, two binary
semaphores mutex and wrt are used to obtain synchronization (GATE-2007) (2 Marks)
Writer() Reader()
Wait(mutex)
Readcount++
If(readcount ==1)
S1
Wait(wrt) S2
Writing is performed CS //Read
Signal(wrt) S3
Readcount--
If(readcount ==0)
The values of S1, S2, S3, S4, (in that order) are S4
(A) signal (mutex), wait (wrt), signal (wrt), wait (mutex) signal(mutex)
(B) signal (wrt), signal (mutex), wait (mutex), wait (wrt)
(C) wait (wrt), signal (mutex), wait (mutex), signal (wrt)
92
• From time to time, a philosopher gets hungry and tries to
pick up the two chopsticks that are closest to her (the
chopsticks that are between her and her left and right
neighbors).
93
• One philosopher picks up her right chopstick first
and then left chop stick, i.e. reverse the
sequence of any philosopher.
Q A solution to the Dining Philosophers Problem which avoids deadlock is: (GATE-
1996) (2 Marks)
(A) ensure that all philosophers pick up the left fork before the right fork
(B) ensure that all philosophers pick up the right fork before the left fork
(C) ensure that one particular philosopher picks up the left fork before the right
fork, and that all other philosophers pick up the right fork before the left fork
94
Types of Semaphore
• Binary Semaphores: The value of a binary semaphore can range only between 0 and 1.
• Counting Semaphores: can range over an unrestricted domain Counting semaphore can range over an
unrestricted domain. i.e. -∞ to +∞.
• Counting semaphores can be used to control access to a given resource consisting of a finite number of
instances. The semaphore is initialized to the number of resources available.
• Each process that wishes to use a resource performs a wait() operation on the semaphore (thereby
decrementing the count). When a process releases a resource, it performs a signal() operation (incrementing the
count).
• When the count for the semaphore goes to 0, all resources are being used. After that, processes that wish to use
a resource will block until the count becomes greater than 0.
Implementation of semaphore
• This simple implementation of semaphore with this wait(S) and signal(S) function suffer from
busy waiting.
• Modified Wait
• To overcome the need for busy waiting, we can modify the definition of the wait() and
signal() operations as follows: When a process executes the wait() operation and finds that
the semaphore value is not >0, it must wait. However, rather than engaging in busy
waiting, the process can block itself. The block operation places a process into a waiting
queue associated with the semaphore, and the state of the process is switched to the
waiting state. Then control is transferred to the CPU scheduler, which selects another
process to execute.
wait(semaphore *S)
{
S->value--;
if (S->value < 0)
{
add this process to S->list;
block();
}
}
95
• Modified Signal
• A process that is blocked, waiting on a semaphore S, should be restarted when some other
process executes a signal() operation. The process is restarted by a wakeup() operation,
which changes the process from the waiting state to the ready state. The process is then
placed in the ready queue. (The CPU may or may not be switched from the running
process to the newly ready process, depending on the CPU-scheduling algorithm.)
Signal(semaphore *S)
{
S->value++;
if (S->value <= 0)
{
remove a process P from S->list;
wakeup(P);
}
}
• The block() operation suspends the process that invokes it. The wakeup(P) operation resumes
the execution of a blocked process P. These two operations are provided by the operating
system as basic system calls.
• Note that in this implementation, semaphore values may be negative, whereas semaphore
values are never negative under the classical definition of semaphores with busy waiting. If a
semaphore value is negative, its magnitude is the number of processes waiting on that
semaphore. This fact results from switching the order of the decrement and the test in the
implementation of the wait() operation.
• The list of waiting processes can be easily implemented by a link field in each process control
block (PCB). Each semaphore contains an integer value and a pointer to a list of PCBs. One way
to add and remove processes from the list so as to ensure bounded waiting is to use a FIFO
queue, where the semaphore contains both head and tail pointers to the queue.
96
Q At a particular time of computation, the value of a counting semaphore is 10.
Then 12 P operations and “x” V operations were performed on this semaphore. If
the final value of semaphore is 7, x will be: (NET-JULY-2018)
a) 8 b) 9 c) 10 d) 11
Q There are three processes P1, P2 and P3 sharing a semaphore for synchronizing a variable.
Initial value of semaphore is one. Assume that negative value of semaphore tells us how many
processes are waiting in queue. Processes access the semaphore in following order : (NET-JAN-
2017)
97
Q A semaphore count of negative n means (s=–n) that the queue
contains ………….. waiting processes. (NET-DEC-2009)
a) n + 1 b) n c) n – 1 d) 0
Q The P and V operations on counting semaphores, where s is a counting semaphore, are defined as
follows:
P(s) : s = s - 1;
if (s < 0) then wait;
P(s) Pb(Xb);
s = s - 1;
V(s) : s = s + 1; if (s < 0) {
if (s <= 0) then wakeup a process waiting on s; Vb(Xb) ;
Assume that Pb and Vb the wait and signal operations on Pb(Yb) ;
binary semaphores are provided. Two binary semaphores Xb }
and Yb are used to implement the semaphore operations
P(s) and V(s) as follows:
else Vb(Xb);
V(s) Pb(Xb) ;
The initial values of Xb and Yb are respectively (GATE-2008) s = s + 1;
(2 Marks)
(A) 0 and 0 (B) 0 and 1 if (s <= 0)
Vb(Yb) ;
(C) 1 and 0 (D) 1 and 1 Vb(Xb) ;
98
Q At a particular time of computation the value of a counting semaphore is 7. Then
20 P operations and 15V operations were completed on this semaphore. If the new
value of semaphore is will be (GATE-1992) (1 Marks)
(A) 42 (B) 2 (C) 7 (D) 12
Disable Interrupt
• This could be a hardware solution where process Pi()
have a privilege instruction, i.e. before entering {
While(T)
into critical section, process will disable all the
{
interrupts and at the time of exit, it again enable Initial Section
interrupts. Entry Section//Disable interrupt
Critical Section
• This solution is only used by OS, as if some user Exit Section//Enable interrupt
process enter into critical section, then can block Remainder Section
the entire system. }
}
• Unfortunately, this solution is not as feasible in a multiprocessor environment. Disabling
interrupts on a multiprocessor can be time consuming, since the message is passed to all the
processors. This message passing delays entry into each critical section, and system efficiency
decreases.
99
Hardware Type Solution Test and Set
• Software-based solutions such as Peterson’s are not guaranteed to work on modern computer
architectures. In the following discussions, we explore several more solutions to the critical-
section problem using techniques ranging from hardware to software, all these solutions are
based on the premise of locking —that is, protecting critical regions through the use of locks.
100
Q The enter_CS() and leave_CS() functions to implement critical section of a process are realized
using test-and-set instruction as follows:
void enter_CS(X) void leave_CS(X)
{ {
while test-and-set(X) ; X = 0;
} }
In the above solution, X is a memory location associated with the CS and is initialized to 0. Now
consider the following statements: (GATE-2009) (2 Marks)
I. The above solution to CS problem is deadlock-free
II. The solution is starvation free.
III. The processes enter CS in FIFO order.
IV. More than one process can enter CS at the same time.
Which of the above statements is TRUE?
(A) I only (B) I and II (C) II and III (D) IV only
101
Q The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value
of x in y without allowing any intervening access to the memory location x. consider the following implementation
of P and V functions on a binary semaphore S. Which one of the following is true? (GATE-2006) (2 Marks)
102
Q Suppose a processor does not have any stack pointer register. Which of the
following statements is true? (GATE-2001) (1 Marks)
(A) It cannot have subroutine call instruction
(B) It can have subroutine call instruction, but no nested subroutine calls
(C) Nested subroutine calls are possible, but interrupts are not
(D) All sequences of subroutine calls and also interrupts are possible
103
Q Consider the following proposed solution for the critical section problem. There are n processes: P0...P(n-1). In the
code, function pmax returns an integer not smaller
than any of its arguments. For all i, t[i] is
initialized to zero. Code for Pi:
do {
c[i]=1; t[i] = pmax(t[0],...,t[n-1])+1; c[i]=0;
for every j ≠ i in
{0,...,n-1} { while
(c[j]);
while (t[j] != 0 && t[j]<=t[i]);
}
Critical
Section;
t[i]=0;
104
105
• A process requests resources; if the resources are not available at that time, the
process enters a waiting state.
• Sometimes, a waiting process is never again able to change state, because the
resources it has requested are held by other waiting processes. This situation is
called a deadlock.
P1 P2
R1 R2
System model
Under the normal mode of operation, a process may utilize a resource in only the following
sequence:
• Request. The process requests the resource. If the request cannot be granted immediately
(for example, if the resource is being used by another process), then the requesting process
must wait until it can acquire the resource.
• Use. The process can operate on the resource (for example, if the resource is a printer, the
process can print on the printer).
106
• Mutual exclusion: - At least one resource must be held in a non-sharable mode;
that is, only one process at a time can use the resource.
• Hold and wait: - A process must be holding at least one resource and
waiting to acquire additional resources that are currently being held
by other processes. E.g. Plate and spoon
107
• Circular wait: - A set P0, P1, ..., Pn of waiting processes must exist such
that P0 is waiting for a resource held by P1, P1 is waiting for a resource
held by P2, ..., Pn−1 is waiting for a resource held by Pn, and Pn is
waiting for a resource held by P0.
• Avoidance: - Try to avoid deadlock in run time so ensuring that the system will never enter a
deadlocked state.
• Detection: - We can allow the system to enter a deadlocked state, then detect it, and recover.
• Ignorance: - We can ignore the problem altogether and pretend that deadlocks never occur in
the system.
• Last solution is the one used by most operating systems, including Linux and Windows. It is
then up to the application developer to write programs that handle deadlocks.
• Some researchers have argued that none of the basic approaches alone is appropriate for the
entire spectrum of resource-allocation problems in operating systems.
• The basic approaches can be combined, however, allowing us to select an optimal approach
for each class of resources in a system.
Prevention
• It means designing such systems where there is no possibility of existence of
deadlock. For that we have to remove one of the four necessary condition of
deadlock.
108
Hold & wait
No pre-emption
• if a process requests some resources, we first check whether they are available. If they are, we
allocate them.
• If they are not, we check whether they are allocated to some other process that is waiting for
additional resources. If so, we pre-empt the desired resources from the waiting process and
allocate them to the requesting process.
• If the resources are neither available nor held by a waiting process, the requesting process
must wait. While it is waiting, some of its resources may be pre-empted, but only if another
process requests them.
• A process can be restarted only when it is allocated the new resources it is requesting and
recovers any resources that were pre-empted while it was waiting.
Circular wait
• We can eliminate circular wait problem by giving a natural number mapping to
every resource and then any process can request only in the increasing order
and if a process wants a lower number, then process must first release all the
resource larger than that number and then give a fresh request.
109
Q Consider the following policies for preventing deadlock in a system with mutually exclusive
resources.
I) Process should acquire all their resources at the beginning of execution. If any resource is not
available, all resources acquired so far are released.
II) The resources are numbered uniquely, and processes are allowed to request for resources
only in increasing resource numbers
III) The resources are numbered uniquely, and processes are allowed to request for resources
only in decreasing resource numbers
IV) The resources are numbered uniquely. A process is allowed to request for resources only for a
resource with resource number larger than its currently held resources
Which of the above policies can be used for preventing deadlock? (GATE-2015) (2 Marks)
(A) Any one of I and III but not II or IV
(B) Any one of I, III and IV but not II
(C) Any one of II and III but not I or IV
(D) Any one of I, II, III and IV
Q Consider a system with 3 processes that share 4 instances of the same resource type. Each
process can request a maximum of K instances. Resource instances can be requested and
released only one at a time. The larges value of K that will always avoid deadlock is .
(GATE-2018) (1 Marks)
111
Q A system has 6 identical resources and N processes competing for
them. Each process can request at most 2 resources. Which one of the
following values of N could lead to a deadlock? (GATE-2015) (1 Marks)
a) 1 b) 2 c) 3 d) 6
Q A system contains three programs and each requires three tape units for its
operation. The minimum number of tape units which the system must have such
that deadlocks never arise is (GATE-2014) (2 Marks)
(A) 6 (B) 7 (C) 8 (D) 9
112
Q A computer has 6 tape drives with ‘n’ processes competing for them.
Each process may need two drives. For which values of ‘n’ is the system
deadlock free? (NET-JUNE-2008)
(A) 1 (B) 2 (C) 3 (D) 6
Q A computer has six tape drives, with n processes competing for them.
Each process may need two drives. What is the maximum value of n for
the system to be deadlock free? (GATE-1998) (2 Marks)
(A) 6 (B) 5 (C) 4 (D) 3
113
Q An operating system contains 3 user processes each requiring 2 units
of resource R. The minimum number of units of R such that no
deadlocks will ever arise is (GATE-1997) (2 Marks)
(A) 3 (B) 5 (C) 4 (D) 6
Q Consider a system having m resources of the same type. These resources are
shared by 3 processes A, B and C, which have peak demands of 3, 4 and 6
respectively. For what value of m deadlock will not occur? (GATE-1993) (2 Marks)
(a) 7 (b) 9 (c) 10 (d) 13 (e) 15
• With this additional knowledge, the operating system can decide for each request
whether process should wait or not.
115
Max Need Allocation Current Need
E F G E F G E F G
P0 P0 P0
P1 P1 P1
P2 P2 P2
P3 P3 P3
• The resource- allocation state is defined by the number of available and allocated resources
and the maximum demands of the processes before allowing that request first
Max Need Allocation Current Need
E F G E F G E F G
P0 4 3 1 P0 1 0 1 P0 3 3 0
P1 2 1 4 P1 1 1 2 P1 1 0 2
P2 1 3 3 P2 1 0 3 P2 0 3 0
P3 5 4 1 P3 2 0 0 P3 3 4 1
System Max Available
E F G E F G
8 4 6 3 3 0
116
• We check, if there exist “some sequence in which we can satisfies demand of every process
without going into deadlock, if yes, this sequence is called safe sequence” and request can be
allowed. Otherwise there is a possibility of going into deadlock.
Max Need Allocation Current Need
E F G E F G E F G
P0 4 3 1 P0 1 0 1 P0 3 3 0
P1 2 1 4 P1 1 1 2 P1 1 0 2
P2 1 3 3 P2 1 0 3 P2 0 3 0
P3 5 4 1 P3 2 0 0 P3 3 4 1
Q A single processor system has three resource types X, Y and Z, which are shared by three
processes. There are 5 units of each resource type. Consider the following scenario, where the
column alloc denotes the number of units of each resource type allocated to each process, and
the column request denotes the number of units of each resource type requested by a process in
order to complete execution. Which of these processes will finish LAST? (GATE-2007) (2 Marks)
Allocation request
X Y Z X Y Z
P0 1 2 1 1 0 3
P1 2 0 1 0 1 2
P2 2 2 1 1 2 0
(A) P0 (B) P1
(C) P2 (D) None of the above, since the system is in a deadlock
117
Banker’s Algorithm
Several data structures must be maintained to implement the banker’s algorithm. These data structures
encode the state of the resource-allocation system. We need the following data structures, where n is the
number of processes in the system and m is the number of resource types:
Max Need
E F G
P0 4 3 1
Max: An n*m matrix defines the maximum demand of P1 2 1 4
each process. If Max[i][j] equals k, then process Pi may P2 1 3 3
P3 5 4 1
request at most k instances of resource type R j.
118
Safety Algorithm
We can now present the algorithm for finding out whether or not a system is in a safe state. This
algorithm can be described as follows:
1- Let Work and Finish be vectors of length m and n, respectively. Initialize Work = Available and
Finish[i] = false for i = 0, 1, ..., n − 1.
2- Find an index i such that both
Finish[i] == false
Needi ≤ Work
If no such i exists, go to step 4.
3- Work = Work + Allocationi
Finish[i] = true
Go to step 2.
4- If Finish[i] == true for all i, then the system is in a safe state.
This algorithm may require an order of m*n2 operations to determine whether a state is safe.
Resource-Request Algorithm
Next, we describe the algorithm for determining whether requests can be safely granted.
Let Requesti be the request vector for process Pi. If Requesti [j] k, then process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the
following actions are taken:
1- If Requesti <= Needi, go to step 2. Otherwise, raise an error condition, since the
process has exceeded its maximum claim.
3- Have the system pretend to have allocated the requested resources to process Pi
by modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If the resulting resource-allocation state is safe, the transaction is completed, and process Pi is
allocated its resources. However, if the new state is unsafe, then Pi must wait for Requesti, and
the old resource-allocation state is restored.
119
Q Consider the following snapshot of a system running n concurrent processes. Process i is
holding Xi instances of a resource R, 1 ≤ i ≤n. Assume that all instances of R are currently in use.
Further, for all i, process i can place a request for at most Yi additional instances of R while
holding the Xi instances it already has. Of the n processes, there are exactly two
processes p and q such that Yp=Yq=0. Which one of the following conditions guarantees that no
other process apart from p and q can complete execution? (GATE-2019) (2 Marks)
a) Xp + Xq < Min{Yk ∣ 1 ≤k ≤n, k ≠ p, k ≠ q} b) Xp + Xq < Max{Yk ∣ 1 ≤k ≤n, k ≠ p, k ≠ q}
Q Consider a system with five processes P0 through P4 and three resource types A, B and C.
Resource type A has seven instances, resource type B has two instances and resource type C has
six instances suppose at time T = 0 we have the following allocation.
If we implement Deadlock detection algorithm, we claim that system is . (NET-NOV-
2017)
(a) Semaphore (b) Deadlock state (c) Circular wait (d) Not in deadlock state
Allocation Request Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 2 2 0 0 2
120
Q Suppose there are four processes in execution with 12 instances of a
Resource R in a system. The maximum need of each process and current
allocation are given below:
Process Max Need Allocation Current Need
P1 8 3
P2 9 4
P3 5 2
P4 3 1
With reference to current allocation, is system safe? If so, what is the safe
sequence? (NET-JULY-2016)
a) No b) Yes, P1P2P3P4 c) Yes, P4P3P1P2 d) Yes, P2P1P3P4
Q Consider a system with twelve magnetic tape drives and three processes P1, P2
and P3. Process P1 requires maximum ten tape drives, process P2 may need as
many as four tape drives and P3 may need up to nine tape drives. Suppose that at
time t1, process P1 is holding five tape drives, process P2 is holding two tape drives
and process P3 is holding three tape drives. At time t1, system is in: (NET-DEC-2015)
A) safe state b) unsafe state
c) deadlocked state d) starvation state
Process Max Need Allocation Current Need
P1 10 5
P2 4 2
P3 9 3
121
Q A system shares 9 tape drives. The current allocation and maximum requirement
of tape drives for 3 processes are shown below:
Which of the following best describes the current state of the system? (GATE-2014)
(2 Marks)
Proces Current Maximum Current Current
Requirement Available
s Allocation Requirement
P1 3 7
P2 1 6
P3 3 5
Q An operating system has 13 tape drives. There are three processes P1, P2 & P3.
Maximum requirement of P1 is 11 tape drives, P2 is 5 tape drives and P3 is 8 tape
drives. Currently, P1 is allocated 6 tape drives, P2 is allocated 3 tape drives and P3 is
allocated 2 tape drives. Which of the following sequences represent a safe state?
(NET-DEC-2014)
(A) P2 P1 P3 (B) P2 P3 P1 (C) P1 P2 P3 (D) P1 P3 P2
Process Max Need Allocation Current Need
P1 11 6
P2 5 3
P3 8 2
122
Q An operating system uses the Banker’s algorithm for deadlock avoidance when managing the allocation of three
resource types X, Y, and Z to three processes P0, P1, and P2. The table given below presents the current system state.
Here, the Allocation matrix shows the current number of resources of each type allocated to each process and the
Max matrix shows the maximum number of resources of each type required by each process during its execution.
There are 3 units of type X, 2 units of type Y and 2 units of type Z still available. The system is currently in a safe
state. Consider the following independent requests for additional resources in the
current state:
Allocation Max
X Y Z X Y Z
P0 0 0 1 8 4 3
P1 3 2 0 6 2 0
P2 2 1 1 3 3 3
Q Consider a system with five processes P0 through P4 and three resource types R1 R and R3.
Resource type R1 has 10 instances, R2 has 5 instances and R3 has 7 instances. Suppose that at
time T0, the following snapshot of the system has been taken: (NET-JUNE-2014)
Assume that now the process P1 requests one additional instance of type R1 and two instances of
resource type R3. The state resulting after this allocation will be
(A) Ready state (B) Safe state (C) Blocked state (D) Unsafe state
Allocation Request Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 3 9 0 2
P3 2 1 1 2 2 2
P4 0 2 2 4 3 3
123
Q An operating system using banker’s algorithm for deadlock avoidance has ten dedicated
devices (of same type) and has three processes P1, P2 and P3 with maximum resource
requirements of 4, 5 and 8 respectively. There are two states of allocation of devices as follows:
(NET-JUNE-2013)
Which of the following is correct ?
State 1 Processes P1 P2 P3
Device 2 3 4
allocated
State 2 Processes P1 P2 P3
Device 0 2 4
allocated
(A) State 1 is unsafe and state 2 is safe.
(B) State 1 is safe and state 2 is unsafe.
(C) Both, state 1 and state 2 are safe.
Both, state 1 and state 2 are unsafe.
Q Which of the following is NOT true of deadlock prevention and deadlock avoidance
schemes?(GATE-2008) (2 Marks)
(A) In deadlock prevention, the request for resources is always granted if the resulting state is
safe
(B) In deadlock avoidance, the request for resources is always granted if the result state is safe
124
Q Consider the following snapshot of a system running n processes. Process Pi is holding Xi instances of a
resource R, 1 <= i <= n. currently, all instances of R are occupied. Further, for all i, process i has placed a
request for an additional Yi instances while holding the Xi instances it already has. There are exactly two
processes p and q such that Yp = Yq = 0. Which one of the following can serve as a necessary condition to
guarantee that the system is not approaching a deadlock? (GATE-2006) (2 Mark)
(A) min (Xp, Xq) < max (Yk) where k != p and k != q (B) Xp + Xq >= min (Yk) where k != p and k != q
(C) max (Xp, Xq) > 1 (D) min (Xp, Xq) > 1
• The set of vertices V is partitioned into two different types of nodes: P = {P1, P2, ..., Pn}, the set
consisting of all the active processes in the system, and R = {R1, R2, ..., Rm}, the set consisting of
all resource types in the system.
125
• A directed edge from process Pi to resource type Rj is denoted by Pi → Rj; it signifies that
process Pi has requested an instance of resource type Rj and is currently waiting for that
resource.
• A directed edge from resource type Rj to process Pi is denoted by Rj → Pi; it signifies that an
instance of resource type Rj has been allocated to process Pi.
126
Deadlock detection and recovery
• Here we do not check safety and where any process request for some
resources then these resources are allocated immediately, if available.
127
1- If Requesti <= Needi, go to step 2. Otherwise, raise an error condition, since the
process has exceeded its maximum claim.
3- Have the system pretend to have allocated the requested resources to process Pi
by modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If the resulting resource-allocation state is safe, the transaction is completed, and process Pi is
allocated its resources. However, if the new state is unsafe, then Pi must wait for Requesti, and
the old resource-allocation state is restored.
Deadlock detection
• Another possibility is to let the system recover from the deadlock automatically.
There are two options for breaking a deadlock.
128
• Process Termination: One is simply to abort one or more processes to break the
circular wait.
• Pre-empt Resource: The other is to pre-empt some resources from one or more
of the deadlocked processes.
Process Termination
• Abort all deadlocked processes: This method clearly will break the deadlock
cycle, but at great expense. The deadlocked processes may have computed for a
long time, and the results of these partial computations must be discarded and
probably will have to be recomputed later.
• Abort one process at a time: until the deadlock cycle is eliminated. This method
incurs considerable overhead, since after each process is aborted, a deadlock-
detection algorithm must be invoked to determine whether any processes are
still deadlocked.
• Aborting a process may not be easy. If the process was in the midst of updating
a file, terminating it will leave that file in an incorrect state.
• Similarly, if the process was in the midst of printing data on a printer, the system
must reset the printer to a correct state before printing the next job.
129
Resource Pre-emption
• 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. If preemption is required to deal with deadlocks,
then three issues need to be addressed:
• Selecting a victim Which resources and which processes are to be preempted? As in process
termination, we must determine the order of preemption to minimize cost.
• Cost factors may include such parameters as the number of resources a deadlocked process is
holding and the amount of time the process has thus far consumed.
• Rollback. If we preempt a resource from a process, what should be done with that process?
Clearly, it cannot continue with its normal execution; it is missing some needed resource.
• We must roll back the process to some safe state and restart it from that state.
• Since, in general, it is difficult to determine what a safe state is, the simplest solution is a total
rollback: abort the process and then restart it.
• Although it is more effective to roll back the process only as far as necessary to break the
deadlock, this method requires the system to keep more information about the state of all
running processes.
• Starvation. How do we ensure that starvation will not occur? That is, how can we guarantee
that resources will not always be preempted from the same process?
• In a system where victim selection is based primarily on cost factors, it may happen that the
same process is always picked as a victim.
• As a result, this process never completes its designated task, a starvation situation any
practical system must address.
• Clearly, we must ensure that a process can be picked as a victim only a (small) finite number of
times. The most common solution is to include the number of rollbacks in the cost factor.
130
Q Consider a system with 4 types of resources R1 (3 units), R2 (2 units), R3 (3 units), R4 (2 units). A non-pre-emptive resource
allocation policy is used. At any given instance, a request is not entertained if it cannot be completely satisfied. Three processes
P1, P2, P3 request the sources as follows if executed independently. (GATE-2009) (2 Marks)
Process P1: Process P2: Process P3: R1 R2 R3 R4
t=0: requests 2 units t=0: requests 2 units t=0: requests 1 unit
of R2 of R3 of R4 P1
t=1: requests 1 unit t=2: requests 1 unit t=2: requests 2 units P2
of R3 of R4 of R1
t=3: requests 2 units t=4: requests 1 unit t=5: releases 2 units P3
of R1 of R1 of R1
t=5: releases 1 unit t=6: releases 1 unit t=7: requests 1 unit
of R2 of R3 of R2 Available
and 1 unit of R1. R1 R2 R3 R4
t=7: releases 1 unit t=8: Finishes t=8: requests 1 unit
of R3 of R3
t=8: requests 2 units t=9: Finishes
of R4
t=10: Finishes
Which one of the following statements is TRUE if all three processes run concurrently starting at time t=0?
(A) All processes will finish without any deadlock
(B) Only P1 and P2 will be in deadlock.
(C) Only P1 and P3 will be in a deadlock.
(D) All three processes will be in deadlock
Ignorance
• In the absence of algorithms to detect and recover from deadlocks, we may arrive at a
situation in which the system is in a deadlocked state yet has no way of recognizing what has
happened.
• Operating System behaves like there is no concept of deadlock.
Ostrich Algorithm
131
• In this case, the undetected deadlock will cause the system’s performance to deteriorate,
because resources are being held by processes that cannot run and because more and more
processes, as they make requests for resources, will enter a deadlocked state.
• Eventually, the system will stop functioning and will need to be restarted manually.
• Although this method may not seem to be a viable approach to the deadlock problem, it is
nevertheless used in most operating systems.
• Expense is one important consideration. Ignoring the possibility of deadlocks is cheaper than
the other approaches. Since in many systems, deadlocks occur infrequently (say, once per
year), the extra expense of the other methods may not seem worthwhile.
• In addition, methods used to recover from other conditions may be put to use to recover from
deadlock. In some circumstances, a system is in a frozen state but not in a deadlocked state.
• We see this situation, for example, with a real-time process running at the highest priority (or
any process running on a nonredemptive scheduler) and never returning control to the
operating system.
• The system must have manual recovery methods for such conditions and may simply use those
techniques for deadlock recovery.
Q Consider the solution to the bounded buffer producer/consumer problem by using general semaphores S,F and E.
The semaphore S is the mutual exclusion semaphore initialized to 1. The semaphore F corresponds to the number of
free slots in the buffer and is initialized to N. The semaphore E corresponds to the number of elements in the buffer
and is initialized to 0. (GATE-2006) (2 Mark)
Which of the following interchange operations may result in a deadlock?
I) Interchanging Wait (F) and Wait (S) in the Producer process
II) Interchanging Signal (S) and Signal (F) in the Consumer process
a) (I) only b) (II) only Producer() Consumer()
c) Neither (I) nor (II) d) Both (I) and (II) { {
while(T) while(T)
{ {
Produce() wait(E)//UnderFlow
wait(F)//OverFlow wait(S)
Wait(S) pick()
append() signal(S)
signal(S) signal(F)
signal(E) consume()
} }
} }
132
Q In a certain operating system, deadlock prevention is attempted using the following scheme. Each process is
assigned a unique timestamp, and is restarted with the same timestamp if killed. Let Ph be the process holding a
resource R, Pr be a process requesting for the same resource R, and T(Ph) and T(Pr) be their timestamps
respectively. The decision to wait or preempt one of the processes is based on the following algorithm.
if T(Pr) < T(Ph)
then kill Pr
else wait
Which one of the following is TRUE? (GATE-2004) (2 Marks)
(A) The scheme is deadlock-free, but not starvation-free
(B) The scheme is not deadlock-free, but starvation-free
(C) The scheme is neither deadlock-free nor starvation-free
(D) The scheme is both deadlock-free and starvation-free
Answer: (A)
Q The following is a code with two threads, producer and consumer, that can run in parallel.
Further, S and Q are binary semaphores equipped with the standard P and V operations.
semaphore S = 1, Q = 0;
integer x;
producer: consumer:
while (true) do while (true) do
P(S); P(Q);
x = produce (); consume (x);
V(Q); V(S);
done done
Which of the following is TRUE about the program above? (GATE-2008) (2 Marks)
(A) The process can deadlock
(B) One of the threads can starve
(C) Some of the items produced by the producer may be lost
(D) Values generated and stored in ‘x’ by the producer will always be consumed before the
producer can generate a new value
133
Q Consider a system with seven processes A through G and six resources R through W. Resource
ownership is as follows : process A holds R and wants T process B holds nothing but wants T
process C holds nothing but wants S process D holds U and wants S & T process E holds T and
wants V process F holds W and wants S process G holds V and wants U Is the system deadlocked
? If yes, processes are deadlocked. (NET-AUG-2016)
a) No b) Yes, A, B, C c) Yes, D, E, G d) Yes, A, B, F
Q Consider a system with five processes P0 through P4 and three resource types R1 R and R3. Resource type R1 has 10 instances,
R2 has 5 instances and R3 has 7 instances. Suppose that at time T0, the following snapshot of the system has been taken: (NET-
JUNE-2014)
Assume that now the process P1 requests one additional instance of type R1 and two instances of resource type R3. The state
resulting after this allocation will be
(A) Ready state (B) Safe state (C) Blocked state (D) Unsafe state
Q Consider a system which have ‘n’ number of processes and ‘m’ number of
resource types. The time complexity of the safety algorithm, which checks whether
a system is in safe state or not, is of the order of: (NET-AUG-2016)
a) O(mn) b) O(m2n2) c) O(m2n) d) O(mn2)
134
Q A system has four processes and five resources. The current allocation and
maximum needs are as follows: (NET-DEC-2015)
The smallest value of x for which the above system in safe state is .
a) 1 b) 3 c) 2 d) Not safe for any value of x
Allocation Request Available
A B C D E A B C D E A B C D E
P1 1 0 2 1 1 1 1 2 1 3 0 0 X 1 1
P2 2 0 1 1 0 2 2 2 1 0
P3 1 1 0 1 0 2 1 3 1 0
P4 1 1 1 1 0 1 1 2 2 1
135
Fork
• Requirement of Fork command
• In number of applications specially in those where work is of repetitive nature, like web
server i.e. with every client we have to run similar type of code. Have to create a separate
process every time for serving a new request.
• So it must be a better solution that instead to creating a new process every time from
scratch we must have a short command using which we can do this logic.
• Idea of fork command
• Here fork command is a system command using which the entire image of the process can
be copied and we create a new process, this idea help us to complete the creation of the
new process with speed.
• After creating a process, we must have a mechanism to identify weather in newly created
process which one is child and which is parent.
136
• Implementation of fork command
• In general, if fork return 0 then it is child and if fork return 1 then it is parent, and then using a programmer
level code we can change the code of child process to behave as new process.
• Disadvantage
• To create a new process by fork command we have to do system call as, fork is system
function
• Which is slow and time taking
• Increase the burden over Operating System
• Different image of the similar type of task have same code part which means we have the
multiple copy of the same data waiting the main memory
137
Q The following C program is executed on a Unix/Linux system:
#include <unistd.h>
int main ()
{
int i ;
for (i=0; i<10; i++)
if (i%2 == 0)
fork ( ) ;
return 0 ;
}
The total number of child processes created is (GATE-2019) (1 Marks)
138
if (fork() == 0)
{
a = a + 5;
printf ("%d, %d /n", a, &a);
}
else
{
a = a –5;
printf("%d, %d /n", a, &a);
}
Let u, v be the values printed by the parent process, and x, y be the values printed
by the child process. Which one of the following is TRUE? (GATE-2005) (2 Marks)
a) u = x + 10 and v = y b) u = x + 10 and v != y
c) u + 10 = x and v = y d) u + 10 = x and v != y
139
• A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of
registers, ( and a thread ID. )
• Traditional (heavyweight) processes have a single thread of control - There is one program
counter, and one sequence of instructions that can be carried out at any given time.
• multi-threaded applications have multiple threads within a single process, each having their
own program counter, stack and set of registers, but sharing common code, data, and certain
structures such as open files.
Motivation
• Threads are very useful in modern programming whenever a process has multiple tasks to perform
independently of the others.
• This is particularly true when one of the tasks may block, and it is desired to allow the other tasks to proceed
without blocking.
• For example in a word processor, a background thread may check spelling and grammar while a foreground
thread processes user input ( keystrokes ), while yet a third thread loads images from the hard drive, and a fourth
does periodic automatic backups of the file being edited.
• Another example is a web server - Multiple threads allow for multiple requests to be satisfied simultaneously.
• Resource sharing - By default threads share common code, data, and other resources,
which allows multiple tasks to be performed simultaneously in a single address space.
140
• Economy - Creating and managing threads ( and context switches between them ) is much
faster than performing the same tasks for processes.
Q Which of the following is/are shared by all the threads in a process? (GATE -
2017) (1 Marks)
(A) I and II only (B) III only (C) IV only (D) III and IV only
141
Q A thread is usually defined as a “light weight process” because an operating system (OS)
maintains smaller data structures for a thread than for a process. In relation to this, which of the
following is TRUE? (GATE - 2011) (1 Marks)
(A) On per-thread basis, the OS maintains only CPU register state
(B) The OS does not maintain a separate stack for each thread
(C) On per-thread basis, the OS does not maintain virtual memory state
(D) On per-thread basis, the OS maintains only scheduling and accounting information
Multithreading Models
• There are two types of threads to be managed in a modern system: User threads and kernel
threads.
• User threads are supported above the kernel, without kernel support. These are the threads
that application programmers would put into their programs.
• Kernel threads are supported within the kernel of the OS itself. All modern OS support kernel
level threads, allowing the kernel to perform multiple simultaneous tasks and/or to service
multiple kernel system calls simultaneously
Many-To-One Model
• In the many-to-one model, many user-level threads
are all mapped onto a single kernel thread.
142
One-To-One Model
• The one-to-one model creates a separate kernel thread to handle each user thread.
• One-to-one model overcomes the problems listed above involving blocking system calls and the splitting of
processes across multiple CPUs.
• However, the overhead of managing the one-to-one model is more significant, involving more overhead and
slowing down the system.
• Most implementations of this model place a limit on how many threads can be created.
• Linux and Windows from 95 to XP implement the one-to-one model for threads.
Many-To-Many Model
• The many-to-many model multiplexes any number of user
threads onto an equal or smaller number of kernel threads,
combining the best features of the one-to-one and many-to-
one models.
Q User level threads are threads that are visible to the programmer and are unknown to the
kernel. The operating system kernel supports and manages kernel level threads. Three different
types of models relate user and kernel level threads. Which of the following statements is/are
true? (NET-NOV-2017)
(a) (i) The Many - to - one model maps many user threads to one kernel thread
(ii) The one - to - one model maps one user thread to one kernel thread
(iii) The many - to - many model maps many user threads to smaller or equal kernel
threads
143
(a) (i) Many - to - one model maps many kernel threads to one user thread
(ii) One - to - one model maps one kernel thread to one user thread
(iii) Many - to - many model maps many kernel threads to smaller or equal user threads
C) Both (a) and (b) are true D) Both (a) and (b) are false
Q One of the disadvantages of user level threads compared to Kernel level threads is (NET-JAN-
2017)
a) If a user level thread of a process executes a system call, all threads in that process are
blocked.
d) The library procedures invoked for thread management in user level threads are local
procedures.
(B) When a user level thread is blocked, all other threads of its process are blocked.
(C) Context switching between user level threads is faster than context switching between kernel
level threads.
Q Let the time taken to switch between user mode and kernel mode of execution be T1 while
time taken to switch between two user processes be T2. Which of the following is correct? (NET-
JUNE-2013)
a) T1 < T2
b) T1 > T2
c) T1 = T2
144
Q Let the time taken to switch between user and kernel modes of execution be t1 while the time
taken to switch between two processes be t2. Which of the following is TRUE? (GATE-2011) (1
Marks)
(A) t1 > t2
(B) t1 = t2
(C) t1 < t2
Q Consider the following statements about user level threads and kernel level threads. Which
one of the following statements is FALSE? (GATE - 2007) (1 Marks)
A) Context switch time is longer for kernel level threads than for user level threads.
Q Consider the following statements with respect to user-level threads and kernel supported
threads
i. context switch is faster with kernel-supported threads
ii. for user-level threads, a system call can block the entire process
(C) (i) and (iii) only (D) (i) and (ii) only
145
Memory Hierarchy
• Let first understand what we need from a memory
• Large capacity
• Less per unit cost
• Less access time(fast access)
Memory Hierarchy
• The memory hierarchy system consists of all storage devices
employed in a computer system from the slow but high-capacity
auxiliary memory to a relatively faster main memory, to an even
smaller and faster cache memory accessible to the high-speed
CappacriotycessPienrgunlo
itgicAccess Time
146
• If we want this hierarchy to work fine, then most of the time when CPU needs a data it must be present in
Cache, if not possible main memory, worst case Secondary memory.
• But this is a difficult task to do as my computer has, 8 TB of Secondary Memory, 32 GB of Main Memory, but
only 768KB, 4MB, 16 MB of L1, L2, L3 cache respectively.
• If somehow we can estimate what data CPU will require if future we can prefetch in Cache and Main Memory.
• The main memory occupies a central position by being able to communicate directly with the CPU.
• When programs not residing in main memory are needed by the CPU, they are brought in from auxiliary memory.
• Programs not currently needed in main memory are transferred into auxiliary memory to provide space for
currently used programs and data.
• A special very-high speed memory called a Cache is used to increase the speed of processing by making current
programs and data available to the CPU at a rapid rate.
• The cache memory is employed in computer systems to compensate for the speed differential between main
memory access time and processor logic.
Locality of Reference
• The references to memory at any given interval of time tend to be confined within a few localized areas in
memory. This phenomenon is known as the property of locality of reference. There are two types of locality of
reference
• Spatial Locality: Spatial locality refers to the use of data elements in the nearby locations.
• Temporal Locality: Temporal locality refers to the reuse of specific data, and/or resources,
within a relatively small-time duration. Or, the most frequently used items will be needed
soon. (LRU is used for temporal locality)
147
• Reason to have memory hierarchy instead of a single memory is
• Average access time (Should be less)
• Capacity (Should be high)
• Cost Per unit (Should be less)
• Deciding which processes (or parts of processes) and data to move into and
out of memory. Allocating and deallocating memory space as needed.
• Keeping track of which parts of memory are currently being used and who is
using them.
• The first duty of OS is to translate this logical address into physical address. There can be two
approaches for storing a process in main memory.
• We know that when a process is required to be executed it must be loaded to main memory,
by policy has two implications.
• It must be loaded to main memory completely for execution.
• Here we use a memory management unit(OS) whose duty is to take logical address and
translate it into physical address.
• MMU contains a relocation register, which contains the base address of the process in the
main memory and it is added in the logical address every time
148
• Advantage: - easy strategy, simple to use, simple to understand.
• So, if the value of logical address is less than limit, then it means it’s a valid request and we can
continue with translation otherwise, it is a illegal request which is immediately trapped by OS.
LA PA
0 0+r
Max Max + r
Min Min + r
Q Consider the following tables of relocation and limit registers and find the illegal attempts and
if valid find the physical address?
Limit Register Relocation Register
P0 500 1200
P1 275 550
P2 212 880
P3 420 1400
P4 118 200
Following are the request by process
P0—450
P1—300
P2—210
P3—450
149
Space Allocation Method in Contiguous Allocation
• Variable size partitioning: -In this policy, in starting, we treat the memory as a
whole or a single chunk & whenever a process request for some space, exactly
same space is allocated if possible and the remaining space can be reused again.
• Fixed size partitioning: - here, we divide memory into fixed size partitions, which
may be of different sizes, but here if a process request for some space, then a
partition is allocated entirely if possible, and the remaining space will be waisted
internally.
• First fit policy: - as the name implies, it states searching the memory from the base and will
allocate first partition which is capable enough.
150
• Best fit policy: - Here, we search the entire memory and will allocate the smallest partition
which is capable enough.
• Worst fit policy: - it also searches the entire memory and allocate the largest partition
possible.
• Advantage: - perform best in variable size partitioning
• Disadvantage: - perform worst in fix size partitioning, resulting into large internal
fragmentation.
• Next fit policy: - next fit is the modification in the best fit where, after satisfying a request, we
start satisfying next request from the current position.
151
Q Consider six memory partitions of size 200 KB, 400 KB, 600 KB, 500 KB, 300 KB, and 250 KB, where KB refers to
kilobyte. These partitions need to be allotted to four processes of sizes 357 KB, 210 KB, 468 KB and 491 KB in that
order. If the best fit algorithm is used, which partitions are NOT allotted to any process? (GATE-2015) (2 Marks)
(A) 200 KB and 300 KB (B) 200 KB and 250 KB (C) 250 KB and 300 KB (D) 300 KB and 400 KB
Q Consider the following heap (figure) in which blank regions are not in use and hatched region
are in use. (GATE-1994) (2 Marks)
The sequence of requests for blocks of size 300, 25, 125, 50 can be satisfied if we use
(a) either first fit or best fit policy (any one) (b) first fit but not best fit policy
(c) best fit but first fit policy (d) None of the above
152
Q Given memory partitions of 100 K, 500 K, 200 K, 300 K and 600 K (in order) and
processes of 212 K, 417 K, 112 K, and 426 K (in order), using the first-fit algorithm,
in which partition would the process requiring 426 K be placed ? (NET-DEC-2012)
(A) 500 K (B) 200 K (C) 300 K (D) 600 K
Q A 1000 Kbyte memory is managed using variable partitions but No compaction. It currently has
two partitions of sizes 200 Kbytes and 260 Kbytes respectively. The smallest allocation request in
Kbytes that could be denied is for? (GATE-1996) (1 Marks)
(a) 151 (b) 181 (c) 231 (d) 541
153
• How can we solve external fragmentation?
• We can also swap processes in the main memory after fixed intervals of time
& they can be swapped in one part of the memory and the other part become
empty(Compaction, defragmentation). This solution is very costly in respect
to time as it will take a lot of time to swap process when system is in running
state.
Code :
(a) (a) and (b) only (b) (a) and (c) only
(c) (b) and (c) only (d) (a), (b) and (c)
154
• Secondary memory is divides into fixed size partition(because management is easy) all of them
of same size called pages(easy swapping and no external fragmentation).
• Main memory is divided into fix size partitions (because management is easy), each of them
having same size called frames(easy swapping and no external fragmentation).
• In general number of pages are much more than number of frames (approx. 128 time)
Translation process
3. Page table base register(PTBR) provides the base of the page table and then the
corresponding page no is accessed using p.
4. Here we will finds the corresponding frame no (the base address of that frame in main
memory in which the page is stored)
5. Combine corresponding frame no with the instruction offset and get the physical address.
Which is used to access main memory.
155
Page Table
3. Number of entries a process have in the page table is the number of pages a process have in
the secondary memory.
4. Size of each entry in the page table is same it is corresponding frame number.
Advantage
Disadvantage
• Translation process is slow as Main Memory is accessed two times(one for page
table and other for actual access).
156
Q Page information in memory is also called as Page Table. The essential contents
in each entry of a page table is/are . (NET-JULY-2018)
(a) Page Access information
Q The essential content(s) in each entry of a page table is / are? (GATE-2009) (1 Marks)
(A) Virtual page number (B) Page frame number
(C) Both virtual page number and page frame number (D) Access right information
-
-
-
-
-
-
-
157
No of Locations n
Address Length in bits Upper Bound(Log2n)
-
-
-
-
-
-
-
158
No of Locations n
Address Length in bits Upper Bound(Log2n)
159
S No SM LA MM PA p f d addressable Page
Size
1 32 GB 128 MB 1B 1KB
2 42 33 11 1B
3 512GB 31 1B 512B
4 128GB 32GB 30 1B
5 28 14 4096B
160
Q A Computer system implements 8 kilobyte pages and a 32-bit physical address space. Each
page table entry contains a valid bit, a dirty bit three permission bits, and the translation. If the
maximum size of the page table of a process is 24 megabytes, the length of the virtual address
supported by the system is bits? (GATE-2015) (2 Marks)
Q A Computer system implements 8 kilobyte pages and a 32-bit physical address space. Each
page table entry contains a valid bit, a dirty bit three permission bits, and the translation. If the
maximum size of the page table of a process is 24 megabytes, the length of the virtual address
supported by the system is bits? (GATE-2015) (2 Marks)
161
Q A computer system supports 32-bit virtual address as well as 32-bit physical addresses.
Since the virtual address space is of same size as that of physical address space, if we
want to get rid of virtual memory, which one of the following is true? (NET-JUNE-2012)
(A) Efficient implementation of multiuser support is no longer possible.
Q Consider a logical address space of 8 pages of 1024 words mapped with memory
of 32 frames. How many bits are there in the physical address? (NET-DEC-2011)
(A) 9 bits (B) 11 bits (C) 13 bits (D) 15 bits
Q Consider a machine with 64 MB physical memory and a 32-bit virtual address space. If the
page size is 4KB, what is the approximate size of the page table? (GATE-2001) (2 Mark)
(a) 16 MB (b) 8 MB (c) 2 MB (d) 24 MB
162
S No SM LA MM PA p f d addressable Process PT Size
Size
1 16 GB 26 10 1B 2MB
2 44 36 12 1B 1GB
3 39 28 8 1B 512MB
• Each entry in the TLB consists of two parts: a key (Page no) and a value (frame no). When the
associative memory is search for page no, the page no is compared with all page no simultaneously.
If the item is found, the corresponding frame no field is returned.
• The search is fast; the hardware, however, is expensive, TLB Contains the frequently referred page
numbers and corresponding frame number.
• The TLB is used with page tables in the following way. The TLB contains only a few of the page-
table entries. When a logical address is generated by the CPU, its page number is presented to the
TLB. If the page number is found, its frame number is immediately available and is used to access
memory.
• If the page number is not in the TLB (known as a TLB Miss), then a memory reference to the page
table must be made.
• Also we add the page number and frame number to the TLB, so that they will be found quickly on
the next reference.
163
• If the TLB is already full of entries, the operating system must select one for replacement i.e. Page
replacement policies.
• The percentage of times that a particular page number is found in the TLB is called the Hit Ratio.
Disadvantage of TLB:
• TLB can hold the data of one process at a time and in case of multiple context switches TLB will be
required to flush frequently.
Solution:
• Use multiple TLB’s but it will be costly.
• Some TLBs allow certain entries to be wired down, meaning that they cannot be removed from the
TLB. Typically, TLB entries for kernel code are wired down.
Q Assume that in a certain computer, the virtual addresses are 64 bits long and the physical
addresses are 48 bits long. The memory is word addressable. The page size is 8kB and the word
size is 4 bytes. The Translation Look-aside Buffer (TLB) in the address translation path has 128
valid entries. At most how many distinct virtual addresses can be translated without any TLB
miss? (GATE-2019) (2 Marks)
(A) 16 x 210 (B) 8 x 220 (C) 4 x 220 (D) 256 x 210
164
Q A computer system implements a 40-bit virtual address, page size of 8 kilobytes,
and a 128-entry translation look-aside buffer (TLB) organized into 32 sets each
having four ways. Assume that the TLB tag does not store any process id. The
minimum length of the TLB tag in bits is (GATE-2015) (2 Marks)
(A) 20 (B) 10 (C) 11 (D) 22
Q Consider a paging hardware with a TLB. Assume that the entire page table and
all the pages are in the physical memory. It takes 10 milliseconds to search the TLB
and 80 milliseconds to access the physical memory. If the TLB hit ratio is 0.6, the
effective memory access time (in milliseconds) is . (CS-2014) (2 Marks)
165
Q Consider a paging hardware with a TLB. Assume that the entire page table and
all the pages are in the physical memory. It takes 10 milliseconds to search the TLB
and 80 milliseconds to access the physical memory. If the TLB hit ratio is 0.6, the
effective memory access time (in milliseconds) is . (NET-JUNE-2014)
(A) 120 (B) 122 (C) 124 (D) 118
Q The hit ratio of a Translation Look Aside Buffer (TLAB) is 80%. It takes 20
nanoseconds (ns) to search TLAB and 100 ns to access main memory. The effective
memory access time is . (NET-SEP-2013)
(A) 36 ns (B) 140 ns (C) 122 ns (D) 40 ns
(B) a cache-memory in which item to be searched is compared with all the keys simultaneously.
(C) an associative memory in which item to be searched is compared one-by-one with the keys.
(D) an associative memory in which item to be searched is compared with all the keys
simultaneously.
166
Q A paging system tlb takes 10ns main memory takes 50ns what is effective
memory access time if tlb hit ratio is 90%? (GATE-2008) (1 Marks)
a) 54 b) 60 c) 65 d) 75
Q A CPU generates 32-bit virtual addresses. The page size is 4 KB. The processor
has a translation look-aside buffer (TLB) which can hold a total of 128-page table
entries and is 4-way set associative. The minimum size of the TLB tag is? (GATE-
2006) (2 Marks)
(A) 11 bits (B) 13 bits (C) 15 bits (D) 20 bits
Size of Page
• If we increase the size of page table then internal fragmentation increase but size of page
table decreases.
• If we decrease the size of page then internal fragmentation decrease but size of page table
increases.
• So we have to find what should be the size of the page, where both cost are minimal.
Q For the implementation of a paging scheme, suppose the average process size be x bytes, the
page size be y bytes, and each page entry requires z bytes. The optimum page size that minimizes
the total overhead due to the page table and the internal fragmentation loss is given by (NET-
DEC-2014)
A) x/2 b) xz/2 c) root(2xz) d) root(xz)/2
167
Multilevel Paging / Hierarchical Paging
• Modern systems support a large logical address space (2^32 to 2^64).
• In such cases, the page table itself becomes excessively large and can contain millions of entries and can take a
lot of space in memory, so cannot be accommodated into a single frame.
• A simple solution to this is to divide page table into smaller pieces.
• One way is to use a two-level paging algorithm, in which the page table itself is also paged.
168
Q A multilevel page table is preferred in comparison to a single level page table for translating
virtual address to physical address because (GATE-2009) (1 Marks)
(A) It reduces the memory access time to read or write a memory location.
(B ) It helps to reduce the size of page table needed to implement the virtual address space of a
process.
(C) It is required by the translation lookaside buffer.
(D) It helps to reduce the number of page faults in page replacement algorithms.
Q A processor uses 36-bit physical addresses and 32-bit virtual addresses, with a page frame size of 4 Kbytes. Each
page table entry is of size 4 bytes. A three level page table is used for virtual to physical address translation, where
the virtual address is used as follows • Bits 30-31 are used to index into the first level page table • Bits 21-29 are
used to index into the second level page table • Bits 12-20 are used to index into the third level page table, and •
Bits 0-11 are used as offset within the page The number of bits required for addressing the next level page table (or
page frame) in the page table entry of the first, second and third level page tables are respectively (GATE-2008) (2
Marks)
(A) 20, 20 and 20 (B) 24, 24 and 24 (C) 24, 24 and 20 (D) 25, 25 and 24
Q In a system with 32 bit virtual addresses and 1 KB page size, use of one-level
page tables for virtual to physical address translation is not practical because of
(GATE-2003) (1 Marks)
(A) the large amount of internal fragmentation
(B) the large amount of external fragmentation
(C) the large memory overhead in maintaining page tables
(D) the large computation overhead in the translation process
169
Segmentation
• Paging is unable to separate the user's view of
memory from the actual physical memory.
• Segmentation is a memory-management scheme
that supports this user view of memory.
• A logical address space is a collection of segments.
• Each segment has a name and a length. The
addresses specify both the segment name and the
offset within the segment. The user therefore
specifies each address by two quantities: a segment
name and an offset.
• Thus, a logical address consists of a two tuple:
• <segment-number, offset>.
• Segments can be of variable lengths unlike pages
and are stored in main memory.
170
Segmentation with Paging
• Since segmentation also suffers from external fragmentation, it is better to divide the segments into pages as
the segments size increases.
• In segmentation with paging scheme a process is divided into segments and further the segments are divided
into pages.
• One can argue it is segmentation with paging is quite similar to multilevel paging, but actually it is better,
because here when page table is divided the size of partition can be different (as actually the size of different
chapters can be different).
Q A computer uses 46–bit virtual address, 32–bit physical address, and a three–level paged page table organization. The page
table base register stores the base address of the first–level table (T1), which occupies exactly one page. Each entry of T1 stores
the base address of a page of the second–level table (T2). Each entry of T2 stores the base address of a page of the third–level
table (T3). Each entry of T3 stores a page table entry (PTE). The PTE is 32 bits in size. The processor used in the computer has a 1
MB 16 way set associative virtually indexed physically tagged cache. The cache block size is 64 bytes.
What is the size of a page in KB in this computer? (GATE-2013) (2 Marks)
(A) 2 (B) 4 (C) 8 (D) 16
171
Virtual Memory
• One important goal in now-a-days computing environment is to keep many processes in
memory simultaneously to follow multiprogramming, and use resources efficiently especially
main memory.
• Pure Demand Paging/Demand Paging: We can start executing a process with no pages in
memory. When the operating system sets the instruction pointer to the first instruction of the
process, which is on a non-memory-resident page, the process immediately faults for the
page.
• After this page is brought into memory, the process continues to execute, faulting as
necessary until every page that it needs is in memory. At that it can execute with no more
faults. This scheme is Pure Demand Paging: never bring a page into memory until it is
required.
Advantage
• A program would no longer be constrained by the amount of physical memory
that is available, Allows the execution of processes that are not completely in
main memory, i.e. process can be larger than main memory.
• More programs could be run at the same time as use of main memory is less.
• Less I/O would be needed to load or swap user programs into memory, so each
user program would run faster.
• Virtual memory also allows processes to share files easily and to implement
shared memory.
Disadvantages
• Virtual memory is not easy to implement.
• It may substantially decrease performance if it is used carelessly (Thrashing)
Q Which of the following memory allocation scheme suffers from
external fragmentation? (NET-DEC-2012)
(A) Segmentation (B) Pure demand paging
(C) Swapping (D) Paging
173
Q Which of the following statements is false? (GATE-2001) (1 Marks)
(A) Virtual memory implements the translation of a program‘s address space into
physical memory address space
(B) Virtual memory allows each program to exceed the size of the primary memory
(C) Virtual memory increases the degree of multiprogramming
(D) Virtual memory reduces the context switching overhead
Basic Concepts
• When a process is started execution, no page is
loaded into main memory before demand so, it
avoids reading into memory pages that will not be
used anyway, decreasing the swap time and the
amount of physical memory needed.
• This time, however, when this bit is set to "valid” the associated page is both legal and in memory.
If the bit is set to "invalid” the page either is not valid (that is, not in the logical address space of the
process) or is valid but is currently on the disk.
• The page-table entry for a page that is brought into memory is set as usual but the page-table entry
for a page that is not currently in memory is either simply marked invalid.
174
Page Fault: - When a process tries to access a
page that is not in main memory then a Page
Fault Occurs.
• When the disk read is complete, we modify the internal table kept with the
process and the page table to indicate that the page is now in memory.
• We restart the instruction that was interrupted by the trap. The process can now
access the page as though it had always been in memory.
• A crucial requirement for demand paging is the ability to restart any instruction
after a page fault. Because we save the state (registers, condition code, instruction
counter) of the interrupted process when a page fault occurs, we must be able to
restart the process in exactly the same place and state.
175
Performance of Demand Paging
• Effective Access time for Demand Paging:
• (1 - p) x ma + p x page fault service time.
• Here, p: Page fault rate or probability of a page fault.
• ma is memory access time.
Q In a paged memory, the page hit ratio is 0.40. The time required to access a page
in secondary memory is equal to 120 ns. The time required to access a page in
primary memory is 15 ns. The average time required to access a page is .
(NET-JULY-2018)
(a) 105 (b) 68 (c) 75 (d) 78
Q Consider a process executing on an operating system that uses demand paging. The average
time for a memory access in the system is M units if the corresponding memory page is available
in memory, and D units if the memory access causes a page fault. It has been experimental
measured that the average time taken for a memory access in the process is X units.
Which one of the following is the correct expression for the page fault rate experienced by the
process? (GATE-2018) (2 Marks)
(A) (D – M) / (X – M)
(B) (X – M) / (D – M)
(C) (D – X) / (D – M)
(D) (X – M) / (D – X)
Q Suppose that the number of instructions executed between page fault is directly proportional
to the number of page frames allocated to a program. If the available memory is doubled, the
mean interval between page faults is also doubled. Further, consider that a normal instruction
takes one microsecond, but if a page fault occurs, it takes 2001 microseconds. If a program takes
60 sec to run, during which time it gets 15,000 page faults, how long would it take to run if twice
as much memory were available? (NET-DEC-2015)
A) 60 sec b) 30 sec c) 45 sec d) 10 sec
176
Q In a demand paging memory system, page table is held in registers. The time taken to
service a page fault is 8 msec. if an empty frame is available or if the replaced page is not
modified, and it takes 20 msec., if the replaced page is modified. What is the average
access time to service a page fault assuming that the page to be replaced is modified 70
of the time? (NET-DEC-2014)
(A) 11.6 msec. (B) 16.4 msec. (C) 28 msec. (D) 14 msec.
Q Let the page fault service time be 10ms in a computer with average memory
access time being 20ns. If one-page fault is generated for every 10^6 memory
accesses, what is the effective access time for the memory? (GATE-2011) (1 Marks)
(A) 21ns (B) 30ns (C) 23ns (D) 35ns
177
Q Suppose the time to service a page fault is on the average 10 milliseconds, while
a memory access takes 1 microsecond. Then a 99.99% hit ratio results in average
memory access time of? (GATE-2000) (1 Marks).
(A)1.9999 milliseconds (B)1 millisecond
(C) 9.999 microseconds (D)1.9999 microseconds
Page Replacement
• With the increase in multiprogramming each
process will get less amount of space in main
memory so, the rate of page faults may rise.
• If no frames are free, two-page transfers (one out and one in) are required. This situation
effectively doubles the page-fault service time and increases the effective access time accordingly.
• We can reduce this overhead by using a Modify bit or Dirty Bit. When this scheme is used, each
page or frame has a modify bit associated with it in the hardware.
• The modify bit for a page is set by the hardware whenever any word or byte in the page is written
into, indicating that the page has been modified.
• If the bit is set: the page has been modified since it was read in from the disk. In this case, we
must write the page to the disk.
• If the modify bit is not set: however, the page has not been modified since it was read into
memory. In this case, we need not write the memory page to the disk: it is already there.
• Now, we must solve two major problems to implement demand paging: We must be able to
develop a frame allocation algorithm and a page replacement algorithm.
Q A virtual memory has a page size of 1K words. There are eight pages and four blocks. The
associative memory page table contains the following entries:
Which of the following list of virtual addresses (in decimal) will not cause any page fault if
referenced by the CPU ? (NET-DEC-2015)
a) 1024, 3072, 4096, 6144 b) 1234, 4012, 5000, 6200
179
• The FIFO page-replacement algorithm is easy to understand and program. However, its
performance is not always good.
• Belady’s Anomaly: for some page-replacement algorithms, the page-fault rate may increase
as the number of allocated frames increases.
Q. In which one of the following page replacement algorithms it is possible for the
page fault rate to increase even when the number of allocated frames increases?
(GATE-2016) (1 Marks)
(a) LRU (Least Recently Used (b) OPT (Optimal Page Replacement)
(c) MRU (Most Recently Used) (d) FIFO (First In First Out)
180
Q A system uses FIFO policy for page replacement. It has 4-page frames with no
pages loaded to begin with. The system first accesses 100 distinct pages in some
order and then accesses the same 100 pages but now in the reverse order. How
many page faults will occur? (GATE-2010) (1 Marks)
(A) 196 (B) 192 (C) 197 (D) 195
Q A virtual memory system uses First In First Out (FIFO) page replacement policy
and allocates a fixed number of frames to a process. Consider the following
statements: (GATE-2007) (2 Marks)
P: Increasing the number of page frames allocated to a process sometimes
increases the page fault rate.
Q: Some programs do not exhibit locality of reference. Which one of the following is
TRUE?
(A) Both P and Q are true, and Q is the reason for P
(B) Both P and Q are true, but Q is not the reason for P.
(C) P is false, but Q is true
(D) Both P and Q are false.
181
Q A program has five virtual pages, numbered from 0 to 4. If the pages
are referenced in the order 012301401234, with three-page frames, the
total number of page faults with FIFO will be equal to: (NET-DEC-2007)
(A) 0 (B) 4 (C) 6 (D) 9
Q Consider a virtual memory system with FIFO page replacement policy. For an
arbitrary page access pattern, increasing the number of page frames in main
memory will (GATE-2001) (1 Marks)
(A) always decrease the number of page faults
(B) always increase the number of page faults
(C) sometimes increase the number of page faults
(D) never affect the number of page faults
182
Q Assume that there are 3 page frames which are initially empty. If the page
reference string is 1, 2, 3, 4, 2, 1, 5, 3, 2, 4, 6, the number of page faults using the
optimal replacement policy is . (GATE-2014) (2 Marks)
Q A process has been allocated 3-page frames. Assume that none of the pages of the
process are available in the memory initially. The process makes the following sequence
of page references (reference string): 1, 2, 1, 3, 7, 4, 5, 6, 3, 1. If optimal page
replacement policy is used, how many page faults occur for the above reference string?
(GATE-2007) (2 Marks)
(A) 7 (B) 8 (C) 9 (D) 10
Q The optimal page replacement algorithm will select the page that (GATE-2002) (1
Marks)
(A) Has not been used for the longest time in the past.
(B) Will not be used for the longest time in the future.
(C) Has been used least number of times.
(D) Has been used most number of times.
• LRU and OPT belong to a class of page-replacement algorithms, called stack algorithms and
can never exhibit Belady's anomaly.
• A stack algorithm is an algorithm for which it can be shown that the set of pages in memory
for n frames is always a subset of the set of pages that would be in memory with n + 1 frames.
183
Q Consider a virtual page reference string 1, 2, 3, 2, 4, 2, 5, 2, 3, 4.
Suppose LRU page replacement algorithm is implemented with 3 page
frames in main memory. Then the number of page faults are .
(NET-JULY-2018)
(a) 5 (b) 7 (c) 9 (d) 10
Q Recall that Belady’s anomaly is that the page-fault rate may increase as the number of
allocated frames increases. Now, consider the following statements:
S1: Random page replacement algorithm (where a page chosen at random is replaced) suffers
from Belady’s anomaly
S2: LRU page replacement algorithm suffers from Belady’s anomaly
Which of the following is CORRECT? (GATE-2017) (1 Marks)
(a) S1 is true, S2 is true (b) S1 is true, S2 is false
184
Q Suppose that the virtual Address space has eight pages and physical memory
with four page frames. If LRU page replacement algorithm is used, number
of page faults occur with the reference string.
0 2 1 3 5 4 6 3 7 4 7 3 3 5 5 3 1 1 1 7 2 3 4 1 (NET-AUG-2016)
(A) 13 (B) 12 (C) 11 (D) 10
Q Consider a main memory with five page frames and the following sequence of page
references: 3, 8, 2, 3, 9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3. Which one of the following is true with respect
to page replacement policies First-In-First Out (FIFO) and Least Recently Used (LRU)? (GATE-
2015) (2 Marks)
(A) Both incur the same number of page faults
(B) FIFO incurs 2 more-page faults than LRU
(C) LRU incurs 2 more-page faults than FIFO
(D) FIFO incurs 1 more page faults than LRU
Q A LRU page replacement is used with four page frames and eight pages.
How many page faults will occur with the reference string 0172327103 if the
four frames are initially empty? (NET-JUNE-2015)
a) 6 b) 7 c) 8 d) 5
Q A system uses 3-page frames for storing process pages in main memory. It uses the Least
Recently Used (LRU) page replacement policy. Assume that all the page frames are initially
empty. What is the total number of page faults that will occur while processing the page
reference string given below? 4, 7, 6, 1, 7, 6, 1, 2, 7, 2 (GATE-2014) (2 Marks)
185
Q Consider a program that consists of 8 pages (from 0 to 7) and we have 4 page frames in the physical
memory for the pages. The page reference string is : 1 2 3 2 5 6 3 4 6 3 7 3 1 5 3 6 3 4 2 4 3 4 5 1. The
number of page faults in LRU and optimal page replacement algorithms are respectively (without
including initial page faults to fill available page frames with pages) : (NET-JUNE-2014)
(A) 9 and 6 (B) 10 and 7 (C) 9 and 7 (D) 10 and 6
Q A job has four pages A, B, C, D and the main memory has two page frames only. The job needs
to process its pages in following order : ABACABDBACD. Assuming that a page interrupt occurs
when a new page is brought in the main memory, irrespective of whether the page is swapped
out or not. The number of page interrupts in FIFO and LRU page replacement algorithms are
(NET-JUNE-2013)
(A) 9 and 7 (B) 7 and 6 (C) 9 and 8 (D) 8 and 6
• Counters. In the simplest case, we associate with each page-table entry a time-of-use field
and add to the CPU a logical clock or counter.
• The clock is incremented for every memory reference. Whenever a reference to a page is
made, the contents of the clock registers are copied to the time-of-use field in the page-table
entry for that page.
• Whenever a page is referenced, it is removed from the stack and put on the top.
• In this way, the most recently used page is always at the top of the stack and the least
recently used page is always at the bottom.
187
Counting-Based Page Replacement
• We keep a counter of the number of references that have been made to each page and
develop the following two schemes.
• The least frequently used (LFU) page-replacement algorithm requires that the page with the
smallest count be replaced.
• The reason for this selection is that an actively used page should have a large reference count.
• A problem arises, however, when a page is used heavily during the initial phase of a process
but then is never used again. Since it was used heavily, it has a large count and remains in
memory even though it is no longer needed.
• One solution is to shift the counts right by 1 bit at regular intervals, forming an exponentially
decaying average usage count.
• The most frequently used (MFU) page-replacement algorithm is based on the argument that
the page with the smallest count was probably just brought in and has yet to be used.
• Neither MFU nor LFU replacement is common. The implementation of these algorithms is
expensive, and they do not approximate OPT replacement well.
Q Consider a computer system with ten physical page frames. The system is provided with an
access sequence (a1,a2,…,a20,a1,a2,…,a20), where each ai is a distinct virtual page number. The
difference in the number of page faults between the last-in-first-out page replacement policy
and the optimal page replacement policy is . (GATE-2016) (1 Marks)
Q Consider a main memory with 3 page frames for the following page reference string : 5, 4, 3, 2,
1, 4, 3, 5, 4, 3, 4, 1, 4. Assuming that the execution of process is initiated after loading page 5 in
memory, the number of page faults in FIFO and second chance replacement respectively are
(NET-SEP-2013)
(A) 8 and 9 (B) 10 and 11 (C) 7 and 9 (D) 9 and 8
188
Q A computer has twenty physical page frames which contain pages numbered 101 through 120.
Now a program accesses the pages numbered 1, 2, …, 100 in that order, and repeats the access
sequence THRICE. Which one of the following page replacement policies experiences the same
number of page faults as the optimal page replacement policy for this program? (GATE-2014) (2
Marks)
(A) Least-recently-used (B) First-in-first-out
(C) Last-in-first-out (D) Most-recently-used
(C) Fewer page faults occur (D) Fewer segmentation faults occur
• Weighted / Proportional Allocation: Frames will be allocated according to the weights of the
process. The allocation of frames per process: a(i)= s(i)/S x m.
• Where, a(i) is the number of allocated frames, s(i) is the process and S is the size of the virtual
memory in general, S = Σ s(i), and m is the total number of frames available.
• Example: If we have 3 processes of size 20 k, 30k and 50 k then 30 frames will be allocated as:
• P1 gets: 20/100*30 = 6 frames, similarly P2 and P3 will get 9 and 15 frames respectively.
Local Replacement: Local replacement requires that each process select from only its own set of
allocated frames.
• With a local replacement strategy, the number of frames allocated to a process does not
change.
• Under local replacement, the set of pages in memory for a process is affected by the paging
behaviour of only that process.
• Local replacement might hinder a process, however, by not making available to it other, less
used pages of memory.
Thrashing
• A process is thrashing if it is spending more time paging than executing. High
paging activity is called Thrashing.
190
Causes of Thrashing
• High Degree of Multiprogramming: If CPU utilization is too low, we increase the degree of
multiprogramming by introducing a new process to the system.
• A global page-replacement algorithm is used; it replaces pages without regard to the process
to which they belong. Now if that a process enters a new phase in its execution and needs
more frames. It will start faulting and taking frames away from other processes.
• These processes need those pages, however, and so they also fault, taking frames from other
processes. These faulting processes must use the paging device to swap pages in and out. As
they queue up for the paging device, the ready queue empties. As processes wait for the
paging device, CPU utilization decreases.
• The CPU scheduler sees the decreasing CPU utilization and increases the degree of multiprogramming
as a result.
• The new process tries to get started by taking frames from running processes, causing more page
faults and a longer queue for the paging device. As a result, CPU utilization drops even further, and the
CPU scheduler tries to increase the degree of multiprogramming even more.
•
• Thrashing has occurred, and system throughput plunges. The page fault rate increases tremendously.
As a result, the effective memory-access time increases. No work is getting done, because the
processes are spending all their time paging.
Solution
• With local replacement, if one process starts thrashing, it cannot steal frames from another
process and cause the latter to thrash as well.
• To prevent thrashing, we must provide a process with as many frames as it needs.
• We decide how to allocate the number of frames to a process by the working-set strategy.
• The locality model states that, as a process executes, it moves from locality to locality. A
locality is a set of pages that are actively used together
191
Working Set Model
• This model uses a parameter Δ, to define the working set window.
• The set of pages in the most recent Δ page references is the working set.
• If a page is in active use, it will be in the working set. If it is no longer being used, it will drop
from the working set Δ time units after its last reference.
• The accuracy of the working set depends on the selection of Δ . If Δ is too small, it will not
encompass the entire locality; if Δ is too large, it may overlap several localities.
• If we can calculate the Working-Set Size (WSS) for each process then: D = Σ WSSi, where D is D
is the total demand for frames.
• If the total demand is greater than the total number of available frames (D > m), thrashing will
occur, because some processes will not have enough frames.
Page-Fault Frequency
• It is a much more direct strategy to control Page-Fault Frequency.
• Thrashing has a high page-fault rate and thus we want to control the page-fault rate.
• When it is too high, we know that the process needs more frames. Conversely, if the page-
fault rate is too low, then the process may have too many frames.
192
Q Working set model is used in memory management to implement the concept of
(NET-JUNE-2013)
(A) Swapping (B) Principal of Locality
(C) Segmentation (D) Thrashing
Disk scheduling
• One of the responsibilities of the operating system is to use the hardware efficiently. For the
disk drives, efficiency means having fast access time and large disk bandwidth.
• The disk bandwidth is the total number of bytes transferred, divided by the total time
between the first request for service and the completion of the last transfer. We can improve
both the access time and the bandwidth by managing the order in which disk I/O requests are
serviced.
193
Whenever a process needs I/O to or from the disk, it issues a system call to the operating system.
The request specifies several pieces of information:
• Whether this operation is input or output
• What is the disk address for the transfer
• What is the memory address for the transfer
• What is the number of sectors to be transferred
• If the desired disk drive and controller are available, the request can be serviced immediately.
• If the drive or controller is busy, any new requests for service will be placed in the queue of
pending requests for that drive.
• Thus, when one request is completed, the operating system chooses which pending request to
service next. How does the operating system make this choice? Any one of several disk-
scheduling algorithms can be used.
FCFS (First Come First Serve)
The simplest form of disk scheduling is, of course, the first-come, first-served (FCFS) algorithm. In
FCFS, the requests are addressed in the order they arrive in the disk queue. This algorithm is
intrinsically fair, but it generally does not provide the fastest service.
Advantages:
• Easy to understand easy to use
• Every request gets a fair chance
• no starvation (may suffer from convoy effect)
Disadvantages:
• Does not try to optimize seek time (extra seek movements)
• May not provide the best possible service
Q If the disk head is located initially at 32, find the number of disk moves required
with FCFS if the disk queue of I/O blocks requests are 98, 37, 14, 124, 65, 67. (NET-
DEC-2012)
(A) 239 (B) 310 (C) 321 (D) 325
Q Assuming that the disk head is located initially at 32, find the number of disk
moves required with FCFS if the disk queue of I/O block requests are 98, 37, 14,
124, 65, 67: (NET-JUNE-2013)
(A) 310 (B) 324 (C) 320 (D) 321
194
Q If the Disk head is located initially at track 32, find the number of disk moves
required with FCFS scheduling criteria if the disk queue of I/O blocks requests are:
98, 37, 14, 124, 65, 67 (NET-JULY-2016)
a) 320 b) 322 c) 321 d) 319
SSTF Scheduling
• It seems reasonable to service all the requests close to the current head position before
moving the head far to service other REQUESTS. This assumption is the basis for the SSTF
algorithm.
• In SSTF (Shortest Seek Time First), the request nearest to the disk arm will get executed first
i.e. requests having shortest seek time are executed first.
• Although the SSTF algorithm is a substantial improvement over the FCFS algorithm, it is not
optimal. In the example, we can do better by moving the head from 53 to 37, even though the
latter is not closest, and then to 14, before turning around to service 65, 67, 98, 122, 124, and
183. This strategy reduces the total head movement to 208 cylinders.
Advantages:
• Seek movements decreases
• Average Response Time decreases
• Throughput increases
Disadvantages:
• Overhead to calculate the closest request.
• Can cause Starvation for a request which is far from the current location of the header
• High variance of response time as SSTF favours only some requests
• SSTF scheduling is essentially a form of shortest-job-first (SJF) scheduling; and like SJF
scheduling, it may cause starvation of some requests.
Q Consider a disk system with 100 cylinders. The requests to access the cylinders occur in
following sequence 4, 34, 10, 7, 19, 73, 2, 15, 6, 20. Assuming that the head is currently at
cylinder 50, what is the time taken to satisfy all requests if it takes 1ms to move from one
cylinder to adjacent one and shortest seek time first policy is used? (GATE-2009) (1 Marks)
(A) 95 ms (B) 119 ms (C) 233 ms (D) 276 ms
195
Q Suppose a disk has 201 cylinders, numbered from 0 to 200. At some time, the disk arm is at
cylinder 100, and there is a queue of disk access requests for cylinders 30, 85, 90, 100, 105, 110,
135 and 145. If Shortest-Seek Time First (SSTF) is being used for scheduling the disk access, the
request for cylinder 90 is serviced after servicing number of requests. (GATE-
2014) (1 Marks)
Q Consider an imaginary disk with 40 cylinders. A request come to read a block on cylinder 11.
While the seek to cylinder 11 is in progress, new requests come in for cylinders 1, 36, 16, 34, 9
and 12 in that order. The number of arm motions using shortest seek first algorithm is (NET-DEC-
2014)
(A) 111 (B) 112 (C) 60 (D) 61
Q Consider a disk queue with requests for I/O to blocks on cylinders 98, 183, 37, 122, 14, 124, 65,
67. Suppose SSTF disk scheduling algorithm implemented to meet the requests then the total
number of head movements are if the disk head is initially at 53. (NET-NOV-2017)
a) 224 b) 248 c) 236 d) 240
Q A disk drive has 100 cylinders, numbered 0 to 99. Disk requests come to the disk driver
for cylinders 12, 26, 24, 4, 42, 8 and 50 in that order. The driver is currently serving a
request at cylinder 24. A seek takes 6 msec per cylinder moved. How much seek time is
needed for shortest seek time first (SSTF) algorithm? (NET-JUNE-2015)
a) 0.984 sec b) 0.396 sec c) 0.738 sec d) 0.42 sec
196
SCAN
The disk arm starts at one end of the disk and moves towards the other end, servicing requests as it reaches each
track, until it gets to the other end of the disk. At the other end, the direction of head movement is reversed, and
servicing continues. The head continuously scans back and forth across the disk. The SCAN algorithm is sometimes
called the Elevator algorithm, since the disk arm behaves just like an elevator in a building, first servicing all the
requests going up and then reversing to service requests the other way.
• If a request arrives in the queue just in front of the head, it will be serviced almost immediately; a request
arriving just behind the head will have to wait until the arm moves to the end of the disk, reverses direction, and
comes back.
• Assuming a uniform distribution of requests for cylinders, consider the density of requests when the head
reaches one end and reverses direction. At this point, relatively few requests are immediately in front of the
head since these cylinders have recently been serviced. The heaviest density of requests is at the other end of
the disk These requests have also waited the longest.
Advantages:
• Simple easy to understand and use
• No starvation but more wait for some random process
• Low variance and Average response time
Disadvantages:
• Long waiting time for requests for locations just visited by disk arm.
• Unnecessary move to the end of the disk, even if there is no request.
Q On a disk with 1000 cylinders (0 to 999) find the number of tracks, the disk arm must move to
satisfy all the requests in the disk queue. Assume the last request service was at track 345 and
the head is moving toward track 0. The queue in FIFO order contains requests for the following
tracks: 123, 874, 692, 475, 105, 376 (Assume SCAN algorithm) (NET-DEC-2012)
(A) 2013 (B) 1219 (C) 1967 (D) 1507
Q Consider a disk queue with I/O requests on the following cylinders in their arriving order: 6, 10,
12, 54, 97, 73, 128, 15, 44, 110, 34, 45 The disk head is assumed to be at cylinder 23 and moving
in the direction of decreasing number of cylinders. Total number of cylinders in the disk is 150.
The disk head movement using SCAN-scheduling algorithm is: (NET-JAN-2017)
a) 172 b) 173 c) 227 d) 228
197
Q Suppose the following disk request sequence (track numbers) for a disk with 100 tracks is
given: 45, 20, 90, 10, 50, 60, 80, 25, 70. Assume that the initial position of the R/W head is on
track 50. The additional distance that will be traversed by the R/W head when the Shortest Seek
Time First (SSTF) algorithm is used compared to the SCAN (Elevator) algorithm (assuming that
SCAN algorithm moves towards 100 when it starts execution) is tracks (GATE-2015) (2
Marks)
Q Consider a disk queue with request for input/output to block on cylinders 98, 183, 37, 122, 14,
124, 65, 67 in that order. Assume that disk head is initially positioned at cylinder 53 and moving
towards cylinder number 0. The total number of head movements using Shortest Seek Time First
(SSTF) and SCAN algorithms are respectively (NET-DEC-2013)
(A) 236 and 252 cylinders (B) 640 and 236 cylinders
(C) 235 and 640 cylinders (D) 235 and 252 cylinders
C-SCAN Scheduling
Circular-scan is a variant of SCAN designed to provide a more uniform wait time. Like SCAN, C-
SCAN moves the head from one end of the disk to the other, servicing requests along the way.
When the head reaches the other end, however, it immediately returns to the beginning of the
disk without servicing any requests on the return trip.
Advantages:
• Provides more uniform wait time compared to SCAN
• Better response time compared to scan
Disadvantage:
• More seeks movements in order to reach starting position
198
Q Consider the situation in which the disk read/write head is currently located at track 45 (of
tracks 0-255) and moving in the positive direction. Assume that the following track requests have
been made in this order: 40, 67, 11, 240, 87. What is the order in which C-SCAN would service
these requests and what is the total seek distance?
(A) 600 (B) 810 (C) 505 (D) 550
LOOK Scheduling
It is similar to the SCAN disk scheduling algorithm except the difference that the disk arm in spite
of going to the end of the disk goes only to the last request to be serviced in front of the head
and then reverses its direction from there only. Thus, it prevents the extra delay which occurred
due to unnecessary traversal to the end of the disk.
Advantage: -
• Better performance compared to SCAN
• Should be used in case to less load
Disadvantage: -
• Overhead to find the last request
• Should not be used in case of more load.
C LOOK
As LOOK is similar to SCAN algorithm, in similar way, C-LOOK is similar to C-SCAN disk scheduling
algorithm. In C-LOOK, the disk arm in spite of going to the end goes only to the last request to be
serviced in front of the head and then from there goes to the other end’s last request. Thus, it
also prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.
Advantage: -
• Provides more uniform wait time compared to LOOK
• Better response time compared to LOOK
Disadvantage: -
• Overhead to find the last request and go to initial position is more
• Should not be used in case of more load.
Q Consider the situation in which the disk read/write head is currently located at
track 45 (of tracks 0-255) and moving in the positive direction. Assume that the
following track requests have been made in this order: 40, 67, 11, 240, 87. What is
the order in which optimized C-SCAN would service these requests and what is the
total seek distance?(GATE-2015) (2 Marks)
(A) 600 (B) 810 (C) 505 (D) 550
199
Q Which of the following statements is not true about disk-arm scheduling algorithms? (NET-
JUNE-2014)
(A) SSTF (shortest seek time first) algorithm increases performance of FCFS.
(B) The number of requests for disk service are not influenced by file allocation method.
(C) Caching the directories and index blocks in main memory can also help in reducing disk arm
movements.
(D) SCAN and C-SCAN algorithms are less likely to have a starvation problem.
Q In disk scheduling algorithm, the disk head moves from one end to other end of
the disk, serving the requests along the way. When the head reaches the other end, it
immediately returns to the beginning of the disk without serving any requests on the return trip.
(NET-NOV-2017)
a) LOOK b) SCAN c) C-LOOK d) C-SCAN
Q Suppose the following disk request sequence (track numbers) for a disk with 100 tracks is given: 45, 20,
90, 10, 50, 60, 80, 25, 70. Assume that the initial position of the R/W head is on track 50. The additional
distance that will be traversed by the R/W head when the Shortest Seek Time First (SSTF) algorithm is
used compared to the SCAN (Elevator) algorithm (assuming that SCAN algorithm moves towards 100
when it starts execution) is tracks (GATE-2015) (2 Marks)
Q Consider a disk queue with requests for I/O to blocks on cylinders 47, 38, 121, 191, 87, 11, 92, 10. The
C-LOOK scheduling algorithm is used. The head is initially at cylinder number 63, moving towards larger
cylinder numbers on its servicing pass. The cylinders are numbered from 0 to 199. The total head
movement (in number of cylinders) incurred while servicing these requests is . (GATE-2018)
(2 Marks)
Conclusion
200
Selection of a Disk-Scheduling Algorithm
• Given so many disk-scheduling algorithms, how do we choose the best one? SSTF is common and
has a natural appeal because it increases performance over FCFS. SCAN and C-SCAN perform better
for systems that place a heavy load on the disk, because they are less likely to cause a starvation
problem.
• With any scheduling algorithm, however, performance depends heavily on the number and types
of requests. For instance, suppose that the queue usually has just one outstanding request. Then, all
scheduling algorithms behave the same, because they have only one choice of where to move the
disk head: they all behave like FCFS scheduling.
Q Consider an operating system capable of loading and executing a single sequential user process at a
time. The disk head scheduling algorithm used is First Come First Served (FCFS). If FCFS is replaced by
Shortest Seek Time First (SSTF), claimed by the vendor to give 50% better benchmark results, what is the
expected improvement in the I/O performance of user programs? (GATE-2004) (1 Marks)
(A) 50% (B) 40% (C) 25% (D) 0%
Q Which of the following disk scheduling strategies is likely to give the best
throughput? (GATE-1999) (1 Marks)
(a) Farthest cylinder next (b) Nearest cylinder next
(c) First come first served (d) Elevator algorithm
201
Q The maximum amount of information that is available in one portion of the disk
access arm for a removal disk pack (without further movement of the arm with
multiple heads) (NET-DEC-2011)
(A) a plate of data (B) a cylinder of data
(C) a track of data (D) a block of data
Q Consider the input/output (I/O) requests made at different instants of time directed at a hypothetical disk having
200 tracks as given in the following table
Serial No 1 2 3 4 5
Track No 12 85 40 100 75
Time of arrival 65 80 110 100 175
Assume that :
Current head position is at track no. 65 Direction of last movement is towards higher numbered tracks Current clock
time is 160 milliseconds Head movement time per track is 1 millisecond. “look” is a variant of “SCAN” disk- arm
scheduling algorithm. In this algorithm, if no more I/O requests are left in current direction, the disk head reverses
its direction. The seek times in Shortest Seek First (SSF) and “look” disk-arm scheduling algorithms respectively are:
(NET-SEP-2013)
(A) 144 and 123 milliseconds (B) 143 and 123 milliseconds
(C) 149 and 124 milliseconds (D) 256 and 186 milliseconds
Q Consider a storage disk with 4 platters (numbered as 0, 1, 2 and 3), 200 cylinders (numbered as 0, 1, …, 199), and 256 sectors
per track (numbered as 0, 1, … 255). The following 6 disk requests of the form [sector number, cylinder number, platter number]
are received by the disk controller at the same time:
[120, 72, 2], [180, 134, 1], [60, 20, 0], [212, 86, 3], [56, 116, 2], [118, 16, 1]
Currently head is positioned at sector number 100 of cylinder 80, and is moving towards higher cylinder numbers. The average
power dissipation in moving the head over 100 cylinders is 20 milliwatts and for reversing the direction of the head movement
once is 15 milliwatts. Power dissipation associated with rotational latency and switching of head between different platters is
negligible.
The total power consumption in milliwatts to satisfy all of the above disk requests using the Shortest Seek Time First disk
scheduling algorithm is . (GATE-2018) (2 Marks)
202
Q For a magnetic disk with concentric circular tracks, the seek latency is not linearly proportional
to the seek distance due to (GATE-2008) (1 Marks)
a) non-uniform distribution of requests
b) arm starting and stopping inertia
c) higher capacity of tracks on the periphery of the platter
d) use of unfair arm scheduling policies
Transfer time
• Magnetic disks provide the bulk of secondary storage for modern computer systems. Each disk
platter has a flat circular shape, like a CD. Common platter diameters range from 1.8 to 3.5 inches.
The two surfaces of a platter are covered with a magnetic material. We store information by
recording it magnetically on the platters.
A read – write head “flies” just above each surface of every platter. The heads are attached to a disk
arm that moves all the heads as a unit. The surface of a platter is logically divided into circular tracks,
which are subdivided into sectors. The set of tracks that are at one arm position makes up a cylinder.
There may be thousands of concentric cylinders in a disk drive, and each track may contain hundreds
of sectors.
• When the disk is in use, a drive motor spins it athigh speed. Most drives rotate 60 to 250 times per
second, specified in terms of rotations per minute (RPM). Common drives spin at 5,400, 7,200, 10,000,
and 15,000 RPM.
• Total transfer time has two parts. The positioning time, or random-access time, consists of two parts:
the time necessary to move the disk arm to the desired cylinder, calledthe seek time, and the time
necessary for the desired sector to rotate to the disk head, called the rotational latency.
203
• Total Transfer Time = Seek Time + Rotational Latency + Transfer Time
• Seek Time: - It is a time taken by Read/Write header to reach the correct track. (Always given in
question)
• Rotational Latency: - It is the time taken by read/Write header during the wait for the correct sector. In
general, it’s a random value, so far average analysis, we consider the time taken by disk to complete
half rotation.
• Transfer Time: - it is the time taken by read/write header either to read or write on a disk. In general,
we assume that in 1 complete rotation, header can read/write the either track, so
• total time will be = (File Size/Track Size) *time taken to complete one revolution.
Q For a magnetic disk with concentric circular tracks, the seek latency is not linearly proportional
to the seek distance due to (GATE-2008) (2 Marks)
(A) non-uniform distribution of requests
(B) arm starting and stopping inertia
(C) higher capacity of tracks on the periphery of the platter
(D) use of unfair arm scheduling policies
Q Consider a disk where there are 512 tracks, each track is capable of holding 128
sector and each sector holds 256 bytes, find the capacity of the track and disk and
number of bits required to reach correct track, sector and disk.
Q Consider a disk pack with 16 surfaces, 128 tracks per surface and 256 sectors per track. 512
bytes of data are stored in a bit serial manner in a sector. The capacity of the disk pack and the
number of bits required to specify a particular sector in the disk are respectively (GATE-2006) (2
Marks)
(A)256 Mbyte, 19 bits (B)256 Mbyte, 28 bits
(C)512 Mbyte, 20 bits (D) 64 Gbyte, 28 bit
Q consider a disk where each sector contains 512 bytes and there are 400 sectors
per track and 1000 tracks on the disk. If disk is rotating at speed of 1500 RPM, find
the total time required to transfer file of size 1 MB. Suppose seek time is 4ms?
204
Q Consider a disk with 16384 bytes per track having a rotation time of 16 msec and
average seek time of 40 msec. What is the time in msec to read a block of 1024
bytes from this disk? (NET-DEC-2015)
a) 57 sec b) 49 sec c) 48 sec d) 17 sec
Q Consider a system with 8 sector per track and 512 bytes per sector. Assume that
disk rotates at 3000 rpm and average seek time is 15ms standard. Find total time
required to transfer a file which requires 8 sectors to be stored.
a) Assume contiguous allocation
b) Assume Non- contiguous allocation
Q Consider a disk pack with a seek time of 4 milliseconds and rotational speed of 10000 rotations per
minute (RPM). It has 600 sectors per track and each sector can store 512 bytes of data. Consider a file
stored in the disk. The file contains 2000 sectors. Assume that every sector access necessitates a seek,
and the average rotational latency for accessing each sector is half of the time for one complete rotation.
The total time (in milliseconds) needed to read the entire file is . (GATE-2015) (2 Marks)
205
Q Consider a typical disk that rotates at 15000 rotations per minute (RPM) and has a transfer rate
of 50 × 106 bytes/sec. If the average seek time of the disk is twice the average rotational delay
and the controller’s transfer time is 10 times the disk transfer time, the average time (in
milliseconds) to read or write a 512 byte sector of the disk is (GATE-2015) (2
Marks)
Q An application loads 100 libraries at start-up. Loading each library requires exactly one disk
access. The seek time of the disk to a random location is given as 10 milli second. Rotational
speed of disk is 6000 rpm. If all 100 libraries are loaded from random locations on the disk, how
long does it take to load all libraries? (The time to transfer data from the disk block once the
head has been positioned at the start of the block may be neglected) (GATE-2011) (2 Marks)
(A) 0.50 s (B) 1.50 s (C) 1.25 s (D) 1.00 s
206
File allocation methods
The main aim of file allocation problem is how disk space is utilized effectively and files can be
accessed quickly. Three major methods of allocating disk space are in wide use:
• Contiguous
• Linked
• Indexed
Each method has advantages and disadvantages. Although some systems support all three, it is
more common for a system to use one method for all files.
Contiguous Allocation
• Contiguous allocation requires that each file occupy a set of contiguous blocks on the disk.
Disk addresses define a linear ordering on the disk. With this ordering, assuming that only
one job is accessing the disk, accessing block b + 1 after block b normally requires no head
movement.
• Advantage
• Accessing a file that has been allocated contiguously is easy. Thus, both
sequential and direct access can be supported by contiguous allocation.
• Disadvantage
• Suffer from the problem of external fragmentation.
• Suffer from huge amount of external fragmentation.
• Another problem with contiguous allocation file modification
Q Suppose there are six files F1, F2, F3, F4, F5, F6 with corresponding sizes 150 KB, 225 KB, 75 KB,
60 KB, 275 KB and 65 KB respectively. The files are to be stored on a sequential device in such a
way that optimizes access time. In what order should the files be stored? (NET-NOV-2017)
A) F5, F2, F1, F3, F6, F4 b) F4, F6, F3, F1, F2, F5
c) F1, F2, F3, F4, F5, F6 d) F6, F5, F4, F3, F2, F1
207
Linked Allocation
• Linked allocation solves all problems of contiguous allocation. With linked allocation, each file
is a linked list of disk blocks; the disk blocks may be scattered anywhere on the disk. The
directory contains a pointer to the first and last blocks of the file.
• Advantage: -
• To create a new file, we simply create a new entry in the directory. With linked allocation,
each directory entry has a pointer to the first disk block of the file. To read a file, we simply
read blocks by following the pointers from block to block. The size of a file need not be
declared when the file is created. A file can continue to grow as long as free blocks are
available.
• There is no external fragmentation with linked allocation, and any free block on the free-space
list can be used to satisfy a request.
• Disadvantage: -
• To find the ith block of a file, we must start at the beginning of that file and follow the pointers
until we get to the ith block. Each access to a pointer requires a disk read.
• Another disadvantage is the space required for the pointers, so each file requires slightly more
space than it would otherwise.
• Yet another problem is reliability. Recall that the files are linked together by pointers scattered
all over the disk, and consider what would happen if a pointer were lost or damaged. One
partial solution is to use doubly linked lists, and another is to store the file name and relative
block number in each block. However, these schemes require even more overhead for each
file.
208
Indexed Allocation
• Indexed allocation solves problems of contiguous and linked allocation, by bringing all the
pointers together into one location: the index block.
• Each file has its own index block, which is an array of disk-block addresses. The ith
entry in the index block points to the ith block of the file. The directory entry
contains the address of the index block. To find and read the ith block, we use
the pointer in the ith index-block entry.
• When the file is created, all pointers in the index block are set to null. When the
ith block is first written, a block is obtained from the free-space manager, and its
address is put in the ith index-block entry.
• This point raises the question of how large the index block should be. Every file
must have an index block, so we want the index block to be as small as possible.
If the index block is too small, however, it will not be able to hold enough
pointers for a large file.
• Linked scheme: To allow for large files, we can link together several index blocks. For
example, an index block might contain a small header giving the name of the file and a
set of the first 100 disk-block addresses. The next address (the last word in the index
block) is null (for a small file) or is a pointer to another index block (for a large file).
209
• Combined scheme. Another alternative, used in UNIX-based file systems, is to keep the first,
say, 15 pointers of the index block in the file’s inode.
• The first 12 of these pointers point to direct blocks; that is, they contain addresses of blocks
that contain data of the file. Thus, the data for small files do not need a separate index block.
• The next three pointers point to indirect blocks. The first points to a single indirect block,
which is an index block containing not data but the addresses of blocks that do contain data.
• The second points to a double indirect block, which contains the address of a block that
contains the addresses of blocks that contain pointers to the actual data blocks. The last
pointer contains the address of a triple indirect block.
• Advantage
• Indexed allocation supports direct access, without suffering from external
fragmentation, because any free block on the disk can satisfy a request for
more space.
• Disadvantage
Indexed allocation does suffer from wasted space, however. The pointer overhead
of the index block is generally greater than the pointer overhead oflinked allocation.
(b) II only
Q Consider a file currently consisting of 50 blocks. Assume that the file control block and the
index block is already in memory. If a block is added at the end (and the block information to be
added is stored in memory), then how many disk I/O operations are required for indexed (single-
level) allocation strategy? (NET-NOV-2016)
A) 1 b) 101 c) 27 d) 0
210
Q Match the following: (NET-JUNE-2014)
List-I List-II
a) Contiguous allocation i) This scheme supports very large file sizes.
b) Linked allocation ii) This allocation technique supports only sequential files.
c) Indexed allocation iii) Number of disks required to access file is minimal.
d) Multi-level indexed iv) This technique suffers from maximum wastage of space in storing
pointers.
Q The index node (inode) of a Unix-like file system has 12 direct, one single-
indirect and one double-indirect pointers. The disk block size is 4 kB, and the disk
block address is 32-bits long. The maximum possible file size is (rounded off to 1
decimal place) GB. (GATE-2014) (2 Marks)
211
Q A file system with 300 Gbyte disk uses a file descriptor with 8 direct block
addresses, 1 indirect block address and 1 doubly indirect block address. The size of
each disk block is 128 Bytes and the size of each disk block address is 8 Bytes. The
maximum possible file size in this file system is (GATE-2012) (2 Marks)
(A) 3 Kbytes
(B) 35 Kbytes
Q The data blocks of a very large file in the Unix file system are allocated using
(GATE-2008) (1 Marks)
(A) contiguous allocation (B) linked allocation
(C) indexed allocation (D) an extension of indexed allocation
Q A Unix-style i-node has 10 direct pointers and one single, one double and one
triple indirect pointer. Disk block size is 1 Kbyte, disk block address is 32 bits, and
48-bit integers are used. What is the maximum possible file size? (GATE-2004) (2
Marks)
(A) 224 bytes
212
Q In the index allocation scheme of blocks to a file, the maximum possible size of
the file depends on (GATE-2002) (1 Marks)
(a) the size of the blocks, and the size of the address of the blocks.
(b) the number of blocks used for the index, and the size of the blocks.
(c) the size of the blocks, the number of blocks used for the index, and the size of
the address of the blocks.
(d) None of the above
Free-Space Management
• Since disk space is limited, we need to reuse the space from deleted files for new files, if
possible.
• To keep track of free disk space, the system maintains a free-space list. The free-space list
records all free disk blocks—those not allocated to some file or directory.
• To create a file, we search the free-space list for the required amount of space and allocate
that space to the new file.
• This space is then removed from the free-space list. When a file is deleted, its disk space is
added to the free-space list.
Q How many disk blocks are required to keep list of free disk blocks in a 16 GB hard
disk with 1 kB block size using linked list of free disk blocks? Assume that the disk
block number is stored in 32 bits. (NET-DEC-2014)
(A) 1024 blocks (B) 16794 blocks
(C) 20000 blocks (D) 1048576 blocks
213
Bit Vector
• Frequently, the free-space list is implemented as a bit map or bit vector. Each block is
represented by 1 bit. If the block is free, the bit is 1; if the block is allocated, the bit is
0.
• For example, consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26,
and 27 are free and the rest of the blocks are allocated. The free-space bit map would
be 001111001111110001100000011100000 ...
• The main advantage of this approach is its relative simplicity and its efficiency in
finding the first free block or n consecutive free blocks on the disk.
• Unfortunately, bit vectors are inefficient unless the entire vector is kept in main
memory. Keeping it in main memory is possible for smaller disks but not necessarily for
larger ones.
• A 1.3-GB disk with 512-byte blocks would need a bit map of over 332 KB to track its
free blocks.
• A 1-TB disk with 4-KB blocks requires 256 MB to store its bit map. Given that disk size
constantly increases, the problem with bit vectors will continue to escalate as well.
Q How much space will be required to store the bit map of a 1.3 GB disk
with 512 bytes block size? (NET-DEC-2013)
(A) 332.8 KB (B) 83.6 KB
(C) 266.2 KB (D) 256.6 KB
214
Linked List
• Another approach to free-space management is to link together all the free disk blocks,
keeping a pointer to the first free block in a special location on the disk and caching it in
memory.
• This first block contains a pointer to the next free disk block, and so on. Recall our earlier
example, in which blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26, and 27 were free and
the rest of the blocks were allocated.
• In this situation, we would keep a pointer to block 2 as the first free block. Block 2 would
contain a pointer to block 3, which would point to block 4, which would point to block 5,
which would point to block 8, and so on.
• This scheme is not efficient; to traverse the list, we must read each block, which requires
substantial I/O time.
• Fortunately, however, traversing the free list is not a frequent action. Usually, the
operating system simply needs a free block so that it can allocate that block to a file, so the
first block in the free list is used.
Q A FAT (file allocation table) based file system is being used and the total overhead of
each entry in the FAT is 4 bytes in size. Given a 100 x 106 bytes disk on which the file
system is stored and data block size is 103 bytes, the maximum size of a file that can be
stored on this disk in units of 106 bytes is . (GATE-2014) (2 Marks)
215
Q An experimental file server is up 75% of the time and down for 25% of the time
due to bugs. How many times does this file server have to be replicated to give an
availability of at least 99%? (NET-NOV-2016)
a) 2 b) 4 c) 8 d) 16
216
217