Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

unit 3 cpu scheduling algorithms

The document explains the concepts of processes and their lifecycle in operating systems, detailing the distinction between programs and processes, as well as the structure of a Process Control Block (PCB). It covers process scheduling, types of scheduling algorithms, and the importance of CPU and I/O bursts, along with various scheduling strategies like FCFS and SJF. Additionally, it discusses the implications of scheduling on CPU utilization, throughput, and response times.

Uploaded by

Akhilesh kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

unit 3 cpu scheduling algorithms

The document explains the concepts of processes and their lifecycle in operating systems, detailing the distinction between programs and processes, as well as the structure of a Process Control Block (PCB). It covers process scheduling, types of scheduling algorithms, and the importance of CPU and I/O bursts, along with various scheduling strategies like FCFS and SJF. Additionally, it discusses the implications of scheduling on CPU utilization, throughput, and response times.

Uploaded by

Akhilesh kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Process Concept

Program vs. Process


A process is a program in execution. For example, when we write a program in
C or C++ and compile it, the compiler creates binary code. The original code
and binary code are both programs. When we actually run the binary code, it
becomes a process.
A process is an ‘active’ entity, as opposed to a program, which is considered to
be a ‘passive’ entity. A single program can create many processes when run
multiple times; for example, when we open a .exe or binary file multiple times,
multiple instances begin (multiple processes are created).

Process in memory
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image
shows a simplified layout of a process inside main memory −

 Text Section: A Process, sometimes known as the Text Section, also


includes the current activity represented by the value of the Program
Counter.
 Stack: The Stack contains the temporary data, such as function
parameters, returns addresses, and local variables.
 Data Section: Contains the global variable.
 Heap Section: Dynamically allocated memory to process during its run
time.

Process Life Cycle or Process states

When a process executes, it passes through different states. These stages may
differ in different operating systems, and the names of these states are also not
standardized.

 NEW- The process is being created.


 READY- The process is waiting to be assigned to a processor.
 RUNNING- Instructions are being executed.
 WAITING- The process is waiting for some event to occur (such as an
I/O completion or reception of a signal).
 TERMINATED- The process has finished execution.
Process Control Block (PCB) or Task control block

A Process Control Block is a data structure maintained by the Operating System


for every process. The PCB is identified by an integer process ID (PID). A PCB
keeps all the information needed to keep track of a process as listed below:

 Process State: It can be running, waiting etc.


 Process ID and the parent process ID.
 CPU registers and Program Counter. Program Counter holds the
address of the next instruction to be executed for that process.
 CPU Scheduling information: Such as priority information and pointers
to scheduling queues.
 Memory Management information: For example, page tables or
segment tables.
 Accounting information: The User and kernel CPU time consumed,
account numbers, limits, etc.
 I/O Status information: Devices allocated, open file tables, etc.
 All of the above attributes of a process are also known as the context of
the process.

 The architecture of a PCB is completely dependent on Operating System


and may contain different information in different operating systems.

Process Address space


 An address space is a range of valid addresses in memory that are
available for a program or process. That is, it is the memory that a
program or process can access. The memory can be either physical or
virtual and is used for executing instructions and storing data.

 The process address space is the set of logical addresses that a process
references in its code. For example, when 32-bit addressing is in use,
addresses can range from 0 to 0x7fffffff; that is, 2^31 possible numbers,
for a total theoretical size of 2 gigabytes.

The operating system takes care of mapping the logical addresses to physical
addresses at the time of memory allocation to the program. There are three
types of addresses used in a program before and after memory is allocated :

 Symbolic addresses

The addresses used in a source code. The variable names, constants, and
instruction labels are the basic elements of the symbolic address space.

 Relative addresses

At the time of compilation, a compiler converts symbolic addresses into


relative addresses.

 Physical addresses

The loader generates these addresses at the time when a program is loaded
into main memory.
The set of all logical addresses generated by a program is referred to as
a logical address space. The set of all physical addresses corresponding to
these logical addresses is referred to as a physical address space.

Process Identification Information


A PID (i.e., process identification number) is an identification number that is
automatically assigned to each process when it is created on a Unix-
like operating system. A process is an executing (i.e., running) instance of a
program. Each process is guaranteed a unique PID, which is always a non-
negative integer.
Scheduling Concepts
Process Scheduling
 The process scheduling is the activity of the process manager that handles
the removal of the running process from the CPU and the selection of
another process on the basis of a particular strategy.

 Process scheduling is an essential part of a Multiprogramming operating


systems. Such operating systems allow more than one process to be
loaded into the executable memory at a time and the loaded process
shares the CPU using time multiplexing

Process scheduling queues


 Process Scheduling Queues help you to maintain a distinct queue for each
and every process states and PCBs.
 All the process of the same execution states are placed in the same
queue. Therefore, whenever the state of a process is modified, its PCB
needs to be unlinked from its existing queue, which moves back to the
new state queue.

Three types of operating system queues are:

1. Job queue – It helps you to store all the processes in the system.
2. Ready queue – This type of queue helps you to set every process
residing in the main memory, which is ready and waiting to execute.
3. Device queues – It is a process that is blocked because of the absence of
an I/O device.

In the below-given Diagram,

 Rectangle represents a queue.


 Circle denotes the resource
 Arrow indicates the flow of the process.

1. Every new process first put in the Ready queue and waits until it is
selected for execution or it is dispatched.
2. One of the processes is allocated the CPU and it is executing.
3. The process should issue an I/O request
4. Then, it should be placed in the I/O queue.
5. The process should create a new subprocess
6. The process should be waiting for its termination.
7. It should remove forcefully from the CPU, as a result interrupt. Once
interrupt is completed, it should be sent back to ready queue

Scheduling Concepts
1. CPU and I/O Bursts
 Typical process execution pattern: use the CPU for a while (CPU burst), then
do some I/O operations (IO burst).

 CPU bound processes perform I/O operations infrequently and tend to have
long CPU bursts.

 I/O bound processes spend less time doing computation and tend to have short
CPU bursts.
2. Types of CPU Scheduling

CPU scheduling decisions may take place under the following four
circumstances:

1. When a process switches from the running state to the waiting state(for
I/O request or invocation of wait for the termination of one of the child
processes).
2. When a process switches from the running state to the ready state (for
example, when an interrupt occurs).
3. When a process switches from the waiting state to the ready state(for
example, completion of I/O).
4. When a process terminates.
 When Scheduling takes place only under circumstances 1 and 4, we say
the scheduling scheme is non-preemptive; otherwise the scheduling
scheme is preemptive.

Non-Preemptive Scheduling

 Under non-preemptive scheduling, once the CPU has been allocated to a


process, the process keeps the CPU until it releases the CPU either by
terminating or by switching to the waiting state.

 This scheduling method is used by the Microsoft Windows 3.1 and by the
Apple Macintosh operating systems.

 It is the only method that can be used on certain hardware platforms,


because It does not require the special hardware(for example: a timer)
needed for preemptive scheduling.

Preemptive Scheduling

 In this type of Scheduling, the tasks are usually assigned with priorities.
At times it is necessary to run a certain task that has a higher priority
before another task although it is running.

 Therefore, the running task is interrupted for some time and resumed later
when the priority task has finished its execution.

3. Schedulers
Schedulers are special system software which handles process scheduling in
various ways. Their main task is to select the jobs to be submitted into the
system and to decide which process to run. Schedulers are of three types −
1. Long-Term Scheduler
2. Short-Term Scheduler
3. Medium-Term Scheduler
Long Term Scheduler

 It is also called a job scheduler. A long-term scheduler determines which


programs are admitted to the system for processing

 It selects processes from the queue and loads them into memory for
execution.

 The primary objective of the job scheduler is to provide a balanced mix


of jobs, such as I/O bound and processor bound. It also controls the
degree of multiprogramming.

 On some systems, the long-term scheduler may not be available or


minimal. Time-sharing operating systems have no long term scheduler.
When a process changes the state from new to ready, then there is use of
long-term scheduler.

Short Term Scheduler

 It is also called as CPU scheduler. Its main objective is to increase


system performance in accordance with the chosen set of criteria.

 CPU scheduler selects a process among the processes that are ready to
execute and allocates CPU to one of them.

 Short-term schedulers, also known as dispatchers, make the decision of


which process to execute next. Short-term schedulers are faster than
long-term schedulers.
Medium Term Scheduler

 Medium-term scheduling is a part of swapping. It removes the processes


from the memory. It reduces the degree of multiprogramming. The
medium-term scheduler is in-charge of handling the swapped out-
processes.

 A running process may become suspended if it makes an I/O request.


Suspended processes cannot make any progress towards completion. In
this condition, to remove the process from memory and make space for
other processes, the suspended process is moved to the secondary storage.
This is called swapping, and the process is said to be swapped out or
rolled out. Swapping may be necessary to improve the process mix.

4. Dispatcher

Another component involved in the CPU scheduling function is


the Dispatcher. The dispatcher is the module that gives control of the CPU
to the process selected by the short-term scheduler. This function involves:

 Switching context
 Switching to user mode
 Jumping to the proper location in the user program to restart that program
from where it left last time.

The dispatcher should be as fast as possible, given that it is invoked during


every process switch. The time taken by the dispatcher to stop one process and
start another process is known as the Dispatch Latency.
Context Switch
 A context switch is the mechanism to store and restore the state or
context of a CPU in Process Control block so that a process execution
can be resumed from the same point at a later time.

 Using this technique, a context switcher enables multiple processes to


share a single CPU. Context switching is an essential part of a
multitasking operating system features.

When the scheduler switches the CPU from executing one process to execute
another, the state from the current running process is stored into the process
control block. After this, the state for the process to run next is loaded from its
own PCB and used to set the PC, registers, etc. At that point, the second process
can start executing.
Comparison among Schedulers
Scheduling Algorithms

Scheduling Criteria
1. CPU Utilization

We want to keep the CPU as busy as possible. CPU utilization may range from
0 to 100 percent. In a real system, it should range from 40 percent (for a lightly
loaded system) to 90 percent (for a heavily used system).

2. Throughput

It is the total number of processes completed per unit time or rather say total
amount of work done in a unit of time. This may range from 10/second to
1/hour depending on the specific processes.

3. Turnaround Time

The interval from the time of submission of a process to the time of completion
is the turnaround time. Turnaround time is the sum of the periods spent waiting
to get into memory, waiting in the ready queue, executing on the CPU, and
doing I/O.

4. Waiting Time

The sum of the periods spent waiting in the ready queue amount of time a
process has been waiting in the ready queue to acquire get control on the CPU.

5. Response Time

Amount of time it takes from when a request was submitted until the first
response is produced. Remember, it is the time till the first response and not the
completion of process execution (final response).

Note: In general CPU utilization and Throughput are maximized and other
factors are reduced for proper optimization.
Scheduling Algorithms
A Process Scheduler schedules different processes to be assigned to the CPU
based on particular scheduling algorithms. These algorithms are either non-
preemptive or preemptive. There are six popular process scheduling algorithms:

 First Come First Serve(FCFS) Scheduling

 Shortest-Job-First(SJF) Scheduling

 Priority Scheduling

 Round Robin(RR) Scheduling

 Multilevel Queue Scheduling

 Multilevel Feedback Queue Scheduling

1. First Come First Serve (FCFS) Scheduling


 Simplest scheduling algorithm that schedules according to arrival times

of processes. First come first serve scheduling algorithm states that the
process that requests the CPU first is allocated the CPU first.

 It is implemented by using the FIFO queue. When a process enters the


ready queue, its PCB is linked onto the tail of the queue. When the CPU
is free, it is allocated to the process at the head of the queue. The running
process is then removed from the queue.

 This is used in Batch Systems. FCFS is a non-preemptive scheduling

algorithm.
 A perfect real life example of FCFS scheduling is buying tickets at

ticket counter.
Advantage of FCFS Scheduling
1. Simple to implement and understand.

2. Minimum overheads.

Disadvantage of FCFS Scheduling


1. It is Non Pre-emptive algorithm, which means the process
priority doesn't matter. If a process with very least priority is being
executed, more like daily routine backup process, which takes more
time, and all of a sudden some other high priority process arrives,
like interrupt to avoid system crash, the high priority process will have
to wait, and hence in this case, the system will crash, just because of
improper process scheduling.

2. Not optimal Average Waiting Time.


3. Resources utilization in parallel is not possible, which leads to Convoy
Effect, and hence poor resource (CPU, I/O etc) utilization.
What is Convoy Effect?
Convoy Effect is a situation where many processes, which needs to use a
resource for short time are blocked by one process holding that resource for a
long time.

This essentially leads to poor utilization of resources and hence poor


performance.
2. Shortest Job First (SJF) Scheduling

 Shortest Job First scheduling works on the process with the shortest burst

time or duration first.

 This is the best approach to minimize waiting time. This is used in Batch

Systems.

 It is of two types:

1. Non Pre-emptive

2. Pre-emptive

 To successfully implement it, the burst time/duration time of the

processes should be known to the processor in advance, which is

practically not feasible all the time.

 This scheduling algorithm is optimal if all the jobs/processes are

available at the same time. (either Arrival time is 0 for all, or Arrival time

is same for all)


Problem with Non Pre-emptive SJF
If the arrival time for processes are different, which means all the processes are
not available in the ready queue at time 0, and some jobs arrive after some time,
in such situation, sometimes process with short burst time have to wait for the
current process's execution to finish, because in Non Pre-emptive SJF, on
arrival of a process with short duration, the existing job/process's execution is
not halted/stopped to execute the short job first.

This leads to the problem of Starvation, where a shorter process has to wait for
a long time until the current longer process gets executed. This happens if
shorter jobs keep coming, but this can be solved using the concept of aging.

 Shortest Remaining time first (Pre-emptive SJF)


In Preemptive Shortest Job First Scheduling, jobs are put into ready queue as
they arrive, but as a process with short burst time arrives, the existing process
is preempted or removed from execution, and the shorter job is executed first.

The Pre-emptive SJF is also known as Shortest Remaining Time First,


because at any given point of time, the job with the shortest remaining time is
executed first.
3. Priority Scheduling
 Shortest Job First scheduling algorithm is a special case of general
priority scheduling algorithm, the priority of a process is generally the
inverse of the CPU burst time, i.e. the larger the burst time the lower is
the priority of that process.

 In priority scheduling algorithm, each process is assigned a priority.


Process with highest priority is to be executed first and so on.

 Processes with same priority are executed on first come first served
basis.

 The priority of process, when internally defined, can be decided


based on memory requirements, time limits, number of open
files, ratio of I/O burst to CPU burst etc.

 Whereas, external priorities are set based on criteria outside the


operating system, like the importance of the process, funds paid
for the computer resource use, market factor etc.

Priority scheduling can be of two types:


1. Preemptive Priority Scheduling: If the new process arrived at
the ready queue has a higher priority than the currently running
process, the CPU is preempted, which means the processing of
the current process is stoped and the incoming new process with
higher priority gets the CPU for its execution.
2. Non-Preemptive Priority Scheduling: In case of non-
preemptive priority scheduling algorithm if a new process
arrives with a higher priority than the current running process,
the incoming process is put at the head of the ready queue,
which means after the execution of the current process it will be
processed.

Problem with Priority Scheduling Algorithm


In priority scheduling algorithm, the chances of indefinite
blocking or starvation.

A process is considered blocked when it is ready to run but has to wait for the
CPU as some other process is running currently.

But in case of priority scheduling if new higher priority processes keeps coming
in the ready queue then the processes waiting in the ready queue with lower
priority may have to wait for long durations before getting the CPU for
execution.

To prevent starvation of any process, we can use the concept of aging where we
keep on increasing the priority of low-priority process based on the its waiting
time.

For example, if we decide the aging factor to be 0.5 for each day of waiting,
then if a process with priority 20(which is comparitively low priority) comes in
the ready queue. After one day of waiting, its priority is increased to 19.5 and so
on.
4. Round Robin (RR)Scheduling
 Round Robin is the preemptive process scheduling
algorithm
 A fixed time is allotted to each process, called quantum,
for execution.
 Once a process is executed for given time period that
process is preemptied and other process executes for
given time period.
 Context switching is used to save states of preemptied
processes.
5. Multilevel Queue Scheduling
 Multiple-level queues are not an independent scheduling
algorithm. They make use of other existing algorithms to
group and schedule jobs with common characteristics.
 A multi-level 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: A common division is made between
foreground (or interactive) processes and background
(or batch) processes. These two types of processes have
different response-time requirements, and so might have
different scheduling needs. In addition, foreground
processes may have priority over background processes.

Let us consider an example of a multilevel queue-scheduling


algorithm with five queues:

1. System Processes

2. Interactive Processes
3. Interactive Editing Processes

4. Batch Processes

5. Student Processes
 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 will be preempted.
6. Multilevel Feedback Queue Scheduling
In a multilevel queue-scheduling algorithm, processes are
permanently assigned to a queue on entry to the system.
Processes do not move between queues. This setup has the
advantage of low scheduling overhead, but the disadvantage
of being inflexible.

Multilevel feedback queue scheduling, however, allows a


process to move between queues. The idea is to separate
processes with different CPU-burst characteristics. If a
process uses too much CPU time, it will be moved to a lower-
priority queue. Similarly, 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.
In general, a multilevel feedback queue scheduler is defined
by the following parameters:

 The number of queues.


 The scheduling algorithm for each queue.
 The method used to determine when to upgrade a process
to a higher-priority queue.
 The method used to determine when to demote a process
to a lower-priority queue.
 The method used to determine which queue a process
will enter when that process needs service.

The definition of a multilevel feedback queue scheduler


makes it the most general CPU-scheduling algorithm. It can
be configured to match a specific system under design.
Unfortunately, it also requires some means of selecting values
for all the parameters to define the best scheduler. Although a
multilevel feedback queue is the most general scheme, it is
also the most complex.
Thread and their management
 A thread is a path of execution within a process. A thread is also known as
lightweight process. The idea is to achieve parallelism by dividing a process
into multiple threads.
 A process can contain multiple threads. For example, in a browser, multiple
tabs can be different threads. MS Word uses multiple threads: one thread to
format the text, another thread to process inputs, etc.

Process vs. Thread

 The primary difference is that threads within the same process run in a shared
memory space, while processes run in separate memory spaces.
 Threads are not independent of one another like processes are, and as a result
threads share with other threads their code section, data section, and OS
resources (like open files and signals). But, like process, a thread has its own
program counter (PC), register set, and stack space.

Advantages of Thread over Process

1. Responsiveness: If the process is divided into multiple threads, if one thread


completes its execution, then its output can be immediately returned.
2. Faster context switch: Context switch time between threads is lower compared to
process context switch. Process context switching requires more overhead from the
CPU.
3. Effective utilization of multiprocessor system: If we have multiple threads in a
single process, then we can schedule multiple threads on multiple processors. This
will make process execution faster.
4. Resource sharing: Resources like code, data, and files can be shared among all
threads within a process.
Note: stack and registers can’t be shared among the threads. Each thread has its own
stack and registers.
5. Communication: Communication between multiple threads is easier, as the threads
shares common address space. While in process we have to follow some specific
communication technique for communication between two processes.
6. Enhanced throughput of the system: If a process is divided into multiple threads,
and each thread function is considered as one job, then the number of jobs completed
per unit of time is increased, thus increasing the throughput of the system.

Types of Thread
There are two types of threads:User Threads and Kernel Threads

 User Level Threads

 User threads are above the kernel and without kernel support. These are the
threads that application programmers use in their programs.
 In this case, the thread management kernel is not aware of the existence of
threads.
 The thread library contains code for creating and destroying threads, for
passing message and data between threads, for scheduling thread execution and
for saving and restoring thread contexts. The application starts with a single
thread.
Advantages

 Thread switching does not require Kernel mode privileges.


 User level thread can run on any operating system.
 Scheduling can be application specific in the user level thread.
 User level threads are fast to create and manage.
Disadvantages

 In a typical operating system, most system calls are blocking.


 Multithreaded application cannot take advantage of multiprocessing.

 Kernel Level Threads

 Kernel threads are supported within the kernel of the OS itself. In this case, thread
management is done by the Kernel. There is no thread management code in the
application area.
 Kernel threads are supported directly by the operating system. Any application
can be programmed to be multithreaded. All of the threads within an application are
supported within a single process.
 All modern OSs support kernel level threads, allowing the kernel to perform multiple
simultaneous tasks and/or to service multiple kernel system calls simultaneously.
 The Kernel maintains context information for the process as a whole and for
individual’s threads within the process.
 Scheduling by the Kernel is done on a thread basis. The Kernel performs thread
creation, scheduling and management in Kernel space. Kernel threads are
generally slower to create and manage than the user threads.
Advantages

 Kernel can simultaneously schedule multiple threads from the same process on
multiple processes.
 If one thread in a process is blocked, the Kernel can schedule another thread of
the same process.
 Kernel routines themselves can be multithreaded.
Disadvantages

 Kernel threads are generally slower to create and manage than the user
threads.
 Transfer of control from one thread to another within the same process
requires a mode switch to the Kernel.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the following strategies:

 Many to One Model


 One to One Model
 Many to Many Model

Many to One Model

 In the many to one model, many user-level threads are all mapped onto a
single kernel thread.
 Thread management is handled by the thread library in user space, which is
efficient in nature.

One to One Model

 The one to one model creates a separate kernel thread to handle each and every
user thread.
 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.
 Users can create any number of the threads.
 Blocking the kernel system calls does not block the entire process.
 Processes can be split across multiple processors.
Deadlocks
Deadlock is a situation where a set of processes are blocked because each process is holding
a resource and waiting for another resource acquired by some other process.

System Model

 A system can be modelled as a collection of limited resources, which can be


partitioned into different categories, to be allocated to a number of processes, each
having different needs.
 Resource categories may include memory, printers, CPUs, open files, tape drives,
CD-ROMS, etc.By definition, all the resources within a category are equivalent, and a
request of this category can be equally satisfied by any one of the resources in that
category.
 If this is not the case ( i.e. if there is some difference between the resources within a
category ), then that category needs to be further divided into separate categories. For
example, "printers" may need to be separated into "laser printers" and "color inkjet
printers".
 Some categories may have a single resource like cpu.
 Each process utilizes the resource in the following sequence:
1. Request - If the request cannot be immediately granted, then the process must
wait until the resource(s) it needs become available. For example the system
calls open( ), malloc( ), new( ), and request( ).
2. Use - The process uses the resource, e.g. prints to the printer or reads from the
file.
3. Release - The process relinquishes the resource so that it becomes available
for other processes. For example, close ( ), free ( ), delete ( ), and release ( ).
 For all kernel-managed resources, the kernel keeps track of what resources are free
and which are allocated, to which process they are allocated, and a queue of processes
waiting for this resource to become available.
 Application-managed resources can be controlled using mutexes or wait ( ) and
signal( ) calls, ( i.e. binary or counting semaphores. )
 A set of processes is deadlocked when every process in the set is waiting for a
resource that is currently allocated to another process in the set
Deadlock Characterization
These four conditions are known as the Coffman conditions.Deadlock can arise if four
conditions hold simultaneously:

1. Mutual exclusion: At least one resource must be held in a non-shareable mode.


Otherwise, the processes would not be prevented from using the resource when
necessary. Only one process can use the resource at any given instant of time.

2. Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes

3. No preemption: a resource can be released only voluntarily by the process holding


it, after that process has completed its task

4. Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such that P0 is
waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2,
…, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource
that is held by P0.

Resource-Allocation Graph
Note:

Graph with a Deadlock Graph with cycle but no deadlock


Methods for Handling Deadlocks
There are three ways of handling deadlocks:

 Deadlock prevention or avoidance - Do not allow the system to get into a


deadlocked state. Deadlock prevention provides a set of methods for ensuring that at
least one of the necessary conditions cannot hold. Deadlocks avoidance, the system
must have additional information about all processes. In particular, the system
must know what resources a process will or may request in the future.

 Deadlock detection and recovery – Allow the system to enter a deadlock state,
detect it and recover. Abort a process or preempt some resources when deadlocks are
detected.

 Ignore the problem all together - If deadlocks only occur once a year or so, it may
be better to simply let them happen and reboot as necessary than to incur the constant
overhead and system performance penalties associated with deadlock prevention or
detection. This is the approach that both Windows and UNIX take.

 If deadlocks are neither prevented nor detected, then when a deadlock occurs
the system will gradually slow down, as more and more processes become
stuck waiting for resources currently held by the deadlock and by other
waiting processes. Unfortunately this slowdown can be indistinguishable from
a general system slowdown when a real-time process has heavy computing
needs.

1. Deadlock Prevention
Restrain the ways request can be made:

 Mutual Exclusion – not required for sharable resources (e.g., read-only files); must
hold for non-sharable resources

 Hold and Wait – must guarantee that whenever a process requests a resource, it does
not hold any other resources

 Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has
none allocated to it.

 Low resource utilization; starvation possible

 No Preemption –

 If a process that is holding some resources requests another resource that


cannot be immediately allocated to it, then all resources currently being held
are released
 Preempted resources are added to the list of resources for which the process
is waiting

 Process will be restarted only when it can regain its old resources, as well as
the new ones that it is requesting

 Circular Wait – impose a total ordering of all resource types, and require that each
process requests resources in an increasing order of enumeration

2. Deadlock Avoidance
Requires that the system has some additional a priori information available

 Simplest and most useful model requires that each process declare the maximum
number of resources of each type that it may need.

 The deadlock-avoidance algorithm dynamically examines the resource-allocation


state to ensure that there can never be a circular-wait condition.

 Resource-allocation state is defined by the number of available and allocated


resources, and the maximum demands of the processes.

Safe State
 When a process requests an available resource, system must decide if immediate
allocation leaves the system in a safe state

 System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the
processes in the systems such that for each Pi, the resources that Pi can still request
can be satisfied by currently available resources + resources held by all the Pj, with j <
i

 That is:

 If Pi resource needs are not immediately available, then Pi can wait until all Pj
have finished

 When Pj is finished, Pi can obtain needed resources, execute, return allocated


resources, and terminate

 When Pi terminates, Pi +1 can obtain its needed resources, and so on

Note: 1.If a system is in safe state  no deadlocks

2. If a system is in unsafe state  possibility of deadlock


Avoidance  ensures that a system will never enter an unsafe state.

1.Resource-Allocation Graph Scheme (single instance of resource)


 Claim edge Pi  Rj indicated that process Pj may request resource Rj; represented by
a dashed line

 Claim edge converts to request edge when a process requests a resource

 Request edge converted to an assignment edge when the resource is allocated to the
process

 When a resource is released by a process, assignment edge reconverts to a claim edge

 Resources must be claimed a priori in the system.Suppose that process Pi requests a


resource Rj .The request can be granted only if converting the request edge to an
assignment edge does not result in the formation of a cycle in the resource
allocation graph.

 Example:
2.Banker’s Algorithm (for multiple instance of a resource type)

 Each process must a priori claim maximum use

 When a process requests a resource it may have to wait

 When a process gets all its resources it must return them in a finite amount of
time

Let n = number of processes, and m = number of resources types. Data


Structures for the Banker’s Algorithm:

 Available: Vector of length m. If available [j] = k, there are k instances of


resource type Rj available

 Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k


instances of resource type Rj

 Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently


allocated k instances of Rj

 Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of


Rj to complete its task

Need [i, j] = Max [i, j] – Allocation [i, j]

Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:

Work = Available

Finish [i] = false for i = 0, 1, …, n- 1

2. Find an i such that both:

(a) Finish [i] = false

(b) 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
Resource-Request Algorithm for Process Pi

It determines if requests can be safely granted. Let Requesti is the request


vector for process Pi.

If Requesti [j] = k then process Pi wants k instances of resource type Rj then


following actions are taken:

1. If Requesti  Needi go to step 2. Otherwise, raise error condition, since


process has exceeded its maximum claim

2. If Request  Available , go to step 3. Otherwise Pi must wait, since


resources are not available

3. Pretend to allocate requested resources to Pi by modifying the state as


follows:

Available = Available – Requesti ;

Allocationi = Allocationi + Requesti;

Needi = Needi – Requesti;

 If safe  the resources are allocated to Pi

 If unsafe  Pi must wait, and the old resource-allocation


state is restored
Example of Banker’s Algorithm
Example of Banker’s Algorithm
3. Deadlock Detection & Recovery
Allow system to enter deadlock state. Then Detection algorithm and recovery schemes are
used to detect and recover.

1. Single Instance of Each Resource Type: All resources have only a single
instance.

 Maintain wait-for graph

1. Nodes are processes

2. Pi  Pj if Pi is waiting for Pj

 Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle,
there exists a deadlock

 An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is


the number of vertices in the graph

2. Several Instances of a Resource Type:


Deadlock detection algorithm employs several time varying data structures.

 Available: A vector of length m indicates the number of available resources of each type

 Allocation: An n x m matrix defines the number of resources of each type currently


allocated to each process
 Request: An n x m matrix indicates the current request of each process. If
Request [i][j] = k, then process Pi is requesting k more instances of resource
type Rj.

1. Let Work and Finish be vectors of length m and n, respectively.

Initialize:

(a) Work = Available

(b)For i = 1,2, …, n, if Allocationi  0, then


Finish[i] = false; otherwise, Finish[i] = true

2. Find an index i such that both:

(a)Finish[i] == false

(b)Requesti  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] == false, for some i, 1  i  n, then the system is in deadlock


state. Moreover, if Finish[i] == false, then Pi is deadlocked

Algorithm requires an order of O(m x n2) operations to detect whether the


system is in deadlocked state
Example of Detection Algorithm
Detection-Algorithm Usage
 When, and how often, to invoke depends on:

 How often a deadlock is likely to occur?

 How many processes will need to be rolled back?

 one for each disjoint cycle

 If detection algorithm is invoked arbitrarily, there may be many cycles in the resource
graph and so we would not be able to tell which of the many deadlocked processes
“caused” the deadlock.

Recovery from Deadlock


There are two methods for recovery:

1. Process Termination

2. Resource Preemption

You might also like