Unit Iii Os
Unit Iii Os
2. boolean flag[2];
Synchronization Tools: The Critical Section Problem, Peterson’s Solution, Mutex Locks, Semaphores, Flag array indicates if a process is ready to enter critical section flag[i]=true indicates Pi is ready
Monitors, Classic problems of Synchronization.
to enter the critical section.
Deadlocks: system Model, Deadlock characterization, Methods for handling Deadlocks, Deadlock
prevention, Deadlock avoidance, Deadlock detection, Recovery from Deadlock. Peterson's solution provides a solution to the following problems,
• It ensures that if a process is in the critical section, no other process must be allowed to enter it. This
1. Critical Section Problem in OS:
property is termed mutual exclusion.
It is used to implement process synchronization. Critical Section is a part of the program where shared
• If more than one process wants to enter the critical section, the process that should enter the critical
memory can be accessed like shared variables, shared files etc.
region first must be established. This is termed progress.
Solutions To The Critical Section Problem:
• There is a limit to the number of requests that processors can make to enter the critical region, provided
Any solution to the critical section problem must satisfies three conditions. that a process has already requested to enter and is waiting. This is termed bounding.
1. Mutual exclusion • It provides platform neutrality as this solution is developed to run in user mode, which doesn't require
At a time only one process is in the critical section only when that process comes out of the critical any permission from the kernel.
section then other processes can enter critical section.
Algorithm for process Pi
2. Progress
If no process is executing in critical section and some processes are interested to enter the critical do
section cannot be postponed indefinitely.
{
3. Bounded Waiting
A bound must exists on number of times a process can enter the critical section if other process flag[i]=true;
has made a request to enter the critical section.
turn=j;
General structure of process Pi
while (flag[j]==true&&turn++j);
Entry section
//critical section
It ensures that only one process can enter the critical section. If a process is in critical section than
other processes are not allowed to enter the critical section. Before entering the critical section, flag[i]=false;
the corresponding process will locks critical section.
//remainder section
Critical section
It is part of the program where we can access the shared data. }while(true);
Exit section
It allows a process to exit the critical section here the corresponding process unlocks the critical
3. Mutex Locks:
section. A Mutex (Mutual Exclusion) Lock is a synchronization mechanism used in multithreading to prevent
do multiple threads from accessing a shared resource simultaneously. It ensures that only one thread can
{ execute a critical section at a time, avoiding race conditions and data inconsistency.
Entry section
Critical section Components of Mutex Locks
Exit section 1. Mutex Variable
Remainder section This is a special variable that represents the lock’s state.
}while(true); It can have two states:
2) The readers-writers problem A database is to be shared among several concurrent processes. Some
processes may want only to read the database, some may want to update the database. If two readers
access the shared data simultaneously no problem. If a write, some other process access the database
simultaneously problem arised. Writes have exclusive access to the shared database while writing to the
database. This problem is known as readers- writes problem.
First readers-writers problem No reader be kept waiting unless a writer has already obtained permission
to use the shared resource. Second readers-writes problem: Once writer is ready, that writer performs
its write as soon as possible. A process wishing to modify the shared data must request the lock in write
mode. multiple processes are permitted to concurrently acquire a reader-writer lock in read mode. A
reader writer lock in read mode. but only one process may acquire the lock for writing as exclusive access
is required for writers.
3) Dining Philosophers problem
Five philosophers are seated on 5 chairs across a table. Each philosopher has a plate full of noodles. Each
philosopher needs a pair of forks to eat it. There are only 5 forks available all together. There is only one
fork between any two plates of noodles. In order to eat, a philosopher lifts two forks, one to his left and
the other to his right. if he is successful in obtaining two forks, he starts eating after some time, he stops
eating and keeps both the forks down.
What if all the 5 philosophers decide to eat at the same time? All the 5 philosophers would attempt to c. No Preemption: A resource can be released voluntarily by the process holding it, after the process has
pick up two forks at the same time. So, none of them succeed. One simple solution is to represent each completed its task.
fork with a semaphore. A philosopher tries to grab a fork by executing wait() operation on that d. Circular wait: There exists a set of waiting processes {P0,P1,P2,…Pn} P0 is waiting for a resource held
semaphore. He releases his forks by executing the signal() operation. This solution guarantees that no by P1, P1 is waiting for a resource held by P2.
two neighbours are eating simultaneously. Suppose all 5 philosophers become hungry simultaneously 2.Resource Allocation Graph
and each grabs his left fork, he will be delayed forever. Deadlocks are described by using a directed graph called system resource allocation graph. The graph
7. System Model: consists of set of vertices (v) and set of edges (e).
Types of Vertices:
When processes request a resource and if the resources are not available at that time the a. Process Vertex: Represented as circles (○).
process enters into waiting state. Waiting process may not change its state because the b. Resource Vertex: Represented as rectangles (□). There are two types OS Resource vertex.
resources they are requested are held by other process. This situation is called deadlock. The
Single Instance
situation where the process waiting for the resource i.e., not available is called deadlock.
Multiple Instance
Types of Edges:
Request Edge (P → R): A directed edge from a process to a resource (Process P requests Resource R).
Assignment Edge (R → P): A directed edge from a resource to a process (Resource R is assigned to Process
P).
Example 1 (Single Instances RAG)
A system may consist of finite number of resources and is distributed among number
of processes. There resources are partitioned into several instances each with identical
instances.
A process must request a resource before using it and it must release the resource after
using it. It can request any number of resources to carry out a designated task. The amount of
resource requested may not exceed the total number of resources available.
A process may utilize the resources in only the following sequences:
Request:-If the request is not granted immediately then the requesting process must wait it
can acquire the resources. If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only one
Use:-The process can operate on the resource. instance, then the processes will be in deadlock. For example, if process P1 holds resource R1, process
P2 holds resource R2 and process P1 is waiting for R2 and process P2 is waiting for R1, then process P1
Release:-The process releases the resource after using it. and process P2 will be in deadlock.
Deadlock may involve different types of resources. For eg:-Consider a system with one printer
and one tape drive. If a process Pi currently holds a printer and a process Pj holds the tape
drive. If process Pi request a tape drive and process Pj request a printer then a deadlock occurs.
Multithread programs are good candidates for deadlock because they compete for shared
resources.
8. Deadlock characterization:
A deadlock can be characterized in 2 ways.
1.Necessary conditions
a. Mutual Exclusion: At a time only one process can use a resource. If another process requests that
resource the requesting process must wait until the resource has been released.
b. Hold and Wait: A process holding at least one resource, and is waiting to acquire additional resource
that are held by other process.
Here’s another example, that shows Processes P1 and P2 acquiring resources R1 and R2 while process
P3 is waiting to acquire both resources. In this example, there is no deadlock because there is no circular 2)Deadlock Avoidance
dependency. So cycle in single-instance resource type is the sufficient condition for deadlock. It is used to avoid deadlock. The simplest method is each process must provide additional information
Example 2 (Multi-instances RAG) about maximum number of resources of each resource type that it may need.
When a process requests a resource, operating system decides if request can be satisfied immediately or
not.
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 State
A state is safe if the system can allocate all resources requested by all processes (up to their stated
maximums ) without entering a deadlock state.
More formally, a state is safe if there exists a safe sequence of processes { P0, P1, P2, ..., PN } such
that all of the resource requests for Pi can be granted using the resources currently allocated to
Pi and all processes Pj where j < i. ( I.e. if all the processes prior to Pi finish and free up their
resources, then Pi will be able to finish also, using the resources that they have freed up. )
If a safe sequence does not exist, then the system is in an unsafe state, which MAY lead to
deadlock. ( All safe states are deadlock free, but not all unsafe states lead to deadlocks. )
IMPORTANT QUESTIONS
Classic Problems of Synchronization
Methods for handling Deadlocks
Deadlock characterization
Semaphores
Peterson’s Solution
Mutex locks
Figure: (a) Resource allocation graph. (b) Corresponding wait-for graph
Critical section problem
4)Deadlock Recovery
1. Inform the system operator, and allow him/her to take manual intervention.
2. Terminate one or more processes involved in the deadlock
3. Preempt resources.
1. Process Termination
Two basic approaches, both of which recover resources allocated to terminated processes: