0% found this document useful (0 votes)
46 views16 pages

Process Synchronization

The document discusses process synchronization, categorizing processes into independent and cooperative types, emphasizing the need for synchronization to avoid inconsistent results when sharing resources. It explains critical sections, race conditions, and the critical section problem, detailing the requirements for effective synchronization such as mutual exclusion, progress, and bounded wait. Solutions to the critical section problem involve implementing entry and exit sections to control access to shared resources, ensuring only one process can execute within the critical section at a time.

Uploaded by

Ss
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)
46 views16 pages

Process Synchronization

The document discusses process synchronization, categorizing processes into independent and cooperative types, emphasizing the need for synchronization to avoid inconsistent results when sharing resources. It explains critical sections, race conditions, and the critical section problem, detailing the requirements for effective synchronization such as mutual exclusion, progress, and bounded wait. Solutions to the critical section problem involve implementing entry and exit sections to control access to shared resources, ensuring only one process can execute within the critical section at a time.

Uploaded by

Ss
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

Process Synchronization

Types of Processes:
On the basis of synchronization, processes are categorized as one of the following two
types:

● Independent Process : Execution of one process does not affects the execution of
other processes.
● Cooperative Process : Execution of one process affects the execution of other
processes.

Process synchronization problem arises in the case of Cooperative process also because
resources are shared in Cooperative processes.
Need of Synchronization-

Process synchronization is needed-


● When multiple processes execute concurrently sharing some system
resources.
● To avoid the inconsistent results.

Critical Section-

Critical section is a section of the program where a process access the


shared resources during its execution.
Race Condition in Process Synchronization:
Race Condition

When more than one processes are 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.
Example:
The following illustration shows how
inconsistent results may be produced if
multiple processes execute concurrently
without any synchronization.
Consider-
● Two processes P1 and P2 are
executing concurrently.
● Both the processes share a
common variable named “count”
having initial value = 5.
● Process P1 tries to increment the
value of count.
● Process P2 tries to decrement the
value of count.
Contd.:
In assembly language, the instructions of the processes may be written as-

Now, when these processes execute concurrently without synchronization, different results may
be produced.
Case-01: The execution order of the instructions may be- P1(1), P1(2), P1(3), P2(1), P2(2), P2(3)
In this case, Final value of count = 5
Case-02: The execution order of the instructions may be- P2(1), P2(2), P2(3), P1(1), P1(2), P1(3)
In this case, Final value of count = 5
Case-03: The execution order of the instructions may be- P1(1), P2(1), P2(2), P2(3), P1(2), P1(3)
In this case, Final value of count = 6
Case-04: The execution order of the instructions may be- P2(1), P1(1), P1(2), P1(3), P2(2), P2(3)
In this case, Final value of count = 4
Case-05: The execution order of the instructions may be- P1(1), P1(2), P2(1), P2(2), P1(3),
P2(3)
In this case, Final value of count = 4
It is clear from here that inconsistent results may be produced if multiple processes execute
concurrently without any synchronization.
Race Condition :

Race condition is a situation where-


● The final output produced depends on the execution order of instructions of
different processes.
● Several processes compete with each other.
Critical Section Problem:
● If multiple processes access the critical section concurrently, then results
produced might be inconsistent. This problem is called as critical
section problem.
● Consider system of n processes {p0, p1, … pn-1}
● Each process has critical section segment of code
a. Process may be changing common variables, updating table, writing file, etc.
b. When one process in critical section, no other may be in its critical section
● Critical section problem is to design protocol that the process can use
to cooperate.
How it Should be done ?
• Each process must request permission to enter its critical section.
• The section of code implementing this request is the entry section.
• The critical section may be followed by an exit section.
• The remaining code is the remainder section.
Solution to Critical section problem :
● Adding an entry section before the critical
section
● Adding an exit section after the critical section
Entry Section-

● It acts as a gateway for a process to enter inside


the critical section.
● It ensures that only one process is present inside
the critical section at any time.
● It does not allow any other process to enter inside
the critical section if one process is already
present inside it.
Contd.:
Exit Section-

● It acts as an exit gate for a process to leave the critical section.


● When a process takes exit from the critical section, some changes are made
so that other processes can enter inside the critical section.

How to Synchronize two or more out of Synch


Processes that can cause Inconsistency in the
Output ?
● Synchronization Requirements:
Contd.:
1. Mutual Exclusion-
The mechanism must ensure-
● The Processes access the critical section in a mutual exclusive manner.
● Only one process is present inside the critical section at any time.
● No other process can enter the critical section until the process already present inside it
completes.
2. Progress-
The mechanism must ensure-
● An entry of a process inside the critical section is not dependent on the entry of
another process inside the critical section.
● A process can freely enter inside the critical section if there is no other process
present inside it.
● A process enters the critical section only if it wants to enter.
● A process is not forced to enter inside the critical section if it does not want to enter.
Contd. :
3. Bounded Wait-

The mechanism should ensure-


● The wait of a process to enter the critical section is bounded.
● A process gets to enter the critical section before its wait gets over.

4. Architectural Neutral-
The mechanism should ensure-
● It can run on any architecture without any problem.
● There is no dependency on the architecture.
Important Notes:
Note-01:

● Mutual Exclusion and Progress are the mandatory criteria


● They must be fulfilled by all the synchronization mechanisms

Note-02:

● Bounded waiting and Architectural neutrality are the optional criteria


● However, it is recommended to meet these criteria if possible

You might also like