You are on page 1of 11

 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. It should not wait
endlessly to access the critical section.
 Semaphores in Process Synchronization
Semaphore was proposed by Dijkstra in 1965 which is a very significant technique to manage concurrent
processes by using a simple integer value, which is known as a semaphore. Semaphore is simply a
variable that is non-negative and shared between threads. This variable is used to solve the critical section
problem and to achieve process synchronization in the multiprocessing environment.

Semaphores are of two types-

1. Binary Semaphore –
This is also known as mutex lock. It can have only two values – 0 and 1. Its value is initialized
to 1. It is used to implement the solution of critical section problems with multiple processes.
2. Counting Semaphore –
Its value can range over an unrestricted domain. It is used to control access to a resource that
has multiple instances.

Now let us see how it does so.


First, look at two operations that can be used to access and change the value of the semaphore variable.

Some point regarding P and V operation

1. P operation is also called wait, sleep, or down operation, and V operation is also called signal,
wake-up, or up operation.
2. Both operations are atomic and semaphore(s) is always initialized to one. Here atomic means
that variable on which read, modify and update happens at the same time/moment with no pre-
emption i.e. in-between read, modify and update no other operation is performed that may
change the variable.
3. A critical section is surrounded by both operations to implement process synchronization. See
the below image. The critical section of Process P is in between P and V operation.
Now, let us see how it implements mutual exclusion. Let there be two processes P1 and P2 and a
semaphore s is initialized as 1. Now if suppose P1 enters in its critical section then the value of semaphore
s becomes 0. Now if P2 wants to enter its critical section then it will wait until s > 0, this can only happen
when P1 finishes its critical section and calls V operation on semaphore s. This way mutual exclusion is
achieved. Look at the below image for details which is Binary semaphore.

Advantages of Semaphores
Some of the advantages of semaphores are as follows −

 Semaphores allow only one process into the critical section. They follow the mutual exclusion
principle strictly and are much more efficient than some other methods of synchronization.
 There is no resource wastage because of busy waiting in semaphores as processor time is not
wasted unnecessarily to check if a condition is fulfilled to allow a process to access the critical
section.
 Semaphores are implemented in the machine independent code of the microkernel. So they are
machine independent.

Disadvantages of Semaphores
Some of the disadvantages of semaphores are as follows −

 Semaphores are complicated so the wait and signal operations must be implemented in the correct
order to prevent deadlocks.
 Semaphores are impractical for last scale use as their use leads to loss of modularity. This happens
because the wait and signal operations prevent the creation of a structured layout for the system.
 Semaphores may lead to a priority inversion where low priority processes may access the critical
section first and high priority processes later.
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary semaphores. Details about
these are given as follows −

 Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These semaphores are
used to coordinate the resource access, where the semaphore count is the number of available
resources. If the resources are added, semaphore count automatically incremented and if the
resources are removed, the count is decremented.

 Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to 0 and 1. The
wait operation only works when the semaphore is 1 and the signal operation succeeds when
semaphore is 0. It is sometimes easier to implement binary semaphores than counting semaphores.
Producer Consumer Problem using Semaphores

The producer consumer problem is a synchronization problem. There is a fixed size buffer and the
producer produces items and enters them into the buffer. The consumer removes the items from the buffer
and consumes them.
A producer should not produce items into the buffer when the consumer is consuming an item from the
buffer and vice versa. So the buffer should only be accessed by the producer or consumer at a time.
The producer consumer problem can be resolved using semaphores. The codes for the producer and
consumer process are given as follows −

Producer Process
The code that defines the producer process is given below −

do {

. PRODUCE ITEM

wait(empty);

wait(mutex);

. PUT ITEM IN BUFFER

signal(mutex);

signal(full);

} while(1);

In the above code, mutex, empty and full are semaphores. Here mutex is initialized to 1, empty is
initialized to n (maximum size of the buffer) and full is initialized to 0.
The mutex semaphore ensures mutual exclusion. The empty and full semaphores count the number of
empty and full spaces in the buffer.
After the item is produced, wait operation is carried out on empty. This indicates that the empty space in
the buffer has decreased by 1. Then wait operation is carried out on mutex so that consumer process
cannot interfere.
After the item is put in the buffer, signal operation is carried out on mutex and full. The former indicates
that consumer process can now act and the latter shows that the buffer is full by 1.

Consumer Process
The code that defines the consumer process is given below:

do {

wait(full);

wait(mutex);

..

. REMOVE ITEM FROM BUFFER

signal(mutex);

signal(empty);

. CONSUME ITEM

} while(1);

The wait operation is carried out on full. This indicates that items in the buffer have decreased by 1. Then
wait operation is carried out on mutex so that producer process cannot interfere.
Then the item is removed from buffer. After that, signal operation is carried out on mutex and empty. The
former indicates that consumer process can now act and the latter shows that the empty space in the buffer
has increased by 1.
Producer Consumer Problem using Semaphores
Producer consumer problem is a classical synchronization problem. We can solve this problem by
using semaphores.

A semaphore S is an integer variable that can be accessed only through two standard operations :
wait() and signal().
The wait() operation reduces the value of semaphore by 1 and the signal() operation increases its
value by 1.

wait(S){
while(S<=0); // busy waiting
S--;
}

signal(S){
S++;
}
Semaphores are of two types:
1. Binary Semaphore – This is similar to mutex lock but not the same thing. It can have
only two values – 0 and 1. Its value is initialized to 1. It is used to implement the
solution of critical section problem with multiple processes.
2. Counting Semaphore – Its value can range over an unrestricted domain. It is used to
control access to a resource that has multiple instances.
Problem Statement – We have a buffer of fixed size. A producer can produce an item and can
place in the buffer. A consumer can pick items and can consume them. We need to ensure that
when a producer is placing an item in the buffer, then at the same time consumer should not
consume any item. In this problem, buffer is the critical section.
To solve this problem, we need two counting semaphores – Full and Empty. “Full” keeps track of
number of items in the buffer at any given time and “Empty” keeps track of number of
unoccupied slots.

Initialization of semaphores –
mutex = 1
Full = 0 // Initially, all slots are empty. Thus full slots are 0
Empty = n // All slots are empty initially
Solution for Producer –
do{

//produce an item
wait(empty);
wait(mutex);

//place in buffer

signal(mutex);
signal(full);

}while(true)
When producer produces an item then the value of “empty” is reduced by 1 because one slot will
be filled now. The value of mutex is also reduced to prevent consumer to access the buffer. Now,
the producer has placed the item and thus the value of “full” is increased by 1. The value of mutex
is also increased by 1 beacuse the task of producer has been completed and consumer can access
the buffer.
Solution for Consumer –
do{

wait(full);
wait(mutex);

// remove item from buffer

signal(mutex);
signal(empty);

// consumes item

}while(true)
As the consumer is removing an item from buffer, therefore the value of “full” is reduced by 1 and
the value is mutex is also reduced so that the producer cannot access the buffer at this moment.
Now, the consumer has consumed the item, thus increasing the value of “empty” by 1. The value
of mutex is also increased so that producer can access the buffer now.
Readers-Writers Problem
Consider a situation where we have a file shared between many people.
 If one of the people tries editing the file, no other person should be reading or writing at
the same time, otherwise changes will not be visible to him/her.
 However if some person is reading the file, then others may read it at the same time.
Precisely in OS we call this situation as the readers-writers problem
Problem parameters:
 One set of data is shared among a number of processes
 Once a writer is ready, it performs its write. Only one writer may write at a time
 If a process is writing, no other process can read it
 If at least one reader is reading, no other process can write
 Readers may not write and only read
Solution when Reader has the Priority over Writer
Here priority means, no reader should wait if the share is currently opened for reading.

Three variables are used: mutex, wrt, readcnt to implement solution


1. semaphore mutex, wrt; // semaphore mutex is used to ensure mutual exclusion
when readcnt is updated i.e. when any reader enters or exit from the critical section and
semaphore wrt is used by both readers and writers
2. int readcnt; // readcnt tells the number of processes performing read in the critical
section, initially 0
Functions for sempahore :
– wait() : decrements the semaphore value.
– signal() : increments the semaphore value.
Writer process:
1. Writer requests the entry to critical section.
2. If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed,
it keeps on waiting.
3. It exits the critical section.
do {
// writer requests for critical section
wait(wrt);

// performs the write

// leaves the critical section


signal(wrt);
} while(true);
Reader process:
1. Reader requests the entry to critical section.
2. If allowed:
 it increments the count of number of readers inside the critical section. If this
reader is the first reader entering, it locks the wrt semaphore to restrict the
entry of writers if any reader is inside.
 It then, signals mutex as any other reader is allowed to enter while others are
already reading.
 After performing reading, it exits the critical section. When exiting, it checks
if no more reader is inside, it signals the semaphore “wrt” as now, writer can
enter the critical section.
3. If not allowed, it keeps on waiting.
do {

// Reader wants to enter the critical section


wait(mutex);

// The number of readers has now increased by 1


readcnt++;

// there is atleast one reader in the critical section


// this ensure no writer can enter if there is even one reader
// thus we give preference to readers here
if (readcnt==1)
wait(wrt);

// other readers can enter while this current reader is inside


// the critical section
signal(mutex);

// current reader performs reading here


wait(mutex); // a reader wants to leave

readcnt--;

// that is, no reader is left in the critical section,


if (readcnt == 0)
signal(wrt); // writers can enter

signal(mutex); // reader leaves

} while(true);
Thus, the semaphore „wrt„ is queued on both readers and writers in a manner such that preference
is given to readers if writers are also there. Thus, no reader is waiting simply because a writer has
requested to enter the critical section.

You might also like