CST 206 OPERATING SYSTEMS
MODULE 3
Process synchronization- Race conditions – Critical section problem – Peterson’s
solution, Synchronization hardware, Mutex Locks, Semaphores, Monitors –
Synchronization problems - Producer Consumer, Dining Philosophers and Readers-
Writers.
Deadlocks: Necessary conditions, Resource allocation graphs, Deadlock prevention,
Deadlock avoidance – Banker’s algorithms, Deadlock detection, Recovery from
deadlock.
PROCESS SYNCHRONIZATION
On the basis of synchronization, processes are categorized as one of the following two types:
• Independent Process
• Cooperative Process
Process synchronization problem arises in the case of Cooperative process because resources are shared
there.
Process synchronization is the task phenomenon of coordinating the execution of processes in such a way
that no two processes can have access to the same shared data and resources.
• It is a procedure that is involved in order to preserve the appropriate order of execution of
cooperative processes. In order to synchronize the processes, there are various synchronization
mechanisms.
• Process Synchronization is mainly needed in a multi-process system when multiple processes are
running together, and more than one processes try to gain access to the same shared resource or
any data at the same time.
RACE CONDITION
A race condition is a problem that occurs in an operating system where two or more processes or threads
are executing concurrently. The outcome of their execution depends on the order in which they are
executed. In a race condition, the exact timing of events is unpredictable, and the outcome of the execution
may vary based on the timing. This can result in unexpected or incorrect behaviour of the system.
For example,
If two threads are simultaneously accessing and changing the same shared resource, such as a variable or
a file, the final state of that resource depends on the order in which the threads execute. If the threads are
not correctly synchronized, they can overwrite each other's changes, causing incorrect results or even
system crashes.
Effects of Race Condition in OS:
In an operating system, a race condition can have several effects, which are :
1 . D e a d l o c k s : A race condition can cause deadlocks, where two or more processes are awaiting
the completion of a job by one another but are unable to move forward, can be brought on by a race
condition. This can happen if two processes try to access the same resource simultaneously, and the
operating system doesn't have a mechanism to ensure that only one can access the resource at a time.
1
, CST 206 OPERATING SYSTEMS
2 . D a t a c o r r u p t i o n : A race condition can cause data corruption, where two or more processes
simultaneously try to write/update the exact memory location. This can cause the data to be overwritten
or mixed up, resulting in incorrect results or program crashes.
3 . S e c u r i t y v u l n e r a b i l i t i e s : A race condition can also create security vulnerabilities in an
operating system. For example, an attacker may be able to exploit a race condition to gain unauthorized
access to a system or to escalate their privileges.
4 . P e r f o r m a n c e d e g r a d a t i o n : In some cases, a race condition can also cause performance
degradation in an operating system. This can happen if multiple processes are competing for the
same resources, such as CPU time or memory, and the operating system is unable to allocate these
resources efficiently.
PROGRAMS AND CRITICAL SECTIONS
• The part of the program (process) that is accessing and changing shared data is called its critical
section.
Critical Section
• The general way to do that is:
• Consider system of n processes {p0, p1, … pn-1}
• Each process has critical section segment of code
2
, CST 206 OPERATING SYSTEMS
• Process may be changing common variables, updating table, writing file, etc
• When one process in critical section, no other may be in its critical section
• Critical section problem is to design protocol to solve this.
• Each process must ask permission to enter critical section in entry section, may follow critical section
with exit section, then remainder section
Critical Section is the part of a program which tries to access shared resources. That resource may be any
resource in a computer like a memory location, data structure, CPU or any IO device .The critical section
cannot be executed by more than one process at the same time; operating system faces the difficulties in
allowing and disallowing the processes from entering the critical section.
The critical section problem is used to design a set of protocols which can ensure that the race condition
among the processes will never arise.
Solution to Critical Section Problem
There are 3 requirements that stand for a correct solution:
1. Mutual Exclusion: If one process is executing inside critical section then the other process must not
enter in the critical section.
3
MODULE 3
Process synchronization- Race conditions – Critical section problem – Peterson’s
solution, Synchronization hardware, Mutex Locks, Semaphores, Monitors –
Synchronization problems - Producer Consumer, Dining Philosophers and Readers-
Writers.
Deadlocks: Necessary conditions, Resource allocation graphs, Deadlock prevention,
Deadlock avoidance – Banker’s algorithms, Deadlock detection, Recovery from
deadlock.
PROCESS SYNCHRONIZATION
On the basis of synchronization, processes are categorized as one of the following two types:
• Independent Process
• Cooperative Process
Process synchronization problem arises in the case of Cooperative process because resources are shared
there.
Process synchronization is the task phenomenon of coordinating the execution of processes in such a way
that no two processes can have access to the same shared data and resources.
• It is a procedure that is involved in order to preserve the appropriate order of execution of
cooperative processes. In order to synchronize the processes, there are various synchronization
mechanisms.
• Process Synchronization is mainly needed in a multi-process system when multiple processes are
running together, and more than one processes try to gain access to the same shared resource or
any data at the same time.
RACE CONDITION
A race condition is a problem that occurs in an operating system where two or more processes or threads
are executing concurrently. The outcome of their execution depends on the order in which they are
executed. In a race condition, the exact timing of events is unpredictable, and the outcome of the execution
may vary based on the timing. This can result in unexpected or incorrect behaviour of the system.
For example,
If two threads are simultaneously accessing and changing the same shared resource, such as a variable or
a file, the final state of that resource depends on the order in which the threads execute. If the threads are
not correctly synchronized, they can overwrite each other's changes, causing incorrect results or even
system crashes.
Effects of Race Condition in OS:
In an operating system, a race condition can have several effects, which are :
1 . D e a d l o c k s : A race condition can cause deadlocks, where two or more processes are awaiting
the completion of a job by one another but are unable to move forward, can be brought on by a race
condition. This can happen if two processes try to access the same resource simultaneously, and the
operating system doesn't have a mechanism to ensure that only one can access the resource at a time.
1
, CST 206 OPERATING SYSTEMS
2 . D a t a c o r r u p t i o n : A race condition can cause data corruption, where two or more processes
simultaneously try to write/update the exact memory location. This can cause the data to be overwritten
or mixed up, resulting in incorrect results or program crashes.
3 . S e c u r i t y v u l n e r a b i l i t i e s : A race condition can also create security vulnerabilities in an
operating system. For example, an attacker may be able to exploit a race condition to gain unauthorized
access to a system or to escalate their privileges.
4 . P e r f o r m a n c e d e g r a d a t i o n : In some cases, a race condition can also cause performance
degradation in an operating system. This can happen if multiple processes are competing for the
same resources, such as CPU time or memory, and the operating system is unable to allocate these
resources efficiently.
PROGRAMS AND CRITICAL SECTIONS
• The part of the program (process) that is accessing and changing shared data is called its critical
section.
Critical Section
• The general way to do that is:
• Consider system of n processes {p0, p1, … pn-1}
• Each process has critical section segment of code
2
, CST 206 OPERATING SYSTEMS
• Process may be changing common variables, updating table, writing file, etc
• When one process in critical section, no other may be in its critical section
• Critical section problem is to design protocol to solve this.
• Each process must ask permission to enter critical section in entry section, may follow critical section
with exit section, then remainder section
Critical Section is the part of a program which tries to access shared resources. That resource may be any
resource in a computer like a memory location, data structure, CPU or any IO device .The critical section
cannot be executed by more than one process at the same time; operating system faces the difficulties in
allowing and disallowing the processes from entering the critical section.
The critical section problem is used to design a set of protocols which can ensure that the race condition
among the processes will never arise.
Solution to Critical Section Problem
There are 3 requirements that stand for a correct solution:
1. Mutual Exclusion: If one process is executing inside critical section then the other process must not
enter in the critical section.
3