0% found this document useful (0 votes)
6 views

OS_Unit2

Process synchronization in operating systems ensures that cooperative processes execute in a controlled order to prevent conflicts and maintain data consistency. Key concepts include race conditions, critical sections, semaphores, and deadlocks, which can occur when multiple processes hold resources and wait for others in a circular manner. Techniques for handling deadlocks include prevention, avoidance, detection, and recovery methods to maintain system efficiency and correctness.

Uploaded by

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

OS_Unit2

Process synchronization in operating systems ensures that cooperative processes execute in a controlled order to prevent conflicts and maintain data consistency. Key concepts include race conditions, critical sections, semaphores, and deadlocks, which can occur when multiple processes hold resources and wait for others in a circular manner. Techniques for handling deadlocks include prevention, avoidance, detection, and recovery methods to maintain system efficiency and correctness.

Uploaded by

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

Unit:2 Process Management

Process Synchronization in OS (Operating System)

When two or more process cooperates with each other, their


order of execution must be preserved otherwise there can be
conflicts in their execution and inappropriate outputs can be
produced.

A cooperative process is the one which can affect the execution


of other process or can be affected by the execution of other
process. Such processes need to be synchronized so that their
order of execution can be guaranteed.

The procedure involved in preserving the appropriate order of


execution of cooperative processes is known as Process
Synchronization. There are various synchronization
mechanisms that are used to synchronize the processes.

Process Synchronization is the coordination of execution of


multiple processes in a multi-process system to ensure that
they access shared resources in a controlled and predictable
manner. It aims to resolve the problem of race conditions and
other synchronization issues in a concurrent system.
The main objective of process synchronization is to ensure
that multiple processes access shared resources without
interfering with each other and to prevent the possibility of
inconsistent data due to concurrent access. To achieve this,
various synchronization techniques such as semaphores,
monitors, and critical sections are used.
In a multi-process system, synchronization is necessary to
ensure data consistency and integrity, and to avoid the risk of
deadlocks and other synchronization problems. Process
synchronization is an important aspect of modern operating
systems, and it plays a crucial role in ensuring the correct and
efficient functioning of multi-process systems.

Race Condition

A Race Condition typically occurs when two or more threads try


to read, write and possibly make the decisions based on the
memory that they are accessing concurrently.
When more than one process is executing the same
code or accessing the same memory or any shared
variable in that condition there is a possibility that the
output or the value of the shared variable is wrong so
for that all the processes doing the race to say that my
output is correct this condition known as a race
condition. Several processes access and process the
manipulations over the same data concurrently, then the
outcome depends on the particular order in which the access
takes place. A race condition is a situation that may occur
inside a critical section. This happens when the result of
multiple thread execution in the critical section differs
according to the order in which the threads execute. Race
conditions in critical sections can be avoided if the critical
section is treated as an atomic instruction. Also, proper thread
synchronization using locks or atomic variables can prevent
race conditions.

Critical Section

The regions of a program that try to access shared resources


and may cause race conditions are called critical section. To
avoid race condition among the processes, we need to assure
that only one process at a time can execute within the critical
section.

Critical Section Problem


A critical section is a code segment that can be accessed by
only one process at a time. The critical section contains shared
variables that need to be synchronised to maintain the
consistency of data variables. So the critical section problem
means designing a way for cooperative processes to access
shared resources without creating data inconsistencies.
In the entry section, the process requests for entry in
the Critical Section.
Any solution to the critical section problem must satisfy three
requirements:
 Mutual Exclusion: If a process is executing in its
critical section, then no other process is allowed to
execute in the critical section.
 Progress: If no process is executing in the critical
section and other processes are waiting outside the
critical section, then only those processes that are not
executing in their remainder section can participate in
deciding which will enter in the critical section next,
and the selection can not be postponed indefinitely.
 Bounded Waiting: We should be able to predict the
waiting time for every process to get into the critical
section. The process must not be endlessly waiting for
getting into the critical section.
Semaphores
Semaphore is a Hardware Solution. This Hardware solution is written or given
to critical section problem.
What is a Critical Section Problem?

The Critical Section Problem is a Code Snippet. This code


snippet contains a few variables. These variables can be
accessed by a few processes. There is a condition for these
processes.

A semaphore uses two atomic operations, wait and signal for


process synchronization.
A Semaphore is an integer variable, which can be accessed
only through two operations wait() and signal().
There are two types of semaphores: Binary Semaphores and
Counting Semaphores.
What is a Critical Section Problem?

The Critical Section Problem is a Code Snippet. This code


snippet contains a few variables. These variables can be
accessed by a few processes. There is a condition for these
processes.

Problems in Critical Section Problems

There may be a state where one or more processes try to enter


the critical state. After multiple processes enter the Critical
Section, the second process try to access variable which
already accessed by the first process.

Explanation

Suppose there is a variable which is also known as shared


variable. Let us define that shared variable.

Here, x is the shared variable.

1. int x = 10;

Process 1

1. // Process 1
2. int s = 10;
3. int u = 20;
4. x = s + u;
Process 2
Process 2

int s = 10;

int u = 20;

x = s - u;

If the process is accessed the x shared variable one after


other, then we are going to be in a good position.

If Process 1 is alone executed, then the value of x is denoted


as x = 30;

The shared variable x changes to 30 from 10

If Process 2 is alone executed, then the value of x is denoted


as x = -10;

The shared variable x changes to -10 from 30

If both the processes occur at the same time, then the


compiler would be in a confusion to choose which variable
value i.e. -10 or 30. This state faced by the variable x is Data
Inconsistency. These problems can also be solved by Hardware
Locks
To, prevent such kind of problems can also be solved by
Hardware solutions named Semaphores.

1. Binary Semaphore

Here, there are only two values of Semaphore in Binary


Semaphore Concept. The two values are 1 and 0.
If the Value of Binary Semaphore is 1, then the process has the
capability to enter the critical section area. If the value of
Binary Semaphore is 0 then the process does not have the
capability to enter the critical section area.

2. Counting Semaphore

Here, there are two sets of values of Semaphore in Counting


Semaphore Concept. The two types of values are values
greater than and equal to one and other type is value equal to
zero.

If the Value of Binary Semaphore is greater than or equal to 1,


then the process has the capability to enter the critical section
area. If the value of Binary Semaphore is 0 then the process
does not have the capability to enter the critical section area.

Deadlock in OS
A process in operating system uses resources in the following
way.
1. Requests a resource
2. Use the resource
3. Releases the resource
A 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.
Examples Of Deadlock
1. The system has 2 tape drives. P1 and P2 each hold one
tape drive and each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in
deadlock as follows:
 P0 executes wait(A) and preempts.
 P1 executes wait(B).
 Now P0 and P1 enter in deadlock.

P0 P1

wait(A);
wait(B)

wait(B); wait(A)
3. Assume the space is available for allocation of 200K
bytes, and the following sequence of events occurs.

P0
P1

Request 80KB; Request 70KB;


P0
P1

Request 60KB; Request 80KB;

Deadlock occurs if both processes progress to their second


request.
Deadlock can arise if the following four conditions hold
simultaneously (Necessary Conditions)
1. Mutual Exclusion

A resource can only be shared in mutually exclusive


manner. It implies, if two process cannot use the same
resource at the same time.

2. Hold and Wait

A process waits for some resources while holding another


resource at the same time.

3. No preemption

The process which once scheduled will be executed till the


completion. No other process can be scheduled by the
scheduler meanwhile.

4. Circular Wait

All the processes must be waiting for the resources in a


cyclic manner so that the last process is waiting for the
resource which is being held by the first process.

Methods for handling deadlock


There are three ways to handle deadlock
1) Deadlock prevention or avoidance:
Prevention:
The idea is to not let the system into a deadlock state. This
system will make sure that above mentioned four conditions
will not arise. These techniques are very costly so we use this
in cases where our priority is making a system deadlock-free.
One can zoom into each category individually; Prevention is
done by negating one of the above-mentioned necessary
conditions for deadlock. Prevention can be done in four
different ways:
1. Eliminate mutual exclusion
3. Allow pre-emption
2. Solve hold and Wait
4. Circular wait Solution
Avoidance:
Avoidance is kind of futuristic. By using the strategy of
“Avoidance”, we have to make an assumption. We need to
ensure that all information about resources that the process
will need is known to us before the execution of the process.
We use Banker’s algorithm (Which is in turn a gift from
Dijkstra) to avoid deadlock.
In prevention and avoidance, we get the correctness of data
but performance decreases.
2) Deadlock detection and recovery: If Deadlock
prevention or avoidance is not applied to the software then we
can handle this by deadlock detection and recovery. which
consist of two phases:
1. In the first phase, we examine the state of the process
and check whether there is a deadlock or not in the
system.
2. If found deadlock in the first phase then we apply the
algorithm for recovery of the deadlock.
In Deadlock detection and recovery, we get the correctness of
data but performance decreases.
Recovery from Deadlock
1. Manual Intervention:
When a deadlock is detected, one option is to inform the
operator and let them handle the situation manually. While
this approach allows for human judgment and decision-
making, it can be time-consuming and may not be feasible in
large-scale systems.
3. Automatic Recovery:
An alternative approach is to enable the system to recover
from deadlock automatically. This method involves breaking
the deadlock cycle by either aborting processes or pre-
empting resources.
Difference between Starvation and Deadlock

Sr Deadlock Starvation
.

1 Deadlock is a situation Starvation is a situation


where no process got where the low priority
blocked and no process process got blocked and the
proceeds high priority processes
proceed.

2 Deadlock is an infinite Starvation is a long waiting


waiting. but not infinite.

3 Every Deadlock is always a Every starvation need not be


starvation. deadlock.

4 The requested resource is The requested resource is


blocked by the other continuously be used by the
process. higher priority processes.

Resource Allocation Graph

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

Let's consider 3 processes P1, P2 and P3, and two types of


resources R1 and R2. The resources are having 1 instance
each.

According to the graph, R1 is being used by P1, P2 is holding R2


and waiting for R1, P3 is waiting for R1 as well as R2.

The graph is deadlock free since no cycle is being formed in the


graph.
Deadlock Characteristics

A deadlock happens in operating system when two or more


processes need same resource to complete their execution that
is held by the other process.

A deadlock occurs if the four Coffman conditions hold true. But


these conditions are not mutually exclusive. They 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 Pre-emption

A resource cannot be pre-empted 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy