OS Chapter 3
OS Chapter 3
OS Chapter 3
(iii) Modularity
(iv) Convenience
IPC
• There are two models for IPC
a. Message Passing
b. Shared Memory
Race condition
Race condition is a situation arise due to concurrent execution of
more than one processes which are accessing and manipulating the
same shared data and the result of execution depends upon the
specific order where the access take place.
Proces
sA
Proces
sB
T1 T3 T4
Disabling Interrupts
• Problems:
▪ Unattractive or unwise to give user processes the power to
turn off interrupts.
▪ What if one of them did it (disable interrupt) and never turned
them on (enable interrupt) again? That could be the end of
the system.
▪ If the system is a multiprocessor, with two or more CPUs,
disabling interrupts affects only the CPU that executed the
disable instruction. The other ones will continue running and
can access the shared memory.
Shared lock variable
• A shared variable lock having value 0 or 1.
• Before entering into critical region a process checks a
shared variable lock’s value.
– If the value of lock is 0 then set it to 1 before entering the
critical section and enters into critical section and set it to 0
immediately after leaving the critical section.
– If the value of lock is 1 then wait until it becomes 0 by some
other process which is in critical section.
Shared lock variable
Algorithm:
while (true)
{ < set shared variable to 1>;
< critical section >;
< set shared variable to 0>;
< remainder section>;
}
< remainder
< critical section >
<set lock to 1> <set lock to 0> section>
Proces
sA
Proces
sB
T1 T3 T4
Shared lock variable
Problem:
▪ If process P0 sees the value of lock variable 0 and before it can set it to
1 context switch occurs.
▪ Now process P1 runs and finds value of lock variable 0, so it sets value
to 1, enters critical region.
▪ At some point of time P0 resumes, sets the value of lock variable to 1,
enters critical region.
▪ Now two processes are in their critical regions accessing the same
shared memory, which violates the mutual exclusion condition.
Strict Alteration
• Integer variable 'turn' keeps track of whose turn is to enter the critical section.
• Initially turn=0. Process 0 inspects turn, finds it to be 0, and enters in its critical
section.
• Continuously testing a variable waiting for some event to appear is called the
busy waiting.
• When process 0 exits from critical region it sets turn to 1 and now process 1
can find it to be 1 and enters in to critical region.
• In this way, both the processes get alternate turn to enter in critical region.
Strict Alteration
0 0 0 1 0 0
0 enters in critical 0 leaves critical
Proces region region
s0 1 attempt 1 enters in 1 leaves 1
to enter critical critical attempt
Proces region region to enter
s1
T1 T2 1 Busy T3 T4 T5 1 Busy
Wait Wait
Strict Alteration (Disadvantages)
▪ Consider the following situation for two processes P0 and P1.
▪ P0 leaves its critical region, set turn to 1, enters non critical region.
▪ P1 enters and finishes its critical region, set turn to 0.
▪ Now both P0 and P1 in non-critical region.
▪ P0 finishes non critical region, enters critical region again, and leaves this region,
set turn to 1.
▪ P0 and P1 are now in non-critical region.
0 0 0 1 0 0 1
0 enters in critical 0 leaves critical region
Proces region
s0 1 attempt 1 enters in 1 leaves
critical critical region
to enter region
Proces
s1
Strict Alteration (Disadvantages)
▪ P0 finishes non critical region but cannot enter its critical region because turn =
1 and it is turn of P1 to enter the critical section.
▪ Hence, P0 will be blocked by a process P1 which is not in critical region. This
violates one of the conditions of mutual exclusion.
▪ It wastes CPU time, so we should avoid busy waiting as much as we can.
▪ Another Disadvantage: Taking turns is not a good idea when one of the 0 attempt
processes is much slower than the other. to enter
0 0 0 1 0 0 1
0 enters in critical region 0 leaves critical region
Process
0 1 attempt to 1 enters in 1 leaves
enter critical critical region
region
Process
1
TSL (Test and Set Lock) Instruction
enter_region: (Before entering its critical region, process calls enter_region)
TSL REGISTER,LOCK |copy lock variable to register set lock to 1
CMP REGISTER,#0 | was lock variable 0?
JNE enter_region |if it was nonzero, lock was set, so loop
RET |return to caller: critical region entered
leave_region: (When process wants to leave critical region, it calls leave_region)
MOVE LOCK,#0 |store 0 in lock variable
RET |return to caller
Registe
0
r
Process
A
Process
B T1 T3 T4
Exchange Instruction
• Algorithm
enter_region: (Before entering its critical region, process calls enter_region)
MOVE REGISTER,#1 |put 1 in the register
XCHG REGISTER,LOCK |swap content of register & lock variable
CMP REGISTER,#0 |was lock variable 0?
Process-0 Process-1
Peterson’s Solution
Three Essential Criteria
[1] Mutual exclusion
• P0 and P1 can never be in the critical section at the same time:
Disadvantages:-
(i) It involves Busy waiting.
(ii) It is limited to 2 processes.
Producer Consumer problem
• It is multi-process synchronization
problem.
• It is also known as bounded buffer
Producer Buffer Consumer
problem.
• This problem describes two processes
producer and consumer, who share
common, fixed size buffer.
• Producer process
– Produce some information and put it
into buffer
• Consumer process
– Consume this information (remove it
from the buffer)
What Producer Consumer problem is?
• The problem is to make sure that the producer won’t try to add
data (information) into the buffer if it is full and consumer won’t
try to remove data (information) from the an empty buffer.
• Solution for producer:
– Producer either go to sleep or discard data if the buffer is full.
– Once the consumer removes an item from the buffer, it notifies (wakeups)
the producer to put the data into buffer.
• Solution for consumer:
– Consumer can go to sleep if the buffer is empty.
– Once the producer puts data into buffer, it notifies (wakeups) the consumer
to remove (use) data from buffer.
What Producer Consumer problem is?
• Buffer is empty
– Producer want to produce √
– Consumer want to consume X
Producer Buffer Consumer
• Buffer is full
– Producer want to produce X
– Consumer want to consume √
• Buffer is partial filled
– Producer want to produce √
– Consumer want to consume √
Producer Consumer problem using Sleep & Wakeup
#define N 4
int count=0;
count 0
1
void producer (void)
{ int item; item Item 1
while (true) {
item=produce_item();
if (count==N) sleep(); Producer Buffer Consumer
insert_item(item); 1
count=count+1; 2
if(count==1) wakeup(consumer); 3
} 4
}
Producer Consumer problem using Sleep & Wakeup
void consumer (void)
{ int item;
count 0
1
while (true)
{
item
if (count==0) sleep();
item=remove_item();
count=count-1; Producer Buffer Consumer
if(count==N-1) 1 Item 1
wakeup(producer); 2
consume_item(item); 3
} 4
}
Problem
Problem with this solution is that it contains a race condition that can lead to a deadlock. (How???)
– No buffer overflow: A process executes insert_item only when the buffer is not
full (i.e., the process is blocked if the buffer is full).
{ int item;
while (true) empty 4
3
{
full 0
1
down(&full);
down(&mutex); item
item=remove_item(item);
up(&mutex);
up(&empty); Producer Buffer Consumer
consume_item(item); 1 Item 1
} 2
} 3
Monitor
• If so, the calling process will be suspended until the other process
has left the monitor. If no other process is using the monitor, the
calling process may enter.
Producer consumer problem using monitor
• This action causes the calling process to block. It also allows another
process that had been previously prohibited from entering the
monitor to enter now.
Producer consumer problem using monitor
• This other process the consumer, can wake up its sleeping partner
by doing a signal on the condition variable that its partner is waiting
on.
• in is the number of the latest item that was added to the buffer. The
event counter out is the serial number of the latest item that has
been removed from the buffer.
• The producer needs to ensure that there’s a free slot in the buffer
and will wait (sleep) until the difference between the sequence
number and out (the last item consumed) is less than the buffer size.
• The consumer needs to wait (sleep) until there is at least one item in
the buffer; that is, in is greater than or equal to the next sequence
number that it needs to consume.
Event counters
Readers Writer Problem
Problem Statement
• There is a shared resource which should be accessed by multiple
processes.
• There are two types of processes in this context. They
are reader and writer.
Readers Writer Problem
• A writer cannot write to the resource if there are non zero number
of readers accessing the resource at that time.
Solution
while(TRUE) {
wait(w); // waits on the w semaphore until it gets a chance to
write to the resource
/*perform the
write operation */
signal(w); //it increments w so that the next writer can
access the resource.
}
Reader process
while(TRUE) { void Reader (void)
wait(m); //acquire lock { while (true){
read_count++; down(&mutex); //gain access to reader count
• On the other hand, in the code for the reader, the lock is acquired
whenever the read_count is updated by a process.
• When a reader wants to access the resource, first it increments the
read_count value, then accesses the resource and then decrements
the read_count value.
• The semaphore w is used by the first reader which enters the critical
section and the last reader which exits the critical section.
Cont.
• The reason for this is, when the first readers enters the critical
section, the writer is blocked from the resource. Only new readers
can access the resource now.
• Similarly, when the last reader exits the critical section, it signals the
writer using the w semaphore because there are zero readers now
and a writer can have the chance to access the resource.
Message Passing
PROBLEM STATEMENT
• Consider there are five philosophers sitting
around a circular dining table.
• The dining table has five chopsticks and a bowl of rice in the middle as
shown in the figure.
• At any instant, a philosopher is either eating or thinking.
• When a philosopher wants to eat, he uses two chopsticks - one from their left
and one from their right.
• When a philosopher wants to think, he keeps down both chopsticks at their
original place.
Dining Philosopher’s Problem
The case of 5 philosophers (and 5
The structure of Philosopher i:
chopsticks only)
while (true) {
– Shared data wait (chopstick[i]); // wait to
• Bowl of rice (data set) get the left stick
• Semaphore chopstick [5] wait (chopstick[(i + 1) %
initialized to 1 (free) 5]); // get the right
// eat
Observation: Occasionally try to pick up 2
chopsticks (left and right) to eat from bowl signal (chopstick[i]);
• One chopstick at a time signal (chopstick[(i + 1) %
• Need both chopsticks to eat, then 5]);
release both when done
• Problem: not enough chopsticks for all // think ////////////////////
};
▪ N philosophes and N
chopsticks (not 2N)
Dining Philosopher’s Problem
• When a philosopher wants to eat the rice, he will wait for the chopstick at his
left and picks up that chopstick.
• Then he waits for the right chopstick to be available, and then picks it too. After
eating, he puts both the chopsticks down.
Problem: If all five philosophers are hungry simultaneously, and each of them pickup one
chopstick, then a deadlock situation occurs because they will be waiting for another
chopstick forever.
Dining Philosopher’s Problem
The possible solutions for this are:
• A philosopher must be allowed to pick up the chopsticks only if both the left
and right chopsticks are available.
• Allow only four philosophers starts eating. That way, if all the four
philosophers pick up four chopsticks, there will be one chopstick left on the
table. So, one philosopher can start eating and eventually, two chopsticks will
be available. In this way, deadlocks can be avoided.
Solution
• This solution uses only Boolean semaphors.
• There is one global semaphore to provide mutual exclusion for exectution of
critical protocols.
• There is one semaphore for each chopstick.
• In addition, a local two-phase prioritization scheme is used, under which
philosophers defer to their neighbors who have declared themselves
"hungry."
Solution
Solution
The take_chopsticks
procedure involves
checking the status
of neighboring
philosophers and
then declaring one's
own intention to eat.
This is a two-phase
protocol; first
declaring the status
HUNGRY, then
going on to EAT.
Solution
www.paruluniversity.ac.in