0% found this document useful (0 votes)
6 views41 pages

Module 3.1

The document discusses the Producer-Consumer problem, detailing the roles of the producer and consumer processes, their respective codes, and the issues of race conditions and critical sections. It outlines various solutions to the critical section problem, including Peterson's solution, hardware solutions, semaphore solutions, and monitor solutions, while emphasizing the importance of mutual exclusion, progress, and bounded waiting. Additionally, it briefly mentions classic synchronization problems such as the Readers-Writers problem and the Dining Philosophers problem.

Uploaded by

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

Module 3.1

The document discusses the Producer-Consumer problem, detailing the roles of the producer and consumer processes, their respective codes, and the issues of race conditions and critical sections. It outlines various solutions to the critical section problem, including Peterson's solution, hardware solutions, semaphore solutions, and monitor solutions, while emphasizing the importance of mutual exclusion, progress, and bounded waiting. Additionally, it briefly mentions classic synchronization problems such as the Readers-Writers problem and the Dining Philosophers problem.

Uploaded by

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

PROCESS SYNCHRONIZATION

Producer – Consumer Problem


 There is a buffer which contains some number of slots.
 One item can be stored into each slot of the buffer.
 In and out are pointers to the buffer.
 Count is a variable which indicates the number of items currently existing in the buffer.
 Producer is the process which enters items into the buffer.
 In points to the next free slot of buffer into which the producer inserts the next item.
 In pointer is moved to the next position after inserting an item into the buffer.
 Consumer is the process which takes items from the buffer.
 Out points to the slot from which the consumer removes the item.
 Out pointer is moved to the next position after deleting an item from the buffer.

2
Producer Process
The code of producer process is:
while (true)
{
P1: while (count == sizeofbuffer)
P2: ;
P3: buffer[in] = item;
P4: in = (in + 1) % sizeofbuffer;
P5: a = count;
P6: a = a + 1;
P7: count = a;
}

3
Consumer Process
The code of consumer process is:
while(true)
{
C1: while (count == 0)
C2: ;
C3: item = buffer[out];
C4: out = (out+1) % sizeofbuffer;
C5: b = count;
C6: b = b - 1;
C7: count = b;
}

4
Serial Execution
With serial execution, processes are executed one after another.
CPU is switched to another process only after completion of the currently running process.
Concurrent Execution (or) Parallel Execution
With concurrent execution, CPU can be transferred to another process during execution of the current
process.

When the processes are not accessing common variables:

We get correct output when the processes are executed either serially or concurrently.

When the processes are accessing and modifying common variables:

We get correct output in serial execution of the processes.

We may get wrong output in some sequences of concurrent execution of the processes.

One example concurrent execution of instructions of producer & consumer processes that leads to wrong
result is P1, P2, P3, P4, P5, P6, C1, C2, C3, C4, C5, C6, P7, C7. 5
while (true) while(true)
{ {
P1: while (count == sizeofbuffer) C1: while (count == 0)
P2: ; C2: ;
P3: buffer[in] = item; C3: item = buffer[out];
P4: in = (in + 1) % sizeofbuffer; C4: out = (out+1) % sizeofbuffer;
P5: a = count; C5: b = count;
P6: a = a + 1; C6: b = b - 1;
P7: count = a; C7: count = b;
} }

6
If the position of buffer is

When the statements of producer and consumer are executed in the order P1, P2, P3, P4, P5, P6, C1, C2, C3,
C4, C5, C6, P7, C7 then the position of buffer is

We are getting wrong output i.e. count=1. 7


If the position of buffer is

When the statements of producer and consumer are executed in the order P1, P2, P3, P4, P5, P6, C1, C2,
C3, C4, C5, C6, C7, P7 then the position of buffer is

Here also, we are getting wrong output i.e. count=3.


8
Race condition
If we get different outputs for different orders of concurrent execution of statements of processes then
that situation is called as “Race condition”.
The reason for getting wrong results in concurrent execution of processes is accessing and updating some
common variables (shared data) in all processes.

Ex: What would be the different values that A, B can possibly take after the execution of the processes P1,
P2 concurrently?
Initial values of A and B are 6 and 4 respectively.

Process P1: Process P2:

I1: A=A-B; I11: A=A+1;


I2: B=B+A; I12: B=B-1;

9
Concurrent execution sequence1: I1, I2, I11, I12
A=6-4=2
B=4+2=6
A=2+1=3
B=6-1=5
A=3, B=5

Concurrent execution sequence2: I1, I11, I12, I2


A=6-4=2
A=2+1=3
B=4-1=3
B=3+3=6
A=3, B=6
Concurrent execution sequence3: I1, I11, I2, I12
A=6-4=2
A=2+1=3
B=4+3=7
B=7-1=6
A=3, B=6

Concurrent execution sequence4: I11, I12, I1, I2


A=6+1=7
B=4-1=3
A=7-3=4
B=3+4=7
A=4, B=7
Concurrent execution sequence5: I11, I1, I2, I12
A=6+1=7
A=7-4=3
B=4+3=7
B=7-1=6
A=3, B=6

Concurrent execution sequence6: I11, I1, I12, I2


A=6+1=7
A=7-4=3
B=4-1=3
B=3+3=6
A=3, B=6
The different possible values that A and B can take are
A=3, B=5
A=3, B=6
A=4, B=7
Critical Section
Critical section is part of the process where the process is accessing and updating the common variables
(shared data).

In producer process, the critical section is

a = count
a=a+1
count = a

In consumer process, the critical section is


b = count
b=b-1
count = b
Critical Section Problem
Allowing only one process to execute in its critical section at a time.
(or)
If a process is executing in its critical section then at the same time no other process is allowed to
execute in its critical section.

This restriction on the execution of processes leads to generation of correct output in the concurrent
execution of processes.

Solutions for Critical Section Problem (or) Solutions for Implementing Mutual Exclusion

Following are different solutions to the critical section problem


1. Peterson’s Solution or Software solution
2. Hardware Solution
3. Semaphore Solution
4. Monitor Solution

15
Any solution to the critical section problem must satisfy the following three conditions
1) Mutual exclusion
2) Progress
3) Bounded waiting

Mutual Exclusion

At a time, only one process should enter into its critical section.

Progress

All processes should not enter into deadlock state. At any time, at least one process should be in the
running state.

Bounded waiting

All processes should be given equal chance to enter into their critical sections.
16
Peterson’s Solution

It is applicable to two processes only.

Two variables are used


o int turn;

o boolean flag[2];

turn variable indicates whose turn it is to enter into critical section.

turn=1 - indicates that the producer process can enter into its critical section.

turn=2 - indicates that the consumer process can enter into its critical section.

flag indicates whether a process is ready to enter into its critical section or not.

flag[1]=true - indicates that the producer process is ready to enter into its critical section.

flag[2]=true - indicates that the consumer process is ready to enter into its critical section.

17
Initially, flag[1] and flag[2] are set to false.

The following procedure is used to solve the critical section problem.

When the producer process wants to enter into its critical section then the producer process sets its
corresponding flag value to true.

If the consumer process is also ready to enter into its critical section then the producer process allows the
consumer process to enter into its critical section by setting the turn value to the consumer process.

Producer process waits until consumer process comes out from its critical section.

Then the producer process enters into its critical section.

Producer process sets its flag value to false after coming out from its critical section.

Same procedure is followed by the consumer process when it wants to enter into its critical section.

Peterson’s solution to the critical section problem of producer-consumer is

18
19
Hardware Solution

 A variable named ‘lock’ is used.

 When a process wants to enter into its critical section then it must acquire the ‘lock’. i.e. set ‘lock’ value
to true.

 After coming out from the critical section, the process has to release the ‘lock’. i.e. set ‘lock’ value to
false.

 The structure of process is

do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
20
TestandSet()

 ‘TestandSet’ is a hardware instruction which performs operations on the ‘lock’ variable to solve the
critical section problem.

 This function takes ‘lock’ variable as input and returns a boolean value.

 This function stores the current value of lock variable.


The definition of TestandSet() function is

boolean TestandSet(boolean lock)


{
boolean temp=lock;
lock=true;
return temp;
}

TestandSet() is an atomic instruction.

Initial value of ‘lock’ is false.

The solution to critical section problem of producer-consumer is

22
23
Swap()

‘Swap’ is another hardware instruction used to solve the critical section problem. The definition of Swap()
instruction is

void swap(boolean lock, boolean key)


{
boolean temp=lock;
lock=key;
key=temp;
}

Lock is global variable.

key is local variable.

Initial value of lock is false.

The solution to critical section problem of producer-consumer is 24


25
Semaphore Solution

Semaphore is an integer variable. There are two types of semaphore

1) Binary semaphore
2) Counting semaphore

Binary semaphore is used to solve the critical section problem.

Binary semaphore is also called as mutex variable because binary semaphore is used to implement mutual
exclusion.

Initial value of binary semaphore is 1.

Counting semaphore is used to provide synchronous access to a resource by a number of processes.

Initial value of counting semaphore is equal to the number of instances of the resource.
A queue is associated with each semaphore variable.

Two operations are defined on a semaphore variable

1) wait()
2) signal()

The definition of wait() operation is

wait(s)
{
s=s-1;
if(s<0)
{
block the process;
Insert the process in the queue associated with the semaphore
variable s;
}
}
The definition of signal() operation is

signal(s)
{
s=s+1;
if(s<=0)
{
remove a process form the queue associated with the semaphore
variable s;
restart the removed process;
}
}

To solve the critical section problem, a process has to call wait() operation on the semaphore variable
before entering into its critical section and signal() operation after coming out from its critical section.

wait(S)
Critical Section
signal(S)
Solution to the critical section problem of producer-consumer is
Monitor

Monitor is similar to a class in ‘Java’ language. The structure of monitor is


Monitor Nameofmonitor
{
Declaration of shared variables;
Declaration of condition variables;

Procedure1(list of parameters)
{
------
}
Procedure2(list of parameters)
{
-----
}
-----
------
Initializationcode( )
{
----
}
}
In a monitor, zero or more shared variables are declared using syntax rules of java language.

Similarly, zero or more condition variables are declared with the following syntax
condition listofvariables;

Zero or more procedures are defined. Procedures do not return any value.

Shared variables are used to store values and can be accessed only by the procedures of monitor.

Procedures of the monitor can access and perform operations on the shared variables of the monitor.

Initialization code is like a constructor in a java class and is used for initializing the shared variables of the
monitor.
Condition variables are used to solve the critical section problem.

A queue is associated with each condition variable.

Two operations are defined on any condition variable


1. wait( )
2. signal( )

When any process calls wait() operation on any condition variable then the execution of that process will be
stopped and will be placed into the queue associated with that condition variable.

When any process calls signal() operation on any conditional variable then a waiting process from the queue
of the condition variable will be restarted.
At any time, only one process is allowed to call a procedure of the monitor.

When a process say P1 invokes a procedure of the monitor and the procedure is executing then at the same
time if any other process say P2 tries to call a procedure of the monitor then the process P2 has to wait.

With the above property of monitor, mutual exclusion is implemented automatically in the monitor.
Monitor solution for critical section problem of Producer – Consumer

Monitor PC
{
int sizeofbuffer, Buffer[], in, out, count, item;
condition full, empty;

insert(int x)
{
if(count==sizeofbuffer)
wait(full);
Buffer[in]=x;
in=(in+1)%sizeofbuffer;
count=count+1;
signal(empty);
}
delete()
{
if(count==0)
wait(empty);
item=Buffer[out];
out=(out+1)%sizeofbuffer;
count=count-1;
signal(full);
}
Initialization()
{
sizeofbuffer=5;
in=1;
out=0;
count=0;
}
}
Producer Process Consumer Process

while(true) while(true)
{ {
printf(“enter item\n”); PC.delete();
scanf(“%d”,&item); }
PC.insert(item);
}
Classic problems of synchronization

1. Bounded buffer or producer-consumer problem


2. Readers-writers problem
3. Dining philosopher’s problem
Readers-Writers Problem

There is a database that can be shared by number of processes.

Some processes named ‘readers’ only read data from the database.

Other processes named ‘writers’ write data to the database.

At a time, any number of readers can read data from the database without any conflict.

But, when a writer is writing data then other writers and readers are not allowed to access the database.
Dining Philosopher’s Problem
There are 5 philosophers.

A philosopher at any particular time is in either thinking or eating state.

There is a table surrounded by 5 chairs which are used by the 5 philosophers.

There is a rice bowl in the center of table.

There are 5 plates and 5 spoons (chopsticks) and are placed on the table as shown in diagram.

A philosopher can eat food only when the two (left and right) chopsticks (spoons) are available for him.

A chopstick can be used by only one philosopher at a time.

In this problem, we need to implement mutual exclusion for the 5 chopsticks.

You might also like