0% found this document useful (0 votes)
8 views5 pages

Critical Section Problem and Producer Consumer Problem

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Critical Section Problem and Producer Consumer Problem

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Critical Section Problem

Race Condition
A race condition, also known as a race hazard, is a system's behaviour that depends on the
sequence of other events, which can lead to unexpected results.

For example, when two threads access a shared variable simultaneously, the first thread
reads the variable, the second thread reads the same value, and then both threads perform
operations on the value. The thread that writes its value last preserves the value, overwriting
the value written by the previous thread. This can lead to unintended behaviour in the
application.

Critical Section

When more than one process tries to access the same code segment that segment is known
as the critical section. The critical section contains shared variables or resources that need
to be synchronised to maintain the consistency of data variables.

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; the
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.

Requirements of Critical Section

Mutual Exclusion
Mutual exclusion refers to the property of ensuring that only one process or thread can
access a shared resource or critical section at any given time.

If process Pi is executing in its critical section, then no other processes can be executing in
their critical sections.

Progress
All threads or processes that are attempting to enter the critical section should eventually be
able to do so, assuming they are not permanently blocked by higher-priority tasks or
deadlocks. This requirement ensures that the system does not get stuck waiting indefinitely
for access to the critical section.

Bounded Waiting
Bounded waiting means that no process or thread should be indefinitely postponed from
acquiring a resource due to continuous priority given to other processes or threads. It

1
ensures a limit on the amount of time a process or thread waits to access a shared resource
or enter a critical section.

Producer Consumer Problem

The producer-consumer problem is an example of a multi-process synchronisation problem.


The problem describes two processes, the producer and the consumer that share a common
fixed-size buffer and use it as a queue.

➔ The producer’s job is to generate data, put it into the buffer, and start again.
➔ At the same time, the consumer is consuming the data (i.e., removing it from the
buffer), one piece at a time.

Problem Statement
Given the common fixed-size buffer, the task is to make sure that the producer can’t add
data into the buffer when it is full and the consumer can’t remove data from an empty buffer.
Accessing memory buffers should not be allowed to producers and consumers at the same
time.

Solution
The producer is to either go to sleep or discard data if the buffer is full. The next time the
consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer
again. In the same manner, the consumer can go to sleep if it finds the buffer to be empty.
The next time the producer transfers data into the buffer, it wakes up the sleeping consumer.

Semaphores:
Semaphores are variables used to indicate the number of resources available in the system
at a particular time. They are used to achieve Process Synchronisation.

Use of Semaphores in Solution to Producer Consumer Problem.


To solve the Producer-Consumer problem three semaphores variables are used.

Full
The full variable is used to track the space filled in the buffer by the Producer process. It is
initialised to 0 initially as initially no space is filled by the Producer process.

Empty
The Empty variable is used to track the empty space in the buffer. The Empty variable is
initially initialised to the BUFFER-SIZE as initially, the whole buffer is empty.

Mutex
Mutex is used to achieve mutual exclusion. mutex ensures that at any particular time only
the producer or the consumer is accessing the buffer.

Mutex - mutex is a binary semaphore variable that has a value of 0 or 1.

Prepared by Anupam Bhattarai 2


Role of Mutex
Mutex is used to solve the producer-consumer problem as mutex helps in mutual exclusion.
It prevents more than one process from entering the critical section. As mutexes have binary
values i.e 0 and 1. So whenever any process tries to enter the critical section code it first
checks for the mutex value by using the wait operation.

Code for Producer

When the 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 from accessing 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


because the task of producer has been completed and consumer can access the buffer.

Code for Consumer

As the consumer is removing an item from the buffer,


therefore the value of “full” is reduced by 1 and the value
of 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 producers


can access the buffer now.

Prepared by Anupam Bhattarai 3


PETERSON’s SOLUTION

Peterson's solution ensures mutual exclusion. It is implemented in user mode and no


hardware support is required therefore it can be implemented on any platform. Now
Peterson’s solution uses two variables: interest and Turn variable.

It is unfortunately not guaranteed to work on modern hardware, due to vagaries of load and
store operations, but it illustrates a number of important concepts.

The key idea behind Peterson's solution is that each process must give priority to the other
process when both processes want to enter the critical section simultaneously. This
prioritisation is managed through the turn variable.

PSEUDO CODE

Prepared by Anupam Bhattarai 4


Explanation

1. Initialization:
● flag[2] is an array of boolean values, initialised to false. Each element
corresponds to a process (flag[0] for Process P0, flag[1] for Process
P1). It indicates whether a process intends to enter the critical section.
● turn is an integer variable initialised to 0. It indicates whose turn it is to enter
the critical section.
2. Process P0:
● P0 sets its flag to true to indicate its intention to enter the critical section.
● It sets turn to 1, indicating it's P1's turn.
● It enters a loop where it waits while P1 wants to enter the critical section
(flag[1] == true) and it's P1's turn (turn == 1). This loop is the heart
of the mutual exclusion logic. If P1 is in its critical section and it's P1's turn,
P0 waits until it's its turn.
● Once it exits the loop, it proceeds to execute its critical section.
● After completing its task in the critical section, P0 resets its flag to false,
indicating it's done with the critical section.
3. Process P1:
● P1 follows a similar procedure as P0.
● It sets its flag to true to indicate its intention to enter the critical section.
● It sets turn to 0, indicating it's P0's turn.
● It enters a loop where it waits while P0 wants to enter the critical section
(flag[0] == true) and it's P0's turn (turn == 0).
● Once it exits the loop, it proceeds to execute its critical section.
● After completing its task in the critical section, P1 resets its flag to false,
indicating it's done with the critical section.

The key idea here is that each process uses a combination of its flag and the turn variable to
determine whether it should wait or proceed into the critical section. By coordinating their
flags and the turn variable, the processes ensure mutual exclusion, meaning that only one
process can be in the critical section at a time.

Prepared by Anupam Bhattarai 5

You might also like