You are on page 1of 7

Inter-process Communication

Critical Section Problem

The critical section is a code segment where the shared variables can be accessed. An atomic
action is required in a critical section i.e. only one process can execute in its critical section at a
time. All the other processes have to wait to execute in their critical sections.

A diagram that demonstrates the critical section is as follows −

In the above diagram, the entry section handles the entry into the critical section. It acquires the
resources needed for execution by the process. The exit section handles the exit from the critical
section. It releases the resources and also informs the other processes that the critical section is
free.

Solution to the Critical Section Problem

The critical section problem needs a solution to synchronize the different processes. The solution
to the critical section problem must satisfy the following conditions −

 Mutual Exclusion
Mutual exclusion implies that only one process can be inside the critical section at any
time. If any other processes require the critical section, they must wait until it is free.
 Progress
Progress means that if a process is not using the critical section, then it should not stop
any other process from accessing it. In other words, any process can enter a critical
section if it is free.
 Bounded Waiting
Bounded waiting means that each process must have a limited waiting time. Itt should not
wait endlessly to access the critical section.

Race Condition in Critical Section Area Problem

A race condition is a potential scenario that might happen within a critical section area. This
occurs when different results from the execution of numerous threads in a crucial region are
obtained depending on the execution order of the threads.

If the critical section area is regarded as an atomic instruction, race conditions in certain areas
can be avoided. Race problems can also be avoided by employing locks or atomic variables to
properly synchronize threads.

Race Condition is Inter Disciplinary approach. It can occur in both the concepts of Multi
Threading, Process Execution, and Critical Section Area too.

Hardware solution

Test-and-Set

 hardware assistance for process synchronization


 a special hardware instruction that does two operations atomically
 i.e., both operations are executed or neither is
o sets the result to current value
o changes current value to true
o when describing machine language (CPU) operations, the verb 'set' means 'set to
true'

Test-and-Set Solution to the Critical Section Problem

repeat
critical section

remainder section

until false

Test-and-Set(target)

result := target
target := TRUE
return result

Information common to both processes:


target = FALSE

Turn Variable or Strict Alternation Approach

Turn Variable or Strict Alternation Approach is the software mechanism implemented at user
mode. It is a busy waiting solution which can be implemented only for two processes. In this
approach, A turn variable is used which is actually a lock.

This approach can only be used for only two processes. In general, let the two processes be Pi
and Pj. They share a variable called turn variable. The pseudo code of the program can be given
as following.

For Process Pi

1. Non - CS
2. while (turn ! = i);
3. Critical Section
4. turn = j;
5. Non - CS

For Process Pj

1. Non - CS
2. while (turn ! = j);
3. Critical Section
4. turn = i ;
5. Non - CS
The actual problem of the lock variable approach was the fact that the process was entering in
the critical section only when the lock variable is 1. More than one process could see the lock
variable as 1 at the same time hence the mutual exclusion was not guaranteed there.

This problem is addressed in the turn variable approach. Now, A process can enter in the critical
section only in the case when the value of the turn variable equal to the PID of the process.

There are only two values possible for turn variable, i or j. if its value is not i then it will
definitely be j or vice versa.

In the entry section, in general, the process Pi will not enter in the critical section until its value
is j or the process Pj will not enter in the critical section until its value is i.

Initially, two processes Pi and Pj are available and want to execute into critical section.

The turn variable is equal to i hence Pi will get the chance to enter into the critical section. The
value of Pi remains I until Pi finishes critical section.

Pi finishes its critical section and assigns j to turn variable. Pj will get the chance to enter into the
critical section. The value of turn remains j until Pj finishes its critical section.

Peterson’s Solution

Peterson's solution is the synchronization algorithm which is basically used in Operating


systems. Peterson's solution is used to provide the operating system with a functionality called
mutual exclusion, where two concurrent processes can share the same resource with any problem
or collision.
In this algorithm, a critical section is created, which is nothing but a block of code where the
shared resources exist. For example, if there are two processes that need to access the same
shared resources at the same time, then this part where the shared resources are located will be
called a critical section, and if one process is already accessing the critical section, then no other
process can access the critical section.
Peterson’s solution is restricted to two processes that alternate execution between their critical
sections and remainder sections

Semaphores

Semaphores refer to the integer variables that are primarily used to solve the critical section
problem via combining two of the atomic procedures, wait and signal, for the process
synchronization.

The definitions of signal and wait are given below:

Wait

It decrements the value of its A argument in case it is positive. In case S is zero or negative, then
no operation would be performed.

wait(A)

while (A<=0);

A–;

}
Signal

This operation increments the actual value of its argument A.

signal(A)

A++;

Monitors

Monitors in operating systems are used to manage access to shared resources, like files or data,
among multiple processes. They ensure that only one process can use a resource simultaneously,
preventing conflicts and data corruption. Monitors simplify synchronization and protect data
integrity, making it easier for programmers to create reliable software.
They serve as "guards" for critical code sections, ensuring that no two processes can enter them
simultaneously. Monitors are like traffic lights that control access to resources, preventing
crashes and ensuring a smooth flow of data and tasks in an operating system.

Syntax and Pseudo-code of Monitor in OS


Below is the pseudo-code of the monitor in OS:
monitor monitor_name{

//declaring shared variables

variables_declaration;
condition_variables;

procedure p1(...) {
...
};

procedure p2(...){
...
};
...
procedure pn(...){
..
};

{
Initailisation Code();
}

In the above pseudo code, monitor_name is the name of the monitor, and p1, p2, pn are the
procedures that provide access to shared data and variables enclosed in monitors. They help
ensure that only one thread is accessed at a time for using the shared resource. This help prevents
race condition and synchronization problems.

You might also like