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

Process Synchronization

Uploaded by

Vab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Process Synchronization

Uploaded by

Vab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

PROCESS SYNCHRONIZATION

• Process Synchronization is the task of coordinating the execution of processes in a way that
no two processes can have access to the same shared data and resources.
• It is specially 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 data
at the same time.

Nahida Nazir 11/19/2024


HOW PROCESS SYNCHRONIZATION WORKS?

• For Example, process A changing the data in a memory location while another process B is
trying to read the data from the same memory location. There is a high probability that data
read by the second process will be erroneous.

Nahida Nazir 11/19/2024


P1 • P2
int x = shared; int y = shared;
x++; Y--;
sleep (1); sleep (1);
shared = x; shared = y;

Nahida Nazir 11/19/2024


RACE CONDITION
• A race condition is an undesirable situation that occurs when a device or system attempts to
perform two or more operations at the same time, but because of the nature of the device or
system, the operations must be done in the proper sequence to be done correctly.
• is a situation where several process access and manipulate same data congruently and the
outcome of execution depends on the particular order in which access takes place

Nahida Nazir 11/19/2024


SECTIONS OF A PROGRAM

• Entry Section: It is part of the process which decides the entry of a particular process.
• Critical Section: This part allows one process to enter and modify the shared variable.
• Exit Section: Exit section allows the other process that are waiting in the Entry Section, to
enter into the Critical Sections. It also checks that a process that finished its execution
should be removed through this Section.
• Remainder Section: All other parts of the Code, which is not in Critical, Entry, and Exit
Section, are known as the Remainder Section.

Nahida Nazir 11/19/2024


WHAT IS CRITICAL SECTION PROBLEM?

• A critical section is a segment of code which can be accessed by a signal process at a


specific point of time. The section consists of shared data resources that required to be
accessed by other processes.
• The entry to the critical section is handled by the wait() function, and it is represented as P().
• The exit from a critical section is controlled by the signal() function, represented as V().
• In the critical section, only a single process can be executed. Other processes, waiting to
execute their critical section, need to wait until the current process completes its execution.

Nahida Nazir 11/19/2024


RULES FOR CRITICAL
SECTION
• Mutual Exclusion: Mutual Exclusion is a special type of binary semaphore which is used
for controlling access to the shared resource. It includes a priority inheritance mechanism to
avoid extended priority inversion problems. Not more than one process can execute in its
critical section at one time.
• Progress: This solution is used when no one is in the critical section, and someone wants in.
Then those processes not in their reminder section should decide who should go in, in a
finite time.
• Bound Waiting: When a process makes a request for getting into critical section, there is a
specific limit about number of processes can get into their critical section. So, when the limit
is reached, the system must allow request to the process to get into its critical section.

Nahida Nazir 11/19/2024


PETERSON’S SOLUTION
• Peterson’s Solution:
Peterson’s Algorithm is a simple algorithm that can be run by two processes to ensure
mutual exclusion for one resource.
It does not require any special hardware.
It uses busy waiting technique.
Peterson's Solution is a concurrent programming algorithm for mutual exclusion that allows
two or more processes to share a single-use resource without conflict, using only shared
memory for communication.

Nahida Nazir 11/19/2024


LOCK VARIABLE
SYNCHRONIZATION MECHANISM
• A lock variable provides the simplest synchronization mechanism for
processes.
• Its a software mechanism implemented in user mode, i.e. no support
required from the Operating System.
• Its a busy waiting solution (keeps the CPU busy even when its technically
waiting).
• It can be used for more than two processes.
• When Lock = 0 implies critical section is vacant (initial value ) and Lock =
1 implies critical section occupied.
• The pseudocode looks something like this –

Nahida Nazir 11/19/2024


• Entry section - while(lock != 0);
• Lock = 1;
• //critical section
• Exit section - Lock = 0;

Nahida Nazir 11/19/2024


TEST AND SET INSTRUCTION IN OS

• Process Synchronization problems occur when two processes running concurrently share the same data or same
variable. The value of that variable may not be updated correctly before its being used by a second process. Such
a condition is known as Race Around Condition. There are a software as well as hardware solutions to this
problem.
• //Shared variable lock initialized to false
• boolean lock;

• boolean TestAndSet (boolean &target){


• boolean rv = target;
• target = true;
• return rv;
• }

• while(1){
• while (TestAndSet(lock));
• critical section
• lock = false;
• remainder section
• }
Nahida Nazir 11/19/2024
PRINTER SPOOLER PROBLEM
• Consider a situation where there are multiple processes and only one
printer for printing purposes as the printer is a slower device as compared
to CPU and memory so the spooler comes into the picture. The spooler is
a program that maintains a directory containing a list of files that the
printer has to print. Spooler hands over the file to the printer one by one.
• Every process that wants to keep its file in a spooler needs to execute this
program called Spooler Program:

Nahida Nazir 11/19/2024


• I1. LOAD Ri M[IN]
• I2. STORE SD[Ri] "FILE_NAME"
• I3. INCR Ri
• I4. STORE M[IN] Ri

Nahida Nazir 11/19/2024


Nahida Nazir 11/19/2024
• Process Synchronization problems can be solved using Semaphores or
Mutex.

• Semaphores: A semaphore is an integer variable that can be accessed


using only two functions: wait() and signal(). There are two types of
semaphores: binary semaphores and counting semaphores.

• Mutex: A mutex is a lock that provides mutual exclusion. When the mutex
is used, then only one thread can work with the entire buffer. If the mutex
is used in the printer spooler then only one program can print its file at a
time.

Nahida Nazir 11/19/2024


PETERSON’S ALGORITHM:

The algorithm uses two variables, flag and turn.


The variable turn indicates whose turn it is to enter the critical section.
If turn == i, then process Pi is allowed.
The flag array is used to indicate if a process is ready to enter the critical section.
Flag[i] = true implies that process Pi is ready.

Nahida Nazir 11/19/2024


BUSY WAITING
• The repeated execution of a loop of code while waiting for an event to occur is called busy-
waiting. The CPU is not engaged in any real productive activity during this period, and the
process does not progress toward completion

Nahida Nazir 11/19/2024


SEMAPHORE

• In 1965, Dijkstra proposed a new and very significant technique for managing concurrent
processes by using the value of a simple integer variable to synchronize the progress of
interacting processes. This integer variable is called semaphore
• It is a synchronization tool that works on two atomic operations wait() and signal()

• Its an integer variable used in mutual exclusive manner by various concurrent cooperative
processes in order to achieve synchronization.
Counting semaphore (-infinity to +infinity)
Binary semaphore (0, 1)

Nahida Nazir 11/19/2024


WAIT() AND SIGNAL()
The process of using Semaphores provides two operations: wait (P) and signal (V).
The wait operation decrements the value of the semaphore, and the signal operation increments the value of
the semaphore.

The classical definitions of wait/down and signal/up are:


•Wait: Decrements the value of its argument S, as soon as it would become non-negative(greater than or
equal to 1).
•Signal: Increments the value of its argument S, as there is no more process blocked on the queue.

Nahida Nazir 11/19/2024


SYNTAX

Nahida Nazir 11/19/2024


• The wait() semaphore operation can now be • The signal () semaphore operation can
defined as now be defined as
• wait(semaphore S) • signal(semaphore S)
• { • {
• S.value =S.value-1
• S.value =S.value+1
• }
• }
• if (S.value < 0) {
• if (S.value <= 0) {
• }
}
• add this process (PCB) in Suspend list
sleep(); } • Select a process from suspend list wake
• Else up();
• Return; }
• } }
Nahida Nazir 11/19/2024
BINARY SEMAPHORE
• wait(semaphore S) • signal(semaphore S)
• { • {
• if (S.value == 1) • if (suspend list is empty)
{ {
• S.value=0; • S.value=1;
• } • }
• Else • Else
{ {
Block this process and place in suspend Select a process from suspend list ,and
list , sleep(); wake up();
} }
• } • }
Nahida Nazir 11/19/2024
READERS WRITERS SOLUTION USING BINARY SEMAPHORE

• int rc=0 • down(mutex)


• rc=rc-1;
• semaphore mutex=1;
• if(rc==0) then up(D/B);
• semaphore D/B=1;
• up(mutex)
• void Reader(void) • process data
• { • }
• }
• while(true)
• void write(void)
• { • {
• down(mutex); • while(true)
• • {
rc=rc+1;
• down(D/B);
• if(rc==1) then down(D/B);
• DB
• up(mutex); • up(D/B);
• DB • }
• }
Nahida Nazir 11/19/2024
DINING PHILOSOPHER SOLUTION

• Void Philosopher (void) • Void Philosopher (void)


• { • {
• while(true) • while(true)
• { • {
• THINKING • THINKING
• take_chopstick[i]; • Wait (take_chopstick(Si));
• • Wait ( take_chopstick (S(i+1) %
take_chopstick[ (i+1) % 5] ;
5) );
• EATING THE NOODLE • EATING THE NOODLE
• •
• put_chopstick[i] ); • Signal (put_chopstick(i) );
• put_chopstick[ (i+1) % 5] ; • put_chopstick( (i+1) % 5) ;
• } • }
• } • }
Nahida Nazir 11/19/2024
COUNTING VS BINARY SEMAPHORE

Nahida Nazir 11/19/2024


SEMAPHORE VS MUTEX

Nahida Nazir 11/19/2024


TYPES OF SEMAPHORES

• Counting Semaphores: – Counting Semaphore is defined as a semaphore that contains


integer values, and these values have an unrestricted value domain. A counting semaphore is
helpful to coordinate the resource access, which includes multiple instances.
• Binary Semaphores: – Binary Semaphores are also called Mutex lock. There are two values
of binary semaphores, which are 0 and 1. The value of binary semaphore is initialized to 1.
We use binary semaphore to remove the problem of the critical section with numerous
processes.

Nahida Nazir 11/19/2024


ADVANTAGES

• In the Semaphore, only one process is allowed to enter into the critical section. In this, the
principle of mutual exclusion is to be followed strictly. And the semaphore mechanism is a
more efficient mechanism than other methods which we use in process synchronization.
• It is machine-independent because it is implemented in the microkernel’s machine-
independent code.
• With the help of the semaphore, the resources are managed flexibly.
• In semaphore, there is a busy waiting, so there is no wastage of resources and process time.
• In the semaphore, more threads are permitted to enter into the critical section

Nahida Nazir 11/19/2024


LIMITATION

1.Priority Inversion is a big limitation of semaphores.


2.With improper use, a process may block indefinitely. Such a situation is
called Deadlock. We will be studying deadlocks in details in coming
lessons.

Nahida Nazir 11/19/2024


NUMERICAL

• A counting semaphore S is initialized to 10. Then, 6 P operations and 4 V


operations are performed on S. What is the final value of S?
• Ans
• P means decrement and s means increment
• 10 -6+4 = 8

Nahida Nazir 11/19/2024


NUMERICAL

• A counting semaphore S is initialized to 7. Then, 20 P operations and 15 V operations are performed on


S. What is the final value of S?

• Solution-

• We know-
• P operation also called as wait operation decrements the value of semaphore variable by 1.
• V operation also called as signal operation increments the value of semaphore variable by 1.

• Thus,
• Final value of semaphore variable S
• = 7 – (20 x 1) + (15 x 1)
• = 7 – 20 + 15
• =2
Nahida Nazir 11/19/2024

You might also like