Process Synchronization
Process Synchronization
Process Synchronization
SYNCHRONIZAT
ION
INTRODUCTION
• When two or more process cooperate 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 processes or can be affected by the execution of other processes.
• 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.
RACE CONDITION
• A Race Condition typically occurs when two or
more threads or processes try to read, write
and possibly make the decisions based on the
memory that they are accessing concurrently.
CRITICAL SECTION
• The regions of a program that try to access shared resources and may
cause race conditions are called critical section.
• A critical section refers to a segment of code that is executed by
multiple concurrent threads or processes, and which accesses shared
resources.
• When more than one processes try to access the same
code segment, that segment is known as the critical
section.
• To avoid race condition among the processes, we need to ensure that
only one process at a time can execute within the critical section.
THE CRITICAL SECTION PROBLEM
• Entry Section →
• While (lock! = 0);
• Lock = 1;
• //Critical Section
• Exit Section →
• Lock =0;
• If we look at the Pseudo Code, we find that there are three sections in the
code.
• Entry Section, Critical Section and the exit section.
• Initially the value of lock variable is 0.
• The process which needs to get into the critical section, enters into the
entry section and checks the condition provided in the while loop.
• The process will wait infinitely until the value of lock is 1 (that is implied
by while loop).
• Since, at the very first time critical section is vacant hence the process
will enter the critical section by setting the lock variable as 1.
• When the process exits from the critical section, then in the exit section,
it reassigns the value of lock as 0.
• Every Synchronization mechanism is judged on the basis of
four conditions.
• Mutual Exclusion
• Progress
• Bounded Waiting
• Portability
• Out of the four parameters, Mutual Exclusion and Progress
must be provided by any solution. Let?s analyze this
mechanism on the basis of the above mentioned
conditions.
MUTUAL EXCLUSION
• P1 gets into the entry section. Since the value of lock is 0 hence P1 changes its
value from 0 to 1 and enters into the critical section.
• Now there is no other process in the critical section and the value of lock
variable is 0. P2 also wants to execute its critical section. It enters into the
critical section by setting the lock variable to 1.
• P1 is yet to finish its critical section. P1 has already checked the value of lock
variable and remembers that its value was 0 when it previously checked it.
• Hence, it also enters into the critical section without checking the updated
value of lock variable.
• Now, we got two processes in the critical section. According to the
condition of mutual exclusion, more than one process in the critical
section must not be present at the same time.
• Hence, the lock variable mechanism doesn't guarantee the mutual
exclusion.
• The problem with the lock variable mechanism is that, at the same
time, more than one process can see the vacant tag and more than
one process can enter in the critical section.
• Hence, the lock variable doesn't provide the mutual exclusion
that's why it cannot be used in general.
• Since, this method is failed at the basic step; hence, there is no
need to talk about the other conditions to be fulfilled.
2.TEST SET LOCK
MECHANISM
MODIFICATION IN THE ASSEMBLY CODE
• TSL Lock, R0
• CMP R0, #0
• JNZ step 1
• Non CS
• Int[i] = T ;
• while ( Int[j] == T ) ;
• Critical Section
• Int[i] = F ;
• For Process Pj
• Non CS
• Int [1] = T ;
• while ( Int[i] == T ) ;
• Critical Section
• Int[j]=F ;
• In this mechanism, an extra variable interested is used.
• This is a Boolean variable used to store the interest of the processes
to get enter inside the critical section.
• A process which wants to enter in the critical section first checks in
the entry section whether the other process is interested to get
inside.
• The process will wait for the time until the other process is
interested.
• In exit section, the process makes the value of its interest variable
false so that the other process can get into the critical section.
• The table shows the possible values of interest variable of both the
processes and the process which get the chance in the scenario.
Interest [Pi] Interest [Pj] Process which get the
chance
1. Int [Pi] = True 1. Int [Pj] = True 1. Int [Pi] = 1. While (Int [Pi]
2. while (Int [Pj] 2. while False == True);
== True); (Int[Pi]==True); 2. Int [Pi] = True //waiting for Pj
3. Critical 3. while (Int [Pj]
Section == True);
//waiting for Pj
• Initially, the interest variable of both the processes is false.
• The process Pi shows the interest to get inside the critical
section.
• It sets its Interest Variable to true and check whether the Pj is
also interested or not.
• Since the other process's interest variable is false hence Pi will
get enter into the critical section.
• Meanwhile, the process Pi is preempted and Pj is scheduled. Pj is
a cooperative process and therefore, it also wants to enter in the
critical section.
• It shows its interest by setting the interest variable to true.
• It also checks whether the other process is also interested or not.
• We should notice that Pi is preempted but its interested variable is true
that means it needs to further execute in the critical section.
• Therefore Pj will not get the chance and gets stuck in the while loop.
• Meanwhile, CPU changes Pi's state from blocked to running. Pi is yet to
finish its critical section hence it finishes the critical section and makes
an exit by setting the interest variable to False.
• Now, a case can be possible when Pi again wants to enter in the critical
section and set its interested variable to true and checks whether the
interested variable of Pj is true.
• Here, Pj's interest variable is True hence Pi will get stuck in the while
loop and waits for Pj become uninterested.
• Since, Pj still stuck in the while loop waiting for the
Pi' interested variable to become false.
• Therefore, both the processes are waiting for each
other and none of them is getting into the critical
section.
• This is a condition of deadlock and bounded waiting
can never be provided in the case of deadlock.
• Therefore, we can say that the interested variable
mechanism doesn't guarantee deadlock.
ARCHITECTURAL NEUTRALITY