0% found this document useful (0 votes)
2 views19 pages

Operating System Unit-3

Unit III of the Operating System covers synchronization, detailing process synchronization, critical section problems, semaphores, and classic synchronization problems like the Producer-Consumer, Dining Philosophers, and Reader-Writer problems. It also discusses deadlocks, their characterization, and the conditions that lead to deadlocks. The document emphasizes the importance of synchronization mechanisms to manage shared resources and prevent issues such as deadlocks and data inconsistency.

Uploaded by

seshidharlogic28
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)
2 views19 pages

Operating System Unit-3

Unit III of the Operating System covers synchronization, detailing process synchronization, critical section problems, semaphores, and classic synchronization problems like the Producer-Consumer, Dining Philosophers, and Reader-Writer problems. It also discusses deadlocks, their characterization, and the conditions that lead to deadlocks. The document emphasizes the importance of synchronization mechanisms to manage shared resources and prevent issues such as deadlocks and data inconsistency.

Uploaded by

seshidharlogic28
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

Operating System unit-3

Unit III

SYNCHRONIZATION:

Process Synchronization:
The task of coordinating the execution of processes so that no two processes can
access the same shared data and resource is called process synchronization.
Why we need Process Synchronization?
 to prevent deadlock
 helps to manage shared resource like file data hardware devices
In an O.S, there are two types of processes. They are;
1. Independent process
2. Co-operative process
1. Independent process:
 Where execution of one process doesn’t depend on any other process is called
independent process.
2. Co-operative process:
 Here the execution of one process effect the execution of another process is called
cooperative process.
 The co-operative process is implemented with the help of shared memory or message
passing.
Solution for Process Synchronization:
To further solve such situations many more solutions are available which will be discussed in
detail in further –
1. Critical Section
2. Semaphores
3. Mutex Locks

THE CRITICAL SECTION PROBLEM:


1. A "critical section problem" is a problem that deals with the concept of
synchronization.
2. Critical section refers to segment of code or program that tries to access or modify
the value of a variable in shared memory or shared resource.
3. The synchronization mechanism allows the process critical section in a
synchronization code that avoids the inconsistent results between processes
4. If any process is enter into critical section the synchronization code is
automatically waiting The Other process for sometime
5. The following is a synchronization of critical section code.

1
Operating System unit-3

To enter into critical section, a process requests permission through entry section code.
After critical section code is executed, then there is exit section code, to indicate the same.
The main blocks of process are –
1. Entry Section –
 This section decided the entry of a process.
 It is used to acquire the lock on shared resource.
 To enter the critical section code, a process must request permission.
 The entry Section code implements this request.
2. Critical Section –
 When 1 process is executing in its critical section, no other process is allowed
to execute in its critical section.
 This section is helpful to use the shared resource.
3. Exit Section –
 This section allows other process waiting in the entry to enter into critical
section
 it must check if process is completed its execution must be removed from this
section
 remove the lock from resource
4. Remainder Section –
 The remaining code of the process is known as remaining section

The solution to Critical Section Problem


A solution to the critical section problem must satisfy the following three conditions :
1. Mutual Exclusion
Out of a group of cooperating processes, only one process can be in its critical
section at a given point of time.
2. Progress
If no process is in its critical section, and if one or more threads want to execute
their critical section then any one of these threads must be allowed to get into its
critical section.
3. Bounded Waiting
After a process makes a request for getting into its critical section, there is a limit
for how many other processes can get into their critical section, before this
process's request is granted.

SEMAPHORES:

1. It is basically a synchronization tool


2. It is used to solve the critical section problem.
3. It is used to control the access to shared resource by multiple processes
4. It is used for working of process synchronization it is working with wait() & signal().
Characteristics:
 To provide synchronization of tasks.
 Always hold non-negative integer value.
 Used to count [Link] available resources.
2
Operating System unit-3

 Used both wait () and signal ().


How does semaphore work:
1. Count:
• Semaphore maintains “count” that represents the number of available resources.
2. wait:
• Denoted by P(S).
• It control the entry of a process into the critical section.
• It checks count
• If count>0
• Then decrements the count and then access the resource.
• While(S<=0)
{
S--;
}
3. signal:

• Denoted by V(S).
• It control the exit of a task from critical section
• It checks the count
• when a process finishes using the shared resource then it will increments the
semaphore count.
• While(S>=0)
{
S++;
}

Types of semaphore:

Binary semaphore:

1. It is a special form of semaphore used for implementing mutual exclusion,


2. Hence it is often called a Mutex.
3. A binary semaphore is initialized to 1 and only takes the values 0 and 1 using
execution of a program.
4. wait() works only when semaphore is 1.
5. signal() succeeds when semaphore is 0.

3
Operating System unit-3

Counting semaphore:

1. A counting semaphore can have any non-negative integer values.


2. It controls the access to pool of resources.
3. These semaphore are used to coordinate the resource access.
4. Where semaphore count is the number of available resources.
5. If resources are added then the semaphore count is automatically added/incremented,
if removed, count is automatically decreased.
6. These are integer value semaphore and have unrestricted value domain.

Advantages:

 Allows more than one thread to access critical section.


 They do not allow multiple process to enter into critical section.
 It is simple to implement serious synchronization mechanisms such as mutual
exclusion and resource allocation.
 Prevents deadlocks.
 Prevents starvation.
 Improves both system performance and management of resources.

Disadvantages:

 Semaphore programming is so difficult.


 Can lead to programmer error.
 Priority inversion- where high priority process is blocked by low priority
process.
 Not good for large scale use.

CLASSIC PROBLEMS OF SYNCHRONISATION:

 Semaphores can be used in other synchronization problems besides Mutual


Exclusion. Below are some of the classical problems depicting process
synchronization flaws in systems where cooperating processes are present.
We will discuss the following three problems:
1. Bounded Buffer (Producer-Consumer) Problem
2. Dining Philosopher's Problem
3. The Readers Writers Problem
4
Operating System unit-3

1. Producer consumer:

 It is also called as Bounded-buffer problem.


 The problem states that that there is a buffer of ‘n’ slots and each Slot is capable of
storing one unit of data.
 Here there are two processes are running they are namely “producer” and “consumer”
which are operating on buffer.
 Producer process creates an item and adding it to the shared buffer.
 The consumer process takes the item out from the shared buffer and consume them.
 If the producer and consumer process are working on the same buffer concurrently
then critical section problem will be raised.
 So in that case the process will be synchronized by using the concept of semaphore
 Producer and the consumer the process are synchronized with the following three
conditions:

 The producer process must produce an item only when buffer is not full.

do{
wait(empty);
wait(mutex); //acquire lock

produce();

signal(mutex); //release lock


signal(full);

}while(true)
 The consumer process must consume an item only if the shared buffer is not empty.

5
Operating System unit-3

do{
wait(full); //acquire lock
wait(mutex);

consume();

signal(mutex); //release lock


signal(empty);

}while(true)

 The access to the shared buffer must be mutually exclusive.


 The above problem will be solved by using the concept of semaphore there are three
semaphore variables1 semaphore “full”, semaphore “empty”, and semaphore
“mutex”.
 Semaphore full:

1. which tracks the space filled by the producer process


2. It is initialized with the value 0 i.e the buffer will have zero filled space at
beginning
 Semaphore empty:

1. which tracks the empty space in the buffer.


2. it is initially set to buffer size where the value is 1
 Semaphore mutex:

1. it is used for mutual exclusion


2. so that only one process can access the shared buffer at a time
2. Dining philosopher:

 The dining philosopher’s problem is another classic synchronization problem that is


used to evaluate situations where there is a need to allocate multiple resources to
multiple processes.
What is the Problem Statement?
1. five philosophers are sitting around a table, in which there are five chopsticks kept
beside them and a bowl of rice in the centre, when a philosopher wants to eat, he uses
two chopsticks - one from their left and one from their right. When a philosopher
wants to think, he keeps down both chopsticks at their original place
6
Operating System unit-3

2. According to the dining philosopher problem by philosophers are sitting around a


circular table on the job is to think and eat alternatively.
3. A bowl of noodles is placed at the centre of the table along with five chopsticks for
each of the philosophers
4. To eat a philosopher need with the right and left Chopstick a philosopher can eat only
if the both immediate left and right chopsticks of the philosopher is available.
5. In case if both immediate left and right chopsticks of the philosopher or not available
the philosopher put their Chopstick and starts thinking again.
6. According to the above problem an array of five semaphore C[5] for each of these five
chopsticks.
7. Solution is:
• A philosopher must be allowed to pick up the chopsticks only if the both the left and
right chopsticks are available.
• hello only four philosophers to sit on the table the way if all the four philosophers pick
up the four chopsticks there will be one chopstick left on the table so one philosopher
can start eating eventually, two chopsticks will be vailable.
8. The following is the code for synchronizing the philosophers by using the concept of
semaphore:

do{
wait(take_chopstick[i]);
wait(take_chopstick[(i+1)%5]);

eating

signal(put_chopstick[i]); signal(put_chopstick[(i+1)%5]);

thinking
}while(true);

3. Reader-writer problem:

 Readers writer problem is another example of a classic synchronization problem.


 There are many variants of this problem, one of which is examined below.
The Problem Statement
 There is a shared resource which should be accessed by multiple processes. There are
two types of processes in this context. They are reader and writer.
 Any number of readers can read from the shared resource simultaneously, because
there are only reading and not modifying the data.
 but only one writer can write to the shared resource at a time to ensure data integrity.
 When a writer is writing data to the resource, no other process can access the resource.
 A writer cannot write to the resource if there are non-zero number of readers
accessing the resource at that time.

7
Operating System unit-3

The Solution
 From the above problem statement, it is evident that readers have higher priority than
writer. If a writer wants to write to the resource, it must wait until there are no readers
currently accessing that resource.
 Allow multiple reader process to read a file at a same time
 When reader process is read, don't allow writer to write.
 when the writer process is writing we don't allow reader process to read
 no two writers can access the same shared file at the same time

case Process1 Process2 Allowed/not allowed

Case1 Writing Writing not allowed

Case2 Writing Reading not allowed

Case3 Reading Writing not allowed

Case4 Reading Reading Allowed

Part B-Deadlocks

DEADLOCKS:

INTRODUCTION:

 A deadlock is a situation where a set of processes is blocked because each process is


holding a resource and waiting for another resource acquired by some 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.

8
Operating System unit-3

Advantages:
 When programs execute in short bursts and don’t need to share resources.
 Useful when data accuracy matters more than system performance.
 It is useful in various specific situations where data integrity is more important.
 Compile-time checks can be used to enforce deadlocks, which eliminates the
requirement for runtime computation and lowers the possibility of unexpected system
behaviour.
Disadvantages
 It may lead to data integrity issues when not handled properly.  It may lead to an
increase in the costs of the CPU.
DEADLOCK Characterization:

1. Deadlock prerequisite:

I. 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.

[Link] 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
[Link] Pre-emption:

 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
Process1. It will only be released when Process 1 relinquishes it voluntarily after its
execution is complete.

9
Operating System unit-3

[Link] 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.
2. System resource allocation graph:

 A resource allocation graph tracks which resource is held by which process and which
process is waiting for a resource of a particular type.
 The resource allocation graph is the pictorial representation of the state of a system.
As its name suggests, the resource allocation graph is the complete information about
all the processes which are holding some resources or waiting for some resources.
 It also contains the information about all the instances of all the resources whether
they are available or being used by the processes.
 In Resource allocation graph, the process is represented by a Circle while the
Resource is represented by a rectangle. Let's see the types of vertices and edges in
detail.

 Vertices are mainly of two types, Resource and process. Each of them will be
represented by a different shape. Circle represents process while rectangle represents
resource.
 A resource can have more than one instance. Each instance will be represented by a
dot inside the rectangle.

10
Operating System unit-3

 Edges in RAG are also of two types, one represents assignment and other represents
the wait of a process for a resource. The above image shows each of them.
 A resource is shown as assigned to a process if the tail of the arrow is attached to an
instance to the resource and the head is attached to a process.

 A process is shown as waiting for a resource if the tail of an arrow is attached to  the
process while the head is pointing towards the resource

Example: single instance resource deadlock example:

 Let's consider 3 processes P1, P2 and P3, and 3 resources R1, R2,R3. The resources
are having 1 instance each.
 According to the graph, R1 is being used by P1, P2 is holding R2 and waiting for R3,
P3 is waiting for R1 by holding R3.

Here each process already holding one resource but waiting for another processes resource.
Here the situation where deadlock occurs.

11
Operating System unit-3

NECESSARY AND SUFFICIENT CONDITIONS FOR DEADLOCK:

Let’s explain all four conditions related to deadlock in the context of the scenario with two
processes and two resources:

1) Mutual Exclusion
2) Hold and Wait
3) No Pre Emption
4) Circular Wait

1. Mutual Exclusion:

 Mutual Exclusion condition requires that at least one resource be held in a


nonshareable mode, which means that only one process can use the resource at any
given time. Both Resource 1 and Resource 2 are non-shareable in our scenario, and
only one process can have exclusive access to each resource at any given time. As an
example:
 Process 1 obtains Resource 1.
 Process 2 acquires Resource 2.

2. Hold and Wait:

 The hold and wait condition specifies that a process must be holding at least one
resource while waiting for other processes to release resources that are currently held
by other processes. In our example,
 Process 1 has Resource 1 and is awaiting Resource 2.
 Process 2 currently has Resource 2 and is awaiting Resource 1.
 Both processes hold one resource while waiting for the other, satisfying the hold and
wait condition.

3. No Preemption:

 Preemption is the act of taking a resource from a process before it has finished its task.
According to the no preemption condition, resources cannot be taken forcibly from a
process a process can only release resources voluntarily after completing its task.
 For example – Process p1 have resource r1 and requesting for r2 that is hold by
process p2. then process p1 can not preempt resource r2 until process p2 can finish his
execution. After some time it try to restart by requesting both r1 and r2 resources.

4. Circular Wait:

 Circular wait is a condition in which a set of processes are waiting for resources in
such a way that there is a circular chain, with each process in the chain holding a
resource that the next process needs. This is one of the necessary conditions for a
deadlock to occur in a system.
 Example: Imagine four processes—P1, P2, P3, and P4—and four resources—R1, R2,
R3, and R4.
 P1 is holding R1 and waiting for R2 (which is held by P2).
 P2 is holding R2 and waiting for R3 (which is held by P3).

12
Operating System unit-3

 P3 is holding R3 and waiting for R4 (which is held by P4).


 P4 is holding R4 and waiting for R1 (which is held by P1).
 This forms a circular chain where every process is waiting for a resource held by
another, creating a situation where no process can proceed, leading to a deadlock.

DEADLOCK HANDLING APPROACHES:

DEADLOCK PREVENTION:

 The main objective of the deadlock prevention is to break one or more necessary
conditions for deadlock formation
 There are some commonly used deadlock prevention techniques:
1) eliminate mutual exclusion
2) eliminate Hold and weight
3) eliminate no pre-emption
4) eliminate circular wait

1. Eliminate mutual exclusion:

 Eliminate mutual exclusion it is not possible to Dis-satisfy the mutual exclusion,


because in computer system sum of the resources such as printer and tape drivers are
not-shareable.
 For example printer cannot be simultaneously shared by a processes.

2. Eliminate Hold & wait:

 Eliminate Hold and wait there are two protocols we need for eliminating hold & wait
condition.
 According to the first protocol each process must request and get all its resources
before the beginning of its execution.
 To the second protocol a process to request a resources only when its does not occupy
any resource.

3. Eliminate no pre-emption:

 There are also two protocols for eliminating no preemption condition


 According to first protocol process is already holding a resource request another
resource if the requested resource can't be allowed it then it must release all the
resources currently allocated it.
 According to the second protocol when a process request some resource if there are
available then allocate them if request resource is not available then we will check
whether it is used by some other process.
4. Eliminate circular wait:
 The problem of circular wait can be solved by assigning priority numbers to each
resource
 A resource with lower priority cannot be requested by the process then no process can
demand resource i.e already in used by another process.
13
Operating System unit-3

DEADLOCK AVOIDANCE:

 The general idea behind deadlock avoidance is to prevent deadlocks from ever
happening, by preventing at least one of the aforementioned conditions.
 This requires more information about each process.
 In some algorithms the scheduler only needs to know the maximum number of each
resource that a process might potentially use.
 When a scheduler sees that starting a process or granting resource requests may lead to
future deadlocks, then that process is just not started or the request is not granted.
 A resource allocation state is defined by the number of available and allocated
resources and the maximum requirements of all processes in the system.

Safe and Unsafe States


 A system is said to be in a Safe State, if there is a safe execution sequence. An
execution sequence is an ordering for process execution such that each process runs
until it terminates or blocked and all request for resources are immediately granted if
the resource is available.
 A system is said to be in an Unsafe State, if there is no safe execution sequence. An
unsafe state may not be deadlocked, but there is at least one sequence of requests from
processes that would make the system deadlocked.
 A system is in a safe state only there exists a safe sequence.

BANKERS ALGORITHM:

For the Banker's algorithm to work, it needs to know three things:


• How much of each resource could possibly request by each process.
• How much of each resource is currently holding by each process.
• How much of each resource the system currently has available

14
Operating System unit-3

15
Operating System unit-3

16
Operating System unit-3

17
Operating System unit-3

DEADLOCK DETECTION AND RECOVERY:

 Deadlock Detection and Recovery is the mechanism of detecting and resolving


deadlocks in an operating system. In operating systems, deadlock recovery is
important to keep everything running smoothly.
 Deadlock detection is the process of identifying when processes are stuck waiting for
resources held by other processes. Recovery is the method of resolving the deadlock to
allow the system to continue functioning.
 Detection is done using techniques like Resource Allocation Graphs (RAG) or Waitfor
Graphs. Once a deadlock is detected, recovery methods include process termination,
resource pre-emption, or process rollback.
 If deadlocks do occur, the operating system must detect and resolve them. Deadlock
detection algorithms, such as the Wait-For Graph, are used to identify deadlocks,
If Resources Have a Single Instance
 In this case for Deadlock detection, we can run an algorithm to check for the cycle in
the Resource Allocation Graph. The presence of a cycle in the graph is a sufficient
condition for deadlock.

In the above diagram, resource 1 and resource 2 have single instances. There is a cycle
R1 → P1 → R2 → P2. So, Deadlock is Confirmed.
If There are Multiple Instances of Resources
 Detection of the cycle is necessary but not a sufficient condition for deadlock
detection, in this case, the system may or may not be in deadlock varies according to
different situations.
 For systems with multiple instances of resources, algorithms like Banker’s Algorithm
can be adapted to periodically check for deadlocks.
Wait-For Graph Algorithm
 The Wait-For Graph Algorithm is a deadlock detection algorithm used to detect
deadlocks in a system where resources can have multiple instances. The algorithm
works by constructing a Wait-For Graph, which is a directed graph that represents the
dependencies between processes and resources.

Deadlock Recovery:

A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is a
time and space-consuming process. Real-time operating systems use Deadlock recovery.

 Killing The Process: Killing all the processes involved in the deadlock. Killing
process one by one. After killing each process check for deadlock again and keep

18
Operating System unit-3

repeating the process till the system recovers from deadlock. Killing all the processes
one by one helps a system to break circular wait conditions.
 Process Rollback: Rollback deadlocked processes to a previously saved state where
the deadlock condition did not exist. It requires checkpointing to periodically save the
state of processes.
 Resource Preemption: Resources are preempted from the processes involved in the
deadlock, and preempted resources are allocated to other processes so that there is a
possibility of recovering the system from the deadlock. In this case, the system goes
into starvation.
 Process Termination:
Abort: Abort one or more deadlocked processes to release resources.
Restart: Restart the aborted process(es) from the beginning or from a checkpoint.
 Limit Resource Utilization:
Resource Limitation: Limit the number of resources allocated to each process.
 Wound-Wait Scheme
Wound: A process is "wounded" when it is preempted and rolled back to a previous
checkpoint.
Wait: A process waits for a resource to become available before proceeding. 
Deadlock Avoidance
Resource Ordering: Order resources in a way that avoids deadlocks.
Avoid Circular Wait: Avoid circular wait between processes by ordering resources or
using semaphores.
 Concurrency Control: Concurrency control mechanisms are used to prevent data
inconsistencies in systems with multiple concurrent processes. These mechanisms
ensure that concurrent processes do not access the same data at the same time, which
can lead to inconsistencies and errors. Deadlocks can occur in concurrent systems
when two or more processes are blocked, waiting for each other to release the
resources they need. This can result in a system-wide stall, where no process can make
progress. Concurrency control mechanisms can help prevent deadlocks by managing
access to shared resources and ensuring that concurrent processes do not interfere with
each other.

19

You might also like