0% found this document useful (0 votes)
63 views60 pages

O.S - Unit-2&3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views60 pages

O.S - Unit-2&3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

O.

S-----UNIT -2

What is Process Scheduling?


Process Scheduling is an OS task that schedules processes of different states like
ready, waiting, and running.

Process scheduling allows OS to allocate a time interval of CPU execution for each
process. Another important reason for using a process scheduling system is that it
keeps the CPU busy all the time. This allows you to get the minimum response
time for programs.

PROCESS AND CPU SCHEDULING-----

Process
A process is basically a program in execution. The execution of a process must progress in a
sequential fashion.
To put it in simple terms, we write our computer programs in a text file and when we
execute this program, it becomes a process which performs all the tasks mentioned in the
program.
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 –
S.N. Component & Description

A Program In execution
An instance of running program
The entity that can be assigned to and executed as a process
1
Stack
The process Stack contains the temporary data such as method/function parameters, return
address and local variables.

2
Heap
This is dynamically allocated memory to a process during its run time.

3
Text-
This includes the current activity represented by the value of Program Counter and the contents of
the processor's registers.

4
Data
This section contains the global and static variables.

Program
A program is a piece of code which may be a single line or millions of lines. A computer
program is usually written by a computer programmer in a programming language. For
example, here is a simple program written in C programming language −
#include<stdio.h>

int main(){
printf("Hello, World! \n");
return0;
}

A computer program is a collection of instructions that performs a specific task when


executed by a computer. When we compare a program with a process, we can conclude that
a process is a dynamic instance of a computer program.
A part of a computer program that performs a well-defined task is known as an algorithm.
A collection of computer programs, libraries and related data are referred to as a software.

Process Life Cycle


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.

In general, a process can have one of the following five states at a time.

S.N. State & Description

1
Start
This is the initial state when a process is first started/created.

2
Ready
The process is waiting to be assigned to a processor. Ready processes are waiting to have the
processor allocated to them by the operating system so that they can run. Process may come into this
state after Start state or while running it by but interrupted by the scheduler to assign CPU to some
other process.

3
Running
Once the process has been assigned to a processor by the OS scheduler, the process state is set to
running and the processor executes its instructions.

4
Waiting
Process moves into the waiting state if it needs to wait for a resource, such as waiting for user input,
or waiting for a file to become available.

5
Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating system, it is moved to the
terminated state where it waits to be removed from main memory.

Process Control Block (PCB)


S.N. Information & Description

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 in the table –
1
Process State
The current state of the process i.e., whether it is ready, running, waiting, or whatever.

2
Process privileges
This is required to allow/disallow access to system resources.

3
Process ID
Unique identification for each of the process in the operating system.

4
Pointer
A pointer to parent process.

5
Program Counter
Program Counter is a pointer to the address of the next instruction to be executed for this process.

6
CPU registers
Various CPU registers where process need to be stored for execution for running state.

7
CPU Scheduling Information
Process priority and other scheduling information which is required to schedule the process.

8
Memory management information
This includes the information of page table, memory limits, Segment table depending on memory
used by the operating system.

9
Accounting information
This includes the amount of CPU used for process execution, time limits, execution ID etc.

10
IO status information
This includes a list of I/O devices allocated to the process.
The architecture of a PCB is completely dependent on Operating System and may contain
different information in different operating systems. Here is a

simplified diagram of a PCB −

The PCB is maintained for a process throughout its lifetime and is deleted once the process
terminates.
Definition
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


The OS maintains all PCBs in Process Scheduling Queues. The OS maintains a separate
queue for each of the process states and PCBs of all processes in the same execution state
are placed in the same queue. When the state of a process is changed, its PCB is unlinked
from its current queue and moved to its new state queue.
The Operating System maintains the following important process scheduling queues −
 Job queue − This queue keeps all the processes in the system.
 Ready queue − This queue keeps a set of all processes residing in main memory,
ready and waiting to execute. A new process is always put in this queue.
 Device queues − The processes which are blocked due to unavailability of an I/O
device constitute this queue.

The OS can use different policies to manage each queue (FIFO, Round Robin, Priority, etc.).
The OS scheduler determines how to move processes between the ready and run queues
which can only have one entry per processor core on the system; in the above diagram, it has
been merged with the CPU.
Two-State Process Model
Two-state process model refers to running and non-running states which are described
below −

S.N. State & Description

1
Running
When a new process is created, it enters into the system as in the running state.

2
Not Running
Processes that are not running are kept in queue, waiting for their turn to execute. Each entry in the
queue is a pointer to a particular process. Queue is implemented by using linked list. Use of
dispatcher is as follows. When a process is interrupted, that process is transferred in the waiting
queue. If the process has completed or aborted, the process is discarded. In either case, the dispatcher
then selects a process from the queue to execute.

Schedulers
Schedulers are special system software which handle 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 −

 Long-Term Scheduler
 Short-Term Scheduler
 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. Process loads into the memory for CPU scheduling.
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. 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.
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. It is the change of ready state to running state of
the process. 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. A 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 process is called swapping, and the process is said to be swapped
out or rolled out. Swapping may be necessary to improve the process mix.

S.N. Long-Term Scheduler Short-Term Scheduler Medium-Term Scheduler

1 It is a job scheduler It is a CPU scheduler It is a process swapping scheduler.

2 Speed is lesser than short term Speed is fastest among other Speed is in between both short and
scheduler two long term scheduler.

3 It controls the degree of It provides lesser control over It reduces the degree of
multiprogramming degree of multiprogramming multiprogramming.

4 It is almost absent or minimal in It is also minimal in time It is a part of Time sharing systems.
time sharing system sharing system

5 It selects processes from pool and It selects those processes which It can re-introduce the process into
loads them into memory for are ready to execute memory and execution can be
execution continued.

Comparison among Scheduler


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.
Context switches are computationally intensive since register and memory state must be
saved and restored. To avoid the amount of context switching time, some hardware systems
employ two or more sets of processor registers. When the process is switched, the following
information is stored for later use.

 Program Counter
 Scheduling information
 Base and limit register value
 Currently used register
 Changed State
 I/O State information
 Accounting information
Operations on Processes
Process: A process is an activity of executing a program. Basically, it is a program
under execution. Every process needs certain resources to complete its task.

Operation on a Process:
The execution of a process is a complex activity. It involves various operations.
Following are the operations that are performed while execution of a process:
1. Creation: This the initial step of process execution activity. Process creation means
the construction of a new process for the execution. This might be performed by
system, user or old process itself. There are several events that leads to the process
creation. Some of the such events are following:
 When we start the computer, system creates several background processes.
 A user may request to create a new process.
 A process can create a new process itself while executing.
 Batch system takes initiation of a batch job.
2. Scheduling/Dispatching: The event or activity in which the state of the process is
changed from ready to running. It means the operating system puts the process from
ready state into the running state. Dispatching is done by operating system when the
resources are free or the process has higher priority than the ongoing process. There
are various other cases in which the process in running state is preempted and process
in ready state is dispatched by the operating system.
3. Blocking: When a process invokes an input-output system call that blocks the
process and operating system put in block mode. Block mode is basically a mode
where process waits for input-output. Hence on the demand of process itself, operating
system blocks the process and dispatches another process to the processor. Hence, in
process blocking operation, the operating system puts the process in ‘waiting’ state.
4. Preemption: When a timeout occurs that means the process hadn’t been terminated
in the allotted time interval and next process is ready to execute, then the operating
system preempts the process. This operation is only valid where CPU scheduling
supports preemption. Basically this happens in priority scheduling where on the
incoming of high priority process the ongoing process is preempted. Hence, in process
preemption operation, the operating system puts the process in ‘ready’ state.
5. Termination: Process termination is the activity of ending the process. In other
words, process termination is the relaxation of computer resources taken by the
process for the execution. Like creation, in termination also there may be several
events that may lead to the process termination. Some of them are:
 Process completes its execution fully and it indicates to the OS that it has
finished.
 Operating system itself terminates the process due to service errors.
 There may be problem in hardware that terminates the process.
 One process can be terminated by another process.

cooperating processes

Cooperating processes are those that can affect or are affected by other processes running on
the system. Cooperating processes may share data with each other.

Reasons for needing cooperating processes


There may be many reasons for the requirement of cooperating processes. Some of these are
given as follows −

 Modularity

Modularity involves dividing complicated tasks into smaller subtasks. These subtasks
can completed by different cooperating processes. This leads to faster and more
efficient completion of the required tasks.

 Information Sharing

Sharing of information between multiple processes can be accomplished using


cooperating processes. This may include access to the same files. A mechanism is
required so that the processes can access the files in parallel to each other.

 Convenience

There are many tasks that a user needs to do such as compiling, printing, editing etc.
It is convenient if these tasks can be managed by cooperating processes.

 Computation Speedup

Subtasks of a single task can be performed parallely using cooperating processes. This
increases the computation speedup as the task can be executed faster. However, this is
only possible if the system has multiple processing elements.

Methods of Cooperation
Cooperating processes can coordinate with each other using shared data or messages. Details
about these are given as follows −

 Cooperation by Sharing

The cooperating processes can cooperate with each other using shared data such as
memory, variables, files, databases etc. Critical section is used to provide data
integrity and writing is mutually exclusive to prevent inconsistent data.

A diagram that demonstrates cooperation by sharing is given as follows −

In the above diagram, Process P1 and P2 can cooperate with each other using shared
data such as memory, variables, files, databases etc.

 Cooperation by Communication

The cooperating processes can cooperate with each other using messages. This may
lead to deadlock if each process is waiting for a message from the other to perform a
operation. Starvation is also possible if a process never receives a message.

A diagram that demonstrates cooperation by communication is given as follows −


In the above diagram, Process P1 and P2 can cooperate with each other using messages to
communicate.

Thread
What is a Thread?
A thread is a path of execution within a process. A process can contain multiple
threads.
Why Multithreading?
A thread is also known as lightweight process. The idea is to achieve parallelism by
dividing a process into 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. More advantages of multithreading are discussed
below
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 processor. 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 process.
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 Threads
There are two types of threads.
User Level Thread
Kernel Level Thread

Inter Process Communication (IPC)


Scheduling Criteria
Different CPU scheduling algorithms have different properties and the choice
of a particular algorithm depends on the various factors. Many criteria have been
suggested for comparing CPU scheduling algorithms.
The criteria include the following:

1. Maximum CPU utilization


2. Fare allocation of CPU
3. Maximum throughput
4. Minimum turnaround time
5. Minimum waiting time
6. Minimum response time

1. CPU utilisation –
The main objective of any CPU scheduling algorithm is to keep the CPU
as busy as possible. Theoretically, CPU utilisation can range from 0 to 100
but in a real-time system, it varies from 40 to 90 percent depending on the
load upon the system.

2. Throughput –
A measure of the work done by CPU is the number of processes being
executed and completed per unit time. This is called throughput. The
throughput may vary depending upon the length or duration of processes.
3. Turnaround time –
For a particular process, an important criteria is how long it takes to
execute that process. The time elapsed from the time of submission of a
process to the time of completion is known as the turnaround time. Turn-
around time is the sum of times spent waiting to get into memory, waiting
in ready queue, executing in CPU, and waiting for I/O.

4. Waiting time –
A scheduling algorithm does not affect the time required to complete the
process once it starts execution. It only affects the waiting time of a
process i.e. time spent by a process waiting in the ready queue.

5. Response time –
In an interactive system, turn-around time is not the best criteria. A
process may produce some output fairly early and continue computing
new results while previous results are being output to the user. Thus
another criteria is the time taken from submission of the process of request
until the first response is produced. This measure is called response time.

There are various algorithms which are used by the Operating System to schedule the
processes on the processor in an efficient way.

Or

There are the following algorithms which can be used to schedule the jobs.

1. First Come First Serve


It is the simplest algorithm to implement. The process with the minimal arrival time will get
the CPU first. The lesser the arrival time, the sooner will the process gets the CPU. It is the
non-preemptive type of scheduling.

2. Round Robin
In the Round Robin scheduling algorithm, the OS defines a time quantum (slice). All the
processes will get executed in the cyclic way. Each of the process will get the CPU for a
small amount of time (called time quantum) and then get back to the ready queue to wait for
its next turn. It is a preemptive type of scheduling.

Skip Ad

3. Shortest Job First


The job with the shortest burst time will get the CPU first. The lesser the burst time, the
sooner will the process get the CPU. It is the non-preemptive type of scheduling.
4. Shortest remaining time first
It is the preemptive form of SJF. In this algorithm, the OS schedules the Job according to the
remaining time of the execution.

5. Priority based scheduling


In this algorithm, the priority will be assigned to each of the processes. The higher the
priority, the sooner will the process get the CPU. If the priority of the two processes is same
then they will be scheduled according to their arrival time.

6. Highest Response Ratio Next


In this scheduling Algorithm, the process with highest response ratio will be scheduled next.
This reduces the starvation in the system.

Scheduling algorithms

A Process Scheduler schedules different processes to be assigned to the CPU based on


particular scheduling algorithms. There are six popular process scheduling algorithms which
we are going to discuss in this chapter −

 First-Come, First-Served (FCFS) Scheduling


 Shortest-Job-Next (SJN) Scheduling
 Priority Scheduling
 Shortest Remaining Time
 Round Robin(RR) Scheduling
 Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive algorithms
are designed so that once a process enters the running state, it cannot be preempted until it
completes its allotted time, whereas the preemptive scheduling is based on priority where a
scheduler may preempt a low priority running process anytime when a high priority process
enters into a ready state.

First Come First Serve (FCFS)


 Jobs are executed on first come, first serve basis.
 It is a non-preemptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.
Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time

P0 0-0=0

P1 5-1=4

P2 8-2=6

P3 16 - 3 = 13

Average Wait Time: (0+4+6+13) / 4 = 5.75

Shortest Job Next (SJN)


 This is also known as shortest job first, or SJF
 This is a non-preemptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in advance.
 Impossible to implement in interactive systems where required CPU time is not
known.
 The processer should know in advance how much time process will take.
Given: Table of processes, and their Arrival time, Execution time
Process Arrival Time Execution Time Service Time

P0 0 5 0

P1 1 3 5

P2 2 8 14

P3 3 6 8

Waiting time of each process is as follows −

Process Waiting Time

P0 0-0=0

P1 5-1=4

P2 14 - 2 = 12

P3 8-3=5

Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25


Priority Based Scheduling
 Priority scheduling is a non-preemptive algorithm and one of the most common
scheduling algorithms in batch systems.
 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.
 Priority can be decided based on memory requirements, time requirements or any
other resource requirement.
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are
considering 1 is the lowest priority.

Process Arrival Time Execution Time Priority Service Time

P0 0 5 1 0

P1 1 3 2 11

P2 2 8 1 14

P3 3 6 3 5

Waiting time of each process is as follows −

Process Waiting Time


P0 0-0=0

P1 11 - 1 = 10

P2 14 - 2 = 12

P3 5-3=2

Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6

Shortest Remaining Time


 Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
 The processor is allocated to the job closest to completion but it can be preempted by
a newer ready job with shorter time to completion.
 Impossible to implement in interactive systems where required CPU time is not
known.
 It is often used in batch environments where short jobs need to give preference.

Round Robin Scheduling


 Round Robin is the preemptive process scheduling algorithm.
 Each process is provided a fix time to execute, it is called a quantum.
 Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.
 Context switching is used to save states of preempted processes.

Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time


P0 (0 - 0) + (12 - 3) = 9

P1 (3 - 1) = 2

P2 (6 - 2) + (14 - 9) + (20 - 17) = 12

P3 (9 - 3) + (17 - 12) = 11

Average Wait Time: (9+2+12+11) / 4 = 8.5

Multiple-Level Queues 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.

 Multiple queues are maintained for processes with common characteristics.


 Each queue can have its own scheduling algorithms.
 Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in
another queue. The Process Scheduler then alternately selects jobs from each queue and
assigns them to the CPU based on the algorithm assigned to the queue.
Multiple-Processor Scheduling in Operating System
In multiple-processor scheduling multiple CPU’s are available and hence Load
Sharing becomes possible. However multiple processor scheduling is
more complex as compared to single processor scheduling. In multiple processor
scheduling there are cases when the processors are identical i.e. HOMOGENEOUS, in
terms of their functionality, we can use any processor available to run any process in
the queue.

Approaches to Multiple-Processor Scheduling –

One approach is when all the scheduling decisions and I/O processing are handled by
a single processor which is called the Master Server and the other processors
executes only the user code. This is simple and reduces the need of data sharing. This
entire scenario is called Asymmetric Multiprocessing.
A second approach uses Symmetric Multiprocessing where each processor is self
scheduling. All processes may be in a common ready queue or each processor may
have its own private queue for ready processes. The scheduling proceeds further by
having the scheduler for each processor examine the ready queue and select a process
to execute.

Processor Affinity –

Processor Affinity means a processes has an affinity for the processor on which it is
currently running.
When a process runs on a specific processor there are certain effects on the cache
memory. The data most recently accessed by the process populate the cache for the
processor and as a result successive memory access by the process are often satisfied
in the cache memory. Now if the process migrates to another processor, the contents
of the cache memory must be invalidated for the first processor and the cache for the
second processor must be repopulated. Because of the high cost of invalidating and
repopulating caches, most of the SMP(symmetric multiprocessing) systems try to
avoid migration of processes from one processor to another and try to keep a process
running on the same processor. This is known as PROCESSOR AFFINITY.
There are two types of processor affinity:
1. Soft Affinity – When an operating system has a policy of attempting to
keep a process running on the same processor but not guaranteeing it will
do so, this situation is called soft affinity.
2. Hard Affinity – Hard Affinity allows a process to specify a subset of
processors on which it may run. Some systems such as Linux implements
soft affinity but also provide some system calls like sched_setaffinity() that
supports hard affinity.
Load Balancing –

Load Balancing is the phenomena which keeps


the workload evenly distributed across all processors in an SMP system. Load
balancing is necessary only on systems where each processor has its own private
queue of process which are eligible to execute. Load balancing is unnecessary because
once a processor becomes idle it immediately extracts a runnable process from the
common run queue. On SMP(symmetric multiprocessing), it is important to keep the
workload balanced among all processors to fully utilize the benefits of having more
than one processor else one or more processor will sit idle while other processors have
high workloads along with lists of processors awaiting the CPU.
There are two general approaches to load balancing :
1. Push Migration – In push migration a task routinely checks the load on
each processor and if it finds an imbalance then it evenly distributes load on
each processors by moving the processes from overloaded to idle or less
busy processors.
2. Pull Migration – Pull Migration occurs when an idle processor pulls a
waiting task from a busy processor for its execution.

Multicore Processors –

In multicore processors multiple processor cores are places on the same physical
chip. Each core has a register set to maintain its architectural state and thus appears to
the operating system as a separate physical processor. SMP systems that use
multicore processors are faster and consume less power than systems in which each
processor has its own physical chip.
However multicore processors may complicate the scheduling problems. When
processor accesses memory then it spends a significant amount of time waiting for the
data to become available. This situation is called MEMORY STALL. It occurs for
various reasons such as cache miss, which is accessing the data that is not in the cache
memory. In such cases the processor can spend upto fifty percent of its time waiting
for data to become available from the memory. To solve this problem recent hardware
designs have implemented multithreaded processor cores in which two or more
hardware threads are assigned to each core. Therefore if one thread stalls while
waiting for the memory, core can switch to another thread.
There are two ways to multithread a processor :
1. Coarse-Grained Multithreading – In coarse grained multithreading a
thread executes on a processor until a long latency event such as a memory
stall occurs, because of the delay caused by the long latency event, the
processor must switch to another thread to begin execution. The cost of
switching between threads is high as the instruction pipeline must be
terminated before the other thread can begin execution on the processor
core. Once this new thread begins execution it begins filling the pipeline
with its instructions.
2. Fine-Grained Multithreading – This multithreading switches between
threads at a much finer level mainly at the boundary of an instruction cycle.
The architectural design of fine grained systems include logic for thread
switching and as a result the cost of switching between threads is small.

Virtualization and Threading –

In this type of multiple-processor scheduling even a single CPU system acts like a
multiple-processor system. In a system with Virtualization, the virtualization presents
one or more virtual CPU to each of virtual machines running on the system and then
schedules the use of physical CPU among the virtual machines. Most virtualized
environments have one host operating system and many guest operating systems. The
host operating system creates and manages the virtual machines. Each virtual machine
has a guest operating system installed and applications run within that guest.Each
guest operating system may be assigned for specific use cases,applications or users
including time sharing or even real-time operation. Any guest operating-system
scheduling algorithm that assumes a certain amount of progress in a given amount of
time will be negatively impacted by the virtualization. A time sharing operating
system tries to allot 100 milliseconds to each time slice to give users a reasonable
response time. A given 100 millisecond time slice may take much more than 100
milliseconds of virtual CPU time. Depending on how busy the system is, the time
slice may take a second or more which results in a very poor response time for users
logged into that virtual machine. The net effect of such scheduling layering is that
individual virtualized operating systems receive only a portion of the available CPU
cycles, even though they believe they are receiving all cycles and that they are
scheduling all of those cycles.Commonly, the time-of-day clocks in virtual machines
are incorrect because timers take no longer to trigger than they would on dedicated
CPU’s.
Virtualizations can thus undo the good scheduling-algorithm efforts of the operating
systems within virtual machines.

System Call interface for process management

System call provides an interface between user program and operating system. The structure
of system call is as follows −
When the user wants to give an instruction to the OS then it will do it through system calls.
Or a user program can access the kernel which is a part of the OS through system calls.
It is a programmatic way in which a computer program requests a service from the kernel of
the operating system.
Types of system calls
The different system calls are as follows −

 System calls for Process management

 System calls for File management

 System calls for Directory management


Now let us discuss process management system calls.

System calls for Process management


A system is used to create a new process or a duplicate process called a fork.
The duplicate process consists of all data in the file description and registers common. The
original process is also called the parent process and the duplicate is called the child process.
The fork call returns a value, which is zero in the child and equal to the child’s PID (Process
Identifier) in the parent. The system calls like exit would request the services for terminating
a process.
Loading of programs or changing of the original image with duplicate needs execution of
exec. Pid would help to distinguish between child and parent processes.
Example
Process management system calls in Linux.

 fork − For creating a duplicate process from the parent process.

 wait − Processes are supposed to wait for other processes to complete their work.
Types of System Calls Windows Linux

 exec − Loads the selected program into the memory.

 exit − Terminates the process.


The pictorial representation of process management system calls is as follows −

fork() − A parent process always uses a fork for creating a new child process. The child
process is generally called a copy of the parent. After execution of fork, both parent and child
execute the same program in separate processes.
exec() − This function is used to replace the program executed by a process. The child
sometimes may use exec after a fork for replacing the process memory space with a new
program executable making the child execute a different program than the parent.
exit() − This function is used to terminate the process.
wait() − The parent uses a wait function to suspend execution till a child terminates. Using
wait the parent can obtain the exit status of a terminated child.
Process Control CreateProcess() fork()
ExitProcess() exit()
WaitForSingleObject() wait()

File Management CreateFile() open()


ReadFile() read()
WriteFile() write()
CloseHandle() close()

Device Management SetConsoleMode() ioctl()


ReadConsole() read()
WriteConsole() write()

Information Maintenance GetCurrentProcessID() getpid()


SetTimer() alarm()
Sleep() sleep()

Communication CreatePipe() pipe()


CreateFileMapping() shmget()
MapViewOfFile() mmap()

There are many different system calls as shown above. Details of some of those system calls
are as follows −

fork, exit, wait, waitpid, exec


wait()
In some systems, a process may wait for another process to complete its execution. This
happens when a parent process creates a child process and the execution of the parent process
is suspended until the child process executes. The suspending of the parent process occurs
with a wait() system call. When the child process completes execution, the control is returned
back to the parent process.

exec()
This system call runs an executable file in the context of an already running process. It
replaces the previous executable file. This is known as an overlay. The original process
identifier remains since a new process is not created but data, heap, stack etc. of the process
are replaced by the new process.

fork()
Processes use the fork() system call to create processes that are a copy of themselves.
This is one of the major methods of process creation in operating systems. When a parent
process creates a child process and the execution of the parent process is suspended until the
child process executes. When the child process completes execution, the control is returned
back to the parent process.

exit()
The exit() system call is used by a program to terminate its execution. In a multithreaded
environment, this means that the thread execution is complete. The operating system reclaims
resources that were used by the process after the exit() system call.

kill()
The kill() system call is used by the operating system to send a termination signal to a
process that urges the process to exit.However, kill system call does not necessary mean
killing the process and can have various meanings.

wait() and waitpid()


The wait() system call suspends execution of the current process until one of its children
terminates. The call wait(&status) is equivalent to:

waitpid(-1, &status, 0);

The waitpid() system call suspends execution of the current process until a child specified
by pid argument has changed state. By default, waitpid() waits only for terminated children,
but this behaviour is modifiable via the options argument, as described below.
The value of pid can be: (child_pid, &status, options);

Tag Description
meaning wait for any child process whose process group
< -1
ID is equal to the absolute value of pid.

-1 meaning wait for any child process.

meaning wait for any child process whose process group


0
ID is equal to that of the calling process.

meaning wait for the child whose process ID is equal to


>0
the value of pid.

The value of options is an OR of zero or more of the following constants:

PROCESS SCHEDULER_SCHEDULING ALGORITHMS

What is Process Scheduling?


Process Scheduling is an OS task that schedules processes of different states like
ready, waiting, and running.

Process scheduling allows OS to allocate a time interval of CPU execution for each
process. Another important reason for using a process scheduling system is that it
keeps the CPU busy all the time. This allows you to get the minimum response
time for programs.

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 state 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.
n the above-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 .It waits in the ready queue
until it is finally processed for execution. Here, the new process is put in the
ready queue and wait 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.

Two State Process Model


Two-state process models are:

 Running State
 Not Running State
Running
In the Operating system, whenever a new process is built, it is entered into the
system, which should be running.

Not Running
The process that are not running are kept in a queue, which is waiting for their turn
to execute. Each entry in the queue is a point to a specific process.

Scheduling Objectives
Here, are important objectives of Process scheduling

 Maximize the number of interactive users within acceptable response times.


 Achieve a balance between response and utilization.
 Avoid indefinite postponement and enforce priorities.
 It also should give reference to the processes holding the key resources.

Type of Process Schedulers


A scheduler is a type of system software that allows you to handle process
scheduling.

There are mainly three types of Process Schedulers:

1. Long Term Scheduler


2. Short Term Scheduler
3. Medium Term Scheduler

Long Term Scheduler


Long term scheduler is also known as a job scheduler. This scheduler regulates
the program and select process from the queue and loads them into memory for
execution. It also regulates the degree of multi-programing.

However, the main goal of this type of scheduler is to offer a balanced mix of jobs,
like Processor, I/O jobs., that allows managing multiprogramming.

Medium Term Scheduler


Medium-term scheduling is an important part of swapping. It enables you to
handle the swapped out-processes. In this scheduler, a running process can become
suspended, which makes an I/O request.
A running process can become suspended if it makes an I/O request. A suspended
processes can’t make any progress towards completion. In order to remove the
process from memory and make space for other processes, the suspended process
should be moved to secondary storage.

Short Term Scheduler


Short term scheduling is also known as CPU scheduler. The main goal of this
scheduler is to boost the system performance according to set criteria. This helps
you to select from a group of processes that are ready to execute and allocates CPU
to one of them. The dispatcher gives control of the CPU to the process selected by
the short term scheduler.

Difference between Schedulers


Long-Term Vs. Short Term Vs. Medium-Term

Long-Term Short-Term Medium-Term


Long term is also known as a job Short term is also known as CPU Medium-term is
scheduler scheduler swapping sched
It is either absent or minimal in a time- It is insignificant in the time-sharing This scheduler i
sharing system. order. Time-sharing sy
Speed is less compared to the short Speed is the fastest compared to the
It offers medium
term scheduler. short-term and medium-term scheduler.
Allow you to select processes from the It only selects processes that is in a It helps you to s
loads and pool back into the memory ready state of the execution. to memory.
Reduce the leve
Offers full control Offers less control
multiprogramm

What is Context switch?


It is a method to store/restore the state or of a CPU in PCB. So that process
execution can be resumed from the same point at a later time. The context
switching method is important for multitasking OS.

What is CPU Scheduling?


CPU Scheduling is a process of determining which process will own CPU for
execution while another process is on hold. The main task of CPU scheduling is to
make sure that whenever the CPU remains idle, the OS at least select one of the
processes available in the ready queue for execution. The selection process will be
carried out by the CPU scheduler. It selects one of the processes in memory that
are ready for execution.

Types of CPU Scheduling


Here are two kinds of Scheduling methods:

Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their priorities.
Sometimes it is important to run a task with a higher priority before another lower
priority task, even if the lower priority task is still running. The lower priority task
holds for some time and resumes when the higher priority task finishes its
execution.

Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a specific
process. The process that keeps the CPU busy will release the CPU either by
switching context or terminating. It is the only method that can be used for various
hardware platforms. That’s because it doesn’t need special hardware (for example,
a timer) like preemptive scheduling.

When scheduling is Preemptive or Non-Preemptive?


To determine if scheduling is preemptive or non-preemptive, consider these four
parameters:

1. A process switches from the running to the waiting state.


2. Specific process switches from the running state to the ready state.
3. Specific process switches from the waiting state to the ready state.
4. Process finished its execution and terminated.

Only conditions 1 and 4 apply, the scheduling is called non- preemptive.

All other scheduling are preemptive.


Important CPU scheduling Terminologies
 Burst Time/Execution Time: It is a time required by the process to
complete execution. It is also called running time.
 Arrival Time: when a process enters in a ready state
 Finish Time: when process complete and exit from a system
 Multiprogramming: A number of programs which can be present in
memory at the same time.
 Jobs: It is a type of program without any kind of user interaction.
 User: It is a kind of program having user interaction.
 Process: It is the reference that is used for both job and user.
 CPU/IO burst cycle: Characterizes process execution, which alternates
between CPU and I/O activity. CPU times are usually shorter than the time
of I/O.

CPU Scheduling Criteria


A CPU scheduling algorithm tries to maximize and minimize the following:

Maximize:
CPU utilization: CPU utilization is the main task in which the operating system
needs to make sure that CPU remains as busy as possible. It can range from 0 to
100 percent. However, for the RTOS, it can be range from 40 percent for low-level
and 90 percent for the high-level system.
Throughput: The number of processes that finish their execution per unit time is
known Throughput. So, when the CPU is busy executing the process, at that time,
work is being done, and the work completed per unit time is called Throughput.

Minimize:
Waiting time: Waiting time is an amount that specific process needs to wait in the
ready queue.

Response time: It is an amount to time in which the request was submitted until
the first response is produced.

Turnaround Time: Turnaround time is an amount of time to execute a specific


process. It is the calculation of the total time spent waiting to get into the memory,
waiting in the queue and, executing on the CPU. The period between the time of
process submission to the completion time is the turnaround time.

Interval Timer
Timer interruption is a method that is closely related to preemption. When a certain
process gets the CPU allocation, a timer may be set to a specified interval. Both
timer interruption and preemption force a process to return the CPU before its CPU
burst is complete.

Most of the multi-programmed operating system uses some form of a timer to


prevent a process from tying up the system forever.

What is Dispatcher?
It is a module that provides control of the CPU to the process. The Dispatcher
should be fast so that it can run on every context switch. Dispatch latency is the
amount of time needed by the CPU scheduler to stop one process and start another.

Functions performed by Dispatcher:

 Context Switching
 Switching to user mode
 Moving to the correct location in the newly loaded program.

Types of CPU scheduling Algorithm


There are mainly six types of process scheduling algorithms

1. First Come First Serve (FCFS)


2. Shortest-Job-First (SJF) Scheduling
3. Shortest Remaining Time
4. Priority Scheduling
5. Round Robin Scheduling
6. Multilevel Queue Scheduling

Scheduling Algorithms

First Come First Serve


First Come First Serve is the full form of FCFS. It is the easiest and most simple
CPU scheduling algorithm. In this type of algorithm, the process which requests
the CPU gets the CPU allocation first. This scheduling method can be managed
with a FIFO queue.

As the process enters the ready queue, its PCB (Process Control Block) is linked
with the tail of the queue. So, when CPU becomes free, it should be assigned to the
process at the beginning of the queue.
Characteristics of FCFS method:
 It offers non-preemptive and pre-emptive scheduling algorithm.
 Jobs are always executed on a first-come, first-serve basis
 It is easy to implement and use.
 However, this method is poor in performance, and the general wait time is
quite high.

Shortest Remaining Time


The full form of SRT is Shortest remaining time. It is also known as SJF
preemptive scheduling. In this method, the process will be allocated to the task,
which is closest to its completion. This method prevents a newer ready state
process from holding the completion of an older process.

Characteristics of SRT scheduling method:


 This method is mostly applied in batch environments where short jobs are
required to be given preference.
 This is not an ideal method to implement it in a shared system where the
required CPU time is unknown.
 Associate with each process as the length of its next CPU burst. So that
operating system uses these lengths, which helps to schedule the process
with the shortest possible time.

Priority Based Scheduling


Priority scheduling is a method of scheduling processes based on priority. In this
method, the scheduler selects the tasks to work as per the priority.

Priority scheduling also helps OS to involve priority assignments. The processes


with higher priority should be carried out first, whereas jobs with equal priorities
are carried out on a round-robin or FCFS basis. Priority can be decided based on
memory requirements, time requirements, etc.

Round-Robin Scheduling
Round robin is the oldest, simplest scheduling algorithm. The name of this
algorithm comes from the round-robin principle, where each person gets an equal
share of something in turn. It is mostly used for scheduling algorithms in
multitasking. This algorithm method helps for starvation free execution of
processes.
Characteristics of Round-Robin Scheduling
 Round robin is a hybrid model which is clock-driven
 Time slice should be minimum, which is assigned for a specific task to be
processed. However, it may vary for different processes.
 It is a real time system which responds to the event within a specific time
limit.

Shortest Job First


SJF is a full form of (Shortest job first) is a scheduling algorithm in which the
process with the shortest execution time should be selected for execution next. This
scheduling method can be preemptive or non-preemptive. It significantly reduces
the average waiting time for other processes awaiting execution.

Characteristics of SJF Scheduling


 It is associated with each job as a unit of time to complete.
 In this method, when the CPU is available, the next process or job with the
shortest completion time will be executed first.
 It is Implemented with non-preemptive policy.
 This algorithm method is useful for batch-type processing, where waiting
for jobs to complete is not critical.
 It improves job output by offering shorter jobs, which should be executed
first, which mostly have a shorter turnaround time.

Multiple-Level Queues Scheduling


This algorithm separates the ready queue into various separate queues. In this
method, processes are assigned to a queue based on a specific property of the
process, like the process priority, size of the memory, etc.

However, this is not an independent scheduling OS algorithm as it needs to use


other types of algorithms in order to schedule the jobs.

Characteristic of Multiple-Level Queues Scheduling:


 Multiple queues should be maintained for processes with some
characteristics.
 Every queue may have its separate scheduling algorithms.
 Priorities are given for each queue.

The Purpose of a Scheduling algorithm


Here are the reasons for using a scheduling algorithm:

 The CPU uses scheduling to improve its efficiency.


 It helps you to allocate resources among competing processes.
 The maximum utilization of CPU can be obtained with multi-programming.
 The processes which are to be executed are in ready queue.

Summary:
 CPU scheduling is a process of determining which process will own CPU
for execution while another process is on hold.
 In Preemptive Scheduling, the tasks are mostly assigned with their priorities.
 In the Non-preemptive scheduling method, the CPU has been allocated to a
specific process.
 The burst time is the time required for the process to complete execution. It
is also called running time.
 CPU utilization is the main task in which the operating system needs to
ensure that the CPU remains as busy as possible.
 The number of processes that finish their execution per unit time is known
Throughput.
 Waiting time is an amount that a specific process needs to wait in the ready
queue.
 It is the amount of time in which the request was submitted until the first
response is produced.
 Turnaround time is the amount of time to execute a specific process.
 Timer interruption is a method that is closely related to preemption.
 A dispatcher is a module that provides control of the CPU to the process.
 Six types of process scheduling algorithms are: First Come First Serve
(FCFS), 2) Shortest-Job-First (SJF) Scheduling, 3) Shortest Remaining
Time, 4) Priority Scheduling, 5) Round Robin Scheduling, 6) Multilevel
Queue Scheduling.
 In the First Come First Serve method, the process which requests the CPU
gets the CPU allocation first.
 In the Shortest Remaining time, the process will be allocated to the task
closest to its completion.
 In Priority Scheduling, the scheduler selects the tasks to work as per the
priority.
 Round robin scheduling works on the principle where each person gets an
equal share of something in turn.
 In the Shortest job first, the shortest execution time should be selected for
execution next.
 The multilevel scheduling method separates the ready queue into various
separate queues. In this method, processes are assigned to a queue based on
a specific property.
 The CPU uses scheduling to improve its efficiency.
What is Inter Process Communication?
In general, Inter Process Communication is a type of mechanism usually provided by the
operating system (or OS). The main aim or goal of this mechanism is to provide
communications in between several processes. In short, the intercommunication allows a
process letting another process know that some event has occurred.

Let us now look at the general definition of inter-process communication, which will explain
the same thing that we have discussed above.

Definition
"Inter-process communication is used for exchanging useful information between numerous
threads in one or more processes (or programs)."

To understand inter process communication, you can consider the following given diagram
that illustrates the importance of inter-process communication:

47.5M

1.1K

Features of J

Role of Synchronization in Inter Process Communication


It is one of the essential parts of inter process communication. Typically, this is provided by
interprocess communication control mechanisms, but sometimes it can also be controlled by
communication processes.

These are the following methods that used to provide the synchronization:

1. Mutual Exclusion
2. Semaphore
3. Barrier
4. Spinlock

Mutual Exclusion:-

It is generally required that only one process thread can enter the critical section at a time.
This also helps in synchronization and creates a stable state to avoid the race condition.

Semaphore:-

Semaphore is a type of variable that usually controls the access to the shared resources by
several processes. Semaphore is further divided into two types which are as follows:

1. Binary Semaphore

2. Counting Semaphore

Barrier:-

A barrier typically not allows an individual process to proceed unless all the processes does
not reach it. It is used by many parallel languages, and collective routines impose barriers.

Spinlock:-

Spinlock is a type of lock as its name implies. The processes are trying to acquire the
spinlock waits or stays in a loop while checking that the lock is available or not. It is known
as busy waiting because even though the process active, the process does not perform any
functional operation (or task).

Approaches to Interprocess Communication


We will now discuss some different approaches to inter-process communication which are as
follows:
These are a few different approaches for Inter- Process Communication:

1. Pipes
2. Shared Memory
3. Message Queue
4. Direct Communication
5. Indirect communication
6. Message Passing
7. FIFO

To understand them in more detail, we will discuss each of them individually.

Pipe:-
The pipe is a type of data channel that is unidirectional in nature. It means that the data in this
type of data channel can be moved in only a single direction at a time. Still, one can use two-
channel of this type, so that he can able to send and receive data in two processes. Typically,
it uses the standard methods for input and output. These pipes are used in all types of POSIX
systems and in different versions of window operating systems as well.

Shared Memory:-

It can be referred to as a type of memory that can be used or accessed by multiple processes
simultaneously. It is primarily used so that the processes can communicate with each other.
Therefore the shared memory is used by almost all POSIX and Windows operating systems
as well.

Message Queue:-

In general, several different messages are allowed to read and write the data to the message
queue. In the message queue, the messages are stored or stay in the queue unless their
recipients retrieve them. In short, we can also say that the message queue is very helpful in
inter-process communication and used by all operating systems.

To understand the concept of Message queue and Shared memory in more detail, let's take a
look at its diagram given below:

Message Passing:-

It is a type of mechanism that allows processes to synchronize and communicate with each
other. However, by using the message passing, the processes can communicate with each
other without restoring the hared variables.

Usually, the inter-process communication mechanism provides two operations that are as
follows:

o send (message)
o received (message)

Note: The size of the message can be fixed or variable.

Direct Communication:-

In this type of communication process, usually, a link is created or established between two
communicating processes. However, in every pair of communicating processes, only one link
can exist.

Indirect Communication

Indirect communication can only exist or be established when processes share a common
mailbox, and each pair of these processes shares multiple communication links. These shared
links can be unidirectional or bi-directional.

FIFO:-

It is a type of general communication between two unrelated processes. It can also be


considered as full-duplex, which means that one process can communicate with another
process and vice versa.

Some other different approaches


o Socket:-

It acts as a type of endpoint for receiving or sending the data in a network. It is correct for
data sent between processes on the same computer or data sent between different computers
on the same network. Hence, it used by several types of operating systems.

o File:-

A file is a type of data record or a document stored on the disk and can be acquired on
demand by the file server. Another most important thing is that several processes can access
that file as required or needed.

o Signal:-

As its name implies, they are a type of signal used in inter process communication in a
minimal way. Typically, they are the massages of systems that are sent by one process to
another. Therefore, they are not used for sending data but for remote commands between
multiple processes.

Usually, they are not used to send the data but to remote commands in between several
processes.
Why we need interprocess communication?
There are numerous reasons to use inter-process communication for sharing the data. Here
are some of the most important reasons that are given below:

o It helps to speedup modularity


o Computational
o Privilege separation
o Convenience
o Helps operating system to communicate with each other and synchronize their actions as well.

END OF THE UNIT


O.S ---------------------UNIT-III
DEADLOCKS:
Introduction of Deadlock in Operating System
A process in operating system uses resources in the following way.
1) Requests a resource
2) Use the resource
3) Releases the resource

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.
Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in front
of each other. A similar situation occurs in operating systems when there are two or
more processes that hold some resources and wait for resources held by other(s). For
example, in the below diagram, Process 1 is holding Resource 1 and waiting for
resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
Deadlock can arise if the following four conditions hold simultaneously
(Necessary Conditions)
Mutual Exclusion: Two or more resources are non-shareable (Only one process can
use at a time)
Hold and Wait: A process is holding at least one resource and waiting for
resources.
No Preemption: A resource cannot be taken from a process unless the process
releases the resource.
Circular Wait: A set of processes are waiting for each other in circular form.
Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance: The idea is to not let the system into a
deadlock state.
One can zoom into each category individually, Prevention is done by negating one
of above mentioned necessary conditions for deadlock.
Avoidance is kind of futuristic in nature. By using strategy of “Avoidance”, we have
to make an assumption. We need to ensure that all information about resources
which process will need are known to us prior to execution of the process. We use
Banker’s algorithm (Which is in-turn a gift from Dijkstra) in order to avoid
deadlock.
2) Deadlock detection and recovery: Let deadlock occur, then do preemption to
handle it once occurred.
3) Ignore the problem altogether: If deadlock is very rare, then let it happen and
reboot the system. This is the approach that both Windows and UNIX take.
OR
A deadlock happens in operating system when two or more processes need some resource to
complete their execution that is held by the other process.

In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. Similarly
process 2 has resource 2 and needs to acquire resource 1. Process 1 and process 2 are in
deadlock as each of them needs the other’s resource to complete their execution but neither of
them is willing to relinquish their resources.

Coffman Conditions
A deadlock occurs if the four Coffman conditions hold true. But these conditions are not
mutually exclusive.
The Coffman conditions are given as follows −

 Mutual Exclusion

There should be a resource that can only be held by one process at a time. In the
diagram below, there is a single instance of Resource 1 and it is held by Process 1
only.

 Hold and Wait


A process can hold multiple resources and still request more resources from other
processes which are holding them. In the diagram given below, Process 2 holds
Resource 2 and Resource 3 and is requesting the Resource 1 which is held by Process
1.

 No Preemption

A resource cannot be preempted from a process by force. A process can only release a
resource voluntarily. In the diagram below, Process 2 cannot preempt Resource 1
from Process 1. It will only be released when Process 1 relinquishes it voluntarily
after its execution is complete.

 Circular Wait

A process is waiting for the resource held by the second process, which is waiting for
the resource held by the third process and so on, till the last process is waiting for a
resource held by the first process. This forms a circular chain. For example: Process 1
is allocated Resource2 and it is requesting Resource 1. Similarly, Process 2 is
allocated Resource 1 and it is requesting Resource 2. This forms a circular wait loop.
Deadlock Detection
A deadlock can be detected by a resource scheduler as it keeps track of all the resources that
are allocated to different processes. After a deadlock is detected, it can be resolved using the
following methods −

 All the processes that are involved in the deadlock are terminated. This is not a good
approach as all the progress made by the processes is destroyed.
 Resources can be preempted from some processes and given to others till the deadlock is
resolved.
Deadlock Prevention
It is very important to prevent a deadlock before it can occur. So, the system checks each
transaction before it is executed to make sure it does not lead to deadlock. If there is even a
slight chance that a transaction may lead to deadlock in the future, it is never allowed to
execute.

Deadlock Avoidance
It is better to avoid a deadlock rather than take measures after the deadlock has occurred. The
wait for graph can be used for deadlock avoidance. This is however only useful for smaller
databases as it can get quite complex in larger databases.
System model
Overview:
A deadlock occurs when a set of processes is stalled because each process is holding
a resource and waiting for another process to acquire another resource. In the
diagram below, for example, Process 1 is holding Resource 1 while Process 2
acquires Resource 2, and Process 2 is waiting for Resource 1.

System Model :
 For the purposes of deadlock discussion, a system can be modeled as a
collection of limited resources that can be divided into different categories
and allocated to a variety of processes, each with different requirements.
 Memory, printers, CPUs, open files, tape drives, CD-ROMs, and other
resources are examples of resource categories.
 By definition, all resources within a category are equivalent, and any of
the resources within that category can equally satisfy a request from that
category. If this is not the case (i.e. if there is some difference between the
resources within a category), then that category must be subdivided
further. For example, the term “printers” may need to be subdivided into
“laser printers” and “color inkjet printers.”
 Some categories may only have one resource.
 The kernel keeps track of which 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 for all kernel-managed
resources. Mutexes or wait() and signal() calls can be used to control
application-managed resources (i.e. binary or counting semaphores. )
 When every process in a set is waiting for a resource that is currently
assigned to another process in the set, the set is said to be deadlocked.
Operations:
In normal operation, a process must request a resource before using it and release it
when finished, as shown below.
1. Request –
If the request cannot be granted immediately, the process must wait until
the resource(s) required to become available. The system, for example,
uses the functions open(), malloc(), new(), and request ().
2. Use –
The process makes use of the resource, such as printing to a printer or
reading from a file.
3. Release –
The process relinquishes the resource, allowing it to be used by other
processes.

NECESSARY CONDITIONS
There are four conditions that must be met in order to achieve deadlock as
follows.
1. Mutual Exclusion –
At least one resource must be kept in a non-shareable state; if another
process requests it, it must wait for it to be released.

2. Hold and Wait –


A process must hold at least one resource while also waiting for at
least one resource that another process is currently holding.
3. No preemption –
Once a process holds a resource (i.e. after its request is granted), that
resource cannot be taken away from that process until the process
voluntarily releases it.

4. Circular Wait –
There must be a set of processes P0, P1, P2,…, PN such that every
P[I] is waiting for P[(I + 1) percent (N + 1)]. (It is important to note that
this condition implies the hold-and-wait condition, but dealing with the
four conditions is easier if they are considered separately).

Methods for Handling Deadlocks :


In general, there are three approaches to dealing with deadlocks as follows.
1. Preventing or avoiding deadlock by Avoid allowing the system to
become stuck in a loop.
2. Detection and recovery of deadlocks, When deadlocks are
detected, abort the process or preempt some resources.
3. Ignore the problem entirely.
4. To avoid deadlocks, the system requires more information about all
processes. The system, in particular, must understand what
resources a process will or may request in the future. ( Depending
on the algorithm, this can range from a simple worst-case maximum
to a complete resource request and release plan for each process. )
5. Deadlock detection is relatively simple, but deadlock recovery
necessitates either aborting processes or preempting resources,
neither of which is an appealing option.
6. If deadlocks are not avoided or detected, the system will gradually
slow down as more processes become stuck waiting for resources
that the deadlock has blocked and other waiting processes.
Unfortunately, when the computing requirements of a real-time
process are high, this slowdown can be confused with a general
system slowdown.

Deadlock Prevention:
Deadlocks can be avoided by avoiding at least one of the four necessary
conditions: as follows.
Condition-1:
Mutual Exclusion :
 Read-only files, for example, do not cause deadlocks.
 Unfortunately, some resources, such as printers and tape drives,
require a single process to have exclusive access to them.
Condition-2 :
Hold and Wait :
To avoid this condition, processes must be prevented from holding one or
more resources while also waiting for one or more others. There are a few
possibilities here:
 Make it a requirement that all processes request all resources at
the same time. This can be a waste of system resources if a
process requires one resource early in its execution but does not
require another until much later.
 Processes that hold resources must release them prior to
requesting new ones, and then re-acquire the released resources
alongside the new ones in a single new request. This can be a
problem if a process uses a resource to partially complete an
operation and then fails to re-allocate it after it is released.
 If a process necessitates the use of one or more popular resources,
either of the methods described above can result in starvation.
Condition-3 :
No Preemption :
When possible, preemption of process resource allocations can help to avoid
deadlocks.
 One approach is that if a process is forced to wait when requesting
a new resource, all other resources previously held by this process
are implicitly released (preempted), forcing this process to re-
acquire the old resources alongside the new resources in a single
request, as discussed previously.
 Another approach is that when a resource is requested, and it is not
available, the system looks to see what other processes are
currently using those resources and are themselves blocked while
waiting for another resource. If such a process is discovered, some
of their resources may be preempted and added to the list of
resources that the process is looking for.
 Either of these approaches may be appropriate for resources
whose states can be easily saved and restored, such as registers
and memory, but they are generally inapplicable to other devices,
such as printers and tape drives.

Condition-4 :
Circular Wait :
 To avoid circular waits, number all resources and insist that
processes request resources is strictly increasing ( or decreasing)
order.
 To put it another way, before requesting resource Rj, a process
must first release all Ri such that I >= j.
 The relative ordering of the various resources is a significant
challenge in this scheme.
Deadlock Avoidance :
 The general idea behind deadlock avoidance is to avoid deadlocks
by avoiding at least one of the aforementioned conditions.
 This necessitates more information about each process AND
results in low device utilization. (This is a conservative approach.)
 The scheduler only needs to know the maximum number of each
resource that a process could potentially use in some algorithms. In
more complex algorithms, the scheduler can also use the schedule
to determine which resources are required and in what order.
 When a scheduler determines that starting a process or granting
resource requests will result in future deadlocks, the process is
simply not started or the request is denied.
 The number of available and allocated resources, as well as the
maximum requirements of all processes in the system, define a
resource allocation state.
Deadlock Detection :
 If deadlocks cannot be avoided, another approach is to detect them
and recover in some way.
 Aside from the performance hit of constantly checking for
deadlocks, a policy/algorithm for recovering from deadlocks must
be in place, and when processes must be aborted or have their
resources preempted, there is the possibility of lost work.
Recovery From Deadlock :
There are three basic approaches to getting out of a bind:
1. Inform the system operator and give him/her permission to
intervene manually.
2. Stop one or more of the processes involved in the deadlock.
3. Prevent the use of resources.

Approach of Recovery From Deadlock :


Here, we will discuss the approach of Recovery From Deadlock as follows.
Approach-1 :
Process Termination :
There are two basic approaches for recovering resources allocated to
terminated processes as follows.
1. Stop all processes that are involved in the deadlock. This does
break the deadlock, but at the expense of terminating more
processes than are absolutely necessary.
2. Processes should be terminated one at a time until the deadlock is
broken. This method is more conservative, but it necessitates
performing deadlock detection after each step.
In the latter case, many factors can influence which processes are
terminated next as follows.
1. Priorities in the process
2. How long has the process been running and how close it is to
completion.
3. How many and what kind of resources does the process have? (Are
they simple to anticipate and restore? )
4. How many more resources are required for the process to be
completed?
5. How many processes will have to be killed?
6. Whether the process is batch or interactive.
Approach-2 :
Resource Preemption :
When allocating resources to break the deadlock, three critical issues must
be addressed:
1. Selecting a victim –
Many of the decision criteria outlined above apply to determine
which resources to preempt from which processes.
2. Rollback –
A preempted process should ideally be rolled back to a safe state
before the point at which that resource was originally assigned to
the process. Unfortunately, determining such a safe state can be
difficult or impossible, so the only safe rollback is to start from the
beginning. (In other words, halt and restart the process.)
3. Starvation –
How do you ensure that a process does not go hungry because its
resources are constantly being preempted? One option is to use a
priority system and raise the priority of a process whenever its
resources are preempted. It should eventually gain a high enough
priority that it will no longer be preempted.

You might also like