Process Synchronization in Operating Systems
Process Synchronization in Operating Systems
PROCESS SYNCHRONIZATION
Peterson’s solution
A classic software-based solution to the critical-section problem is known as Peterson’s solution.
The structure of process Pi in Peterson’s solution is as follows:
do
{
flag[i] = true; // when pi wants to enter it sets its flag
turn = j; as true and sets turn = j, gives the
while (flag[j] && turn == j); chance to other process. /* while is
wait loop */
critical section
flag[i] = false;
remainder section
} while (true);
This solution is restricted to two processes that alternate execution between their critical sections
and remainder sections. The processes are numbered P0 and P1. We use Pj for convenience to
denote the other process when Pi is present; that is, j equals 1 − I, Peterson’s solution requires the
two processes to share two data items –
int turn;
boolean flag[2];
1 Prepared by: Dr J Faritha Banu /AP/CSE/SRMIST
SRMIST, RAMAPURAM
Department of Computer Science and Engineering
To prove properties 2 and 3, we note that if a process is stuck in the while loop with the condition
flag[j] == true and turn == j, process Pi can be prevented from entering the critical section
only; this loop is the only one possible. flag[j] will be == false, and Pi can enter its critical section
if Pj is not ready to enter the critical section. If Pj has set, flag[j] = true and is also executing in its
while statement, then either turn == i or turn == j. If turn == i, Pi will enter the critical section
then. Pj will enter the critical section, If turn == j. Although once Pj exits its critical section, it will
reset flag[j] to false, allowing Pi to enter its critical section. Pj must also set turn to i, if Pj resets
flag[j] to true. Hence, since Pi does not change the value of the variable turn while executing the
while statement, Pi will enter the critical section (progress) after at most one entry by Pj (bounded
waiting).
Disadvantage
• Peterson’s solution works for two processes. This solution is also a busy waiting solution
so CPU time is wasted.
[Link]
Synchronization Hardware:
Understanding the two-process solution and the benefits of the synchronization hardware
The hardware-based solution to critical section problem is based on a simple tool i.e. lock. The
solution implies that before entering into the critical section the process must acquire a lock and
must release the lock when it exits its critical section. Using of lock also prevent the race condition.
The hardware synchronization provides two kinds of hardware instructions that are TestAndSet
and Swap.
3 Prepared by: Dr J Faritha Banu /AP/CSE/SRMIST
SRMIST, RAMAPURAM
Department of Computer Science and Engineering
Let’s say process P0 wants to enter the critical section it executes the code above which let while
loop invokes TestAndSet() instruction. Using the TestAndSet() instruction the P0 modifies the
lock value to true to acquire the lock and enters the critical section. Now, when P0 is already in its
critical section process P1 also wants to enter in its critical section. So it will execute the do-while
loop and invoke TestAndSet() instruction only to see that the lock is already set to true which
means some process is in the critical section which will make P1 repeat while loop unless P0 turns
the lock to false.
Once the process P0 complete executing its critical section its will turn the lock variable to false.
Then P1 can modify the lock variable to true using TestAndSet() instruction and enter its critical
section. This is how you can achieve mutual exclusion with the do-while structure above i.e. it let
only one process to execute its critical section at a time.
do {
key = TRUE;
while (key == TRUE)
Swap(&lock, &key);
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
The structure above operates on one global shared Boolean variable lock and another local Boolean
variable key. Both of which are initially set to false. The process P0 interested in executing its
critical section execute code above and set lock as true and enter its critical section. Thus refrain
other processes from executing their critical section satisfying mutual exclusion.
• Processes waiting for entering their critical section consumes a lot of processors time which
increases busy waiting.
• As the selection of processes to enter their critical section is arbitrary. It may happen that
some processes are waiting for the indefinite time which leads to process starvation.
• Deadlock is also possible.
SPIN LOCK : While a process is in its critical section, any other process that tries to enter its
critical section must loop continuously in the call to critical section. This type of mutex lock is
also called a spinlock because the process “spins” while waiting for the lock to become available.
A Semaphore s is an integer variable that can only be accessed via two indivisible (atomic)
operations namely
wait (s)
{
while(s<=0);
s--;
}
The definition of wait() is as follows:
signal (s)
{
s++;
}
All modifications to the integer value of the semaphore in the wait() and signal() operations must
be executed indivisibly. That is, when one process modifies the semaphore value, no other process
6 Prepared by: Dr J Faritha Banu /AP/CSE/SRMIST
SRMIST, RAMAPURAM
Department of Computer Science and Engineering
Semaphore Usage:
The two common kinds of semaphores are Counting semaphores and Binary semaphores.
Binary Semaphores: In Binary semaphores, the value of the semaphore variable will be 0 or 1.
Initially, the value of semaphore variable is set to 1 and if some process wants to use some resource
then the wait() function is called and the value of the semaphore is changed to 0 from 1. The
process then uses the resource and when it releases the resource then the signal() function is called
and the value of the semaphore variable is increased to 1. If at a particular instant of time, the value
of the semaphore variable is 0 and some other process wants to use the same resource then it has
to wait for the release of the resource by the previous process. In this way, process synchronization
can be achieved.
Implementation - Gaining the knowledge of the usage of the semaphores for the Mutual
exclusion mechanisms
Semaphore Implementation
• When a process executes the wait operation and finds that the semaphore value is
not positive, the process can block itself. The block operation places the process into
a waiting queue associated with the semaphore.
• A process that is blocked waiting on a semaphore should be restarted when some
other process executes a signal operation. The blocked process should be restarted
by a wakeup operation which put that process into ready queue.
• To implemented the semaphore, we define a semaphore as a record as:
typedef struct {
int value;
struct process *L;
} semaphore;
if ([Link] <= 0) {
add this process to S.L;
block;
}
signal(S)
{
[Link]++;
remove a process P from S.L;
wakeup(P);
}
Readers writers problem, Bounded Buffer problem gives Good understanding of synchronization
mechanisms.
Bounded Buffer problem is also called producer consumer problem. This problem is generalized
in terms of the Producer-Consumer problem. Solution to this problem is, creating two counting
semaphores “full” and “empty” to keep track of the current number of full and empty buffers
Suppose that a database is to be shared among several concurrent processes. Some of these
processes may want only to read the database, whereas others may want to update (that is, to read
and write) the database. We distinguish between these two types of processes by referring to the
former as readers and to the latter as writers. Precisely in OS we call this situation as the readers-
writers problem. Problem parameters:
• Once a writer is ready, it performs its write. Only one writer may write at a time.
• If a process is writing, no other process can read it.
• If at least one reader is reading, no other process can write.
• Readers may not write and only read.
Dining-Philosophers Problem:
Consider five philosophers who spend their lives thinking and eating. The philosophers share a
circular table surrounded by five chairs, each belonging to one philosopher. In the center of the
table is a bowl of rice, and the table is laid with five single chopsticks When a philosopher thinks,
she does not interact with her colleagues. From time to time, a philosopher gets hungry and tries
to pick up the two chopsticks that are closest to her (the chopsticks that are between her and her
left and right neighbors). A philosopher may pick up only one chopstick at a time. Obviously, she
cannot pick up a chopstick that is already in the hand of a neighbor. When a hungry philosopher
has both her chopsticks at the same time, she eats without releasing the chopsticks. When she is
finished eating, she puts down both chopsticks and starts thinking again.
Semaphore Solution:
One simple solution is to represent each chopstick with a semaphore. A philosopher tries to grab
a chopstick by executing a wait() operation on that semaphore. She releases her chopsticks by
executing the signal() operation on the appropriate semaphores. Thus, the shared data are
semaphore chopstick[5];
do {
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);
...
/* eat for awhile */
...
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
...
/* think for awhile */
...
} while (true);
Suppose that all five philosophers become hungry at the same time and each grabs her left
chopstick. All the elements of chopstick will now be equal to 0. When each philosopher tries to
grab her right chopstick, she will be delayed forever. This situation is known as Deadlock.
Monitors
condition x, y;
monitor DP
test((i + 1) % 5);
self[i].signal () ;
} }
Each philosopher, before starting to eat, must invoke the operation pickup() followed by eating
and finally invoke putdown().
[Link](i);
...
eat
12 Prepared by: Dr J Faritha Banu /AP/CSE/SRMIST
SRMIST, RAMAPURAM
Department of Computer Science and Engineering
This solution ensures that no two neighbors are eating simultaneously and that no deadlocks will
occur. However, with this solution it is possible for a philosopher to starve to death.
CPU Scheduling:
• Whenever the CPU becomes idle, the operating system selects one of the processes in the
ready queue for execution.
• The selection process is carried out by the short-term scheduler (or CPU scheduler).
• The ready queue is not necessarily a first-in, first-out (FIFO) queue. It may be a FIFO
queue, a priority queue, a tree, or simply an unordered linked list.
• CPU scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state
2. When a process switches from the running state to the ready state
3. When a process switches from the waiting state to the ready state
4. When a process terminates
• CPU Scheduling can be preemptive or non-preemptive:
• Non-preemptive Scheduling: In nonpreemptive scheduling, once the CPU has been
allocated a process, the process keeps the CPU until it releases the CPU either by
termination or by switching to the waiting state.
• Preemptive Scheduling: In preemptive scheduling, the CPU is allocated to the processes
for a limited CPU cycle time, When the burst time of the process is greater than CPU
cycle, it is moved back to the ready queue and will execute in the next chance. i.e CPU is
taken back from the current process before it finishes its execution and can be allocated to
other process.
• 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
• Scheduling Criteria
1. CPU utilization: The CPU should be kept 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: Itis the number of processes completed per time unit. For long
processes, this rate may be 1 process per hour; for short transactions, throughput
might be 10 processes per second.
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.
Example:
Process Burst Time
P1 24
P2 3
P3 3
• If the processes arrive in the order PI, P2, P3, and are served in FCFS order, we get the
result shown in the following Gantt chart
Gantt Chart
Example 2:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
❖ Priority Scheduling
A priority is associated with each process, and the CPU is allocated to the process with the
highest priority.( smallest integer - highest priority).
Example :
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
AWT=8.2 ms
• Priority Scheduling can be preemptive or non-preemptive.
• Drawback: Starvation – low priority processes may never execute.
• Solution: Aging – It is a technique of gradually increasing the priority of processes that
wait in the system for a long time.
❖ Round-Robin Scheduling
• The round-robin (RR) scheduling algorithm is designed especially for timesharing
systems.
• It is similar to FCFS scheduling, but preemption is added to switch between
processes.
• A small unit of time, called a time quantum (or time slice), is defined.
• The ready queue is treated as a circular queue.
Example:
Process Burst Time
P1 24
P2 3
P3 3
Waiting time
P1 = (0-0)+ (10-4) = 6
P2 = 4
P3 = 7 (6+4+7 / 3 = 5.66 ms)
• The average waiting time is 17/3 = 5.66 milliseconds.
• The performance of the RR algorithm depends heavily on the size of the time–
quantum.
• If time-quantum is very large(infinite) then RR policy is same as FCFS policy.
• If time quantum is very small, RR approach is called processor sharing and appears
to the users as though each of n process has its own processor running at 1/n the
speed of real processor.
❖ Multilevel Queue Scheduling
Example:
• Consider a multilevel feedback queue scheduler with three queues, numbered from
0 to 2 .
• The scheduler first executes all processes in queue 0.
• Only when queue 0 is empty will it execute processes in queue 1.
• Similarly, processes in queue 2 will be executed only if queues 0 and 1 are empty.
• A process that arrives for queue 1 will preempt a process in queue 2.
• A process that arrives for queue 0 will, in turn, preempt a process in queue 1.
2. Soft real-time systems: In a soft real-time system, if the operation is not performed
within the time limits, outputs / results are degraded. Soft real time provide no guarantee
19 Prepared by: Dr J Faritha Banu /AP/CSE/SRMIST
SRMIST, RAMAPURAM
Department of Computer Science and Engineering
Solution:
Step1: LCM of period (20,5,10) = 20
P2 P2 P3 P3 P1 P2 P2 P2 P2 P2 P2 P2 P2 P2 P2 P2 P2 P2 P2 P2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Explanation:
Time slot 1, 2 -- p2 will execute 2 capacity
Time slot 3, 4 -- p3 will execute 2 capacity
20 Prepared by: Dr J Faritha Banu /AP/CSE/SRMIST
SRMIST, RAMAPURAM
Department of Computer Science and Engineering
Link : [Link]
Priority of task is inversely proportional to deadline i.e., task with shortest deadline is
assigned highest priority. Deadline is time limit in which task has to be completed.
Example –
P1 3 7 20
P2 2 4 5
P3 2 9 10
Solution:
P3 : 03 deadline is 9
P2 P2 P1 P1 P1 P2 P2 P3 P3 P2 P2 P3 P3 P2 P2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Advantages:
Performs well in case of availability of tasks having longer period but shorter deadline.
Disadvantages:
Implementation is complex.
[Link]
Deadlock
Definition: A process requests resources. If the resources are not available at that time ,the
process enters a wait state. Waiting processes may never change state again because the
resources they have requested are held by other waiting processes. This situation is called
a deadlock.
A process must request a resource before using it, and must release resource after
using it.
1. Request: If the request cannot be granted immediately then the requesting process
must wait until it can acquire the resource.
2. Use: The process can operate on the resource
3. Release: The process releases the resource.
Deadlock Characterization
1. Mutual exclusion: At least one resource must be held in a non sharable mode. That is
only one process at a time can use the resource. If another process requests that
resource, the requesting process must be delayed until the resource has been released.
23 Prepared by: Dr J Faritha Banu /AP/CSE/SRMIST
SRMIST, RAMAPURAM
Department of Computer Science and Engineering
4. Circular wait: P0 is waiting for a resource that is held by P1, P1 is waiting for a
resource that is held by P2...Pn-1.
Resource-Allocation Graph
• It is a Directed Graph with a set of vertices V and set of edges E.
• V is partitioned into two types:
1. nodes P = {p1, p2,..pn}
2. Resource type R ={R1,R2,...Rm}
• Pi -->Rj - request => request edge
• Rj-->Pi - allocated => assignment edge.
• Pi is denoted as a circle and Rj as a square.
• Rj may have more than one instance represented as a dot with in the square.
Sets P,R and E.
P = { P1,P2,P3}
R = {R1,R2,R3,R4}
E= {P1->R1, P2->R3, R1->P2, R2->P1, R3->P3 }
• Resource instances
One instance of resource type R1, Two instance of resource type R2,One instance
of resource type R3,Three instances of resource type R4.
Process states
Process P1 is holding an instance of resource type R2, and is waiting for an instance of
resource type R1.
P1->R1->P2->R3->P3->R2->P1
P2->R3->P3->R2->P2
1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection and Recovery
• This ensures that the system never enters the deadlock state.
• Deadlock prevention is a set of methods for ensuring that at least one of the
necessary conditions cannot hold.
• By ensuring that at least one of these conditions cannot hold, we can prevent the
occurrence of a deadlock.
• Whenever a process requests a resource, it does not hold any other resource.
• One technique that can be used requires each process to request and be allocated all
its resources before it begins execution.
• Another technique is before it can request any additional resources, it must release
all the resources that it is currently allocated.
• These techniques have two main disadvantages:
First, resource utilization may be low, since many of the resources may be
allocated but unused for a long time.
We must request all resources at the beginning for both protocols. starvation
is possible.
3. Denying No preemption
• If a Process is holding some resources and requests another resource that cannot be
immediately allocated to it. (that is the process must wait), then all resources
currently being held are preempted. (ALLOW PREEMPTION)
• These resources are implicitly released.
• The process will be restarted only when it can regain its old resources.
• Impose a total ordering of all resource types and allow each process to request for
resources in an increasing order of enumeration.
• Let R = {R1,R2,...Rm} be the set of resource types.
• Assign to each resource type a unique integer number.
• If the set of resource types R includes tapedrives, disk drives and printers.
F(tapedrive)=1,
F(diskdrive)=5,
F(Printer)=12.
▪
A deadlock is an unsafe state.
▪
Not all unsafe states are dead locks
▪
An unsafe state may lead to a dead lock
• Two algorithms are used for deadlock avoidance namely;
1. Resource Allocation Graph Algorithm - single instance of a resource type.
2. Banker’s Algorithm – several instances of a resource type.
• Claim edge - Claim edge Pi---> Rj indicates that process Pi may request resource
Rj at some time, represented by a dashed directed edge.
• When process Pi request resource Rj, the claim edge Pi -> Rj is converted to a request
edge.
Similarly, when a resource Rj is released by Pi the assignment edge Rj -> Pi is reconverted
to a claim edge Pi -> Rj
The request can be granted only if converting the request edge Pi -> Rj to an
assignment edge Rj -> Pi does not form a cycle.
27 Prepared by: Dr J Faritha Banu /AP/CSE/SRMIST
SRMIST, RAMAPURAM
Department of Computer Science and Engineering
• If no cycle exists, then the allocation of the resource will leave the system in a safe
state.
• If a cycle is found, then the allocation will put the system in an unsafe state.
Banker's algorithm
Safety algorithm
Initialize work := available and Finish [i]:=false for i=1,2,3 .. n
Find an i such that both
i. Finish[i]=false
ii. Needi<= Work
if no such i exists, goto step 4
3. work :=work+ allocationi;
Finish[i]:=true
goto step 2
4. If finish[i]=true for all i, then the system is in a safe state
• Now apply the safety algorithm to check whether this new state is safe or not.
• If it is safe then the request from process Pi can be granted.
1. Process Termination
1. Abort all deadlocked processes.
2. Abort one deadlocked process at a time until the deadlock cycle is eliminated.
After each process is aborted , a deadlock detection algorithm must be invoked to
determine where any process is still dead locked.
2. Resource Preemption
Preemptive some resources from process and give these resources to other processes
until the deadlock cycle is broken.
i. Selecting a victim: which resources and which process are to be preempted.
ii. Rollback: if we preempt a resource from a process it cannot continue with its
normal execution. It is missing some needed resource. We must rollback the process to
some safe state, and restart it from that state.
iii. Starvation: How can we guarantee that resources will not always be preempted
from the same process.
Homework:
1. Suppose that the following processes arrive for execution at the times indicated. Each
process will run the listed amount of time. In answering the questions, use non-preemptive
scheduling and base all decisions on the information you have at the time the decision must
be made. (Nov/Dec 2018)
Process Arrival Time Burst Time
P1 0.0 8
P2 0.4 4
P3 1.0 1
a. Find the average turnaround time for these processes with the FCFS scheduling
algorithm?
b. Find the average turnaround time for these processes with the SJF scheduling algorithm?
c. The SJF algorithm is supposed to improve performance, but notice that we chose to run
process P1 at time 0 because we did not know that two shorter processes would arrive soon.
Find what is the average turnaround time will be if the CPU is left idle for the first 1 unit
and then SJF scheduling is used
2. Consider the following set of processes, with the length of the CPU-burst time given in
milliseconds:
a. Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF,
A non pre-emptive priority (a smaller priority number implies a higher priority), and
RR (quantum = 1) scheduling.
b. What is the turnaround time of each process for each of the scheduling algorithms in
part a?
3. Explain the FCFS, preemptive and non-preemptive versions of Shortest Job First and
Round Robin (time-slice2) scheduling algorithms with Gantt Chart for the four processes
given. Compare their average turn around and waiting time
Process Arrival Time Burst Time
P1 0.00 8
P2 1.001 4
P3 2 .001 9