0% found this document useful (0 votes)
7 views

Chapter 2 Part IV Process Synchronization

The document discusses process synchronization in operating systems, focusing on cooperating processes and the critical-section problem. It explains the challenges of concurrent execution, introduces the producer-consumer problem, and outlines solutions such as Peterson's solution and synchronization mechanisms like semaphores and monitors. Key concepts include mutual exclusion, race conditions, and the importance of ensuring orderly access to shared resources.

Uploaded by

fikadu.meu.edu
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)
7 views

Chapter 2 Part IV Process Synchronization

The document discusses process synchronization in operating systems, focusing on cooperating processes and the critical-section problem. It explains the challenges of concurrent execution, introduces the producer-consumer problem, and outlines solutions such as Peterson's solution and synchronization mechanisms like semaphores and monitors. Key concepts include mutual exclusion, race conditions, and the importance of ensuring orderly access to shared resources.

Uploaded by

fikadu.meu.edu
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/ 40

2.

4)
( s ec
r 2
e
hapt
C

es s ion
ro c z at
p roni
n c h
s y
Sem II, 2011...IT3106... Operating Systems.. 1
.G5-G8
2.4 Process Synchronization
o Cooperating Processes(revision)
o Problem of concurrent execution
o The Critical-Section Problem
o Peterson’s Solution
o Synchronization Hardware
o Semaphores
o Monitors
o Synchronization Examples

Sem II, 2011...IT3106... Operating Systems.. 2


.G5-G8
Cooperating Processes
• Concurrent Processes can be
– Independent processes
» cannot affect or be affected by the execution of
another process.
– Cooperating processes
» can affect or be affected by the execution of
another process.
• Advantages of process cooperation:
» Information sharing
» Computation speedup
» Modularity
» Convenience(e.g. editing, printing, compiling)
• Concurrent execution requires
» process communication and process synchronization
Sem II, 2011...IT3106... Operating Systems.. 3
.G5-G8
Problem with concurrent execution
• Concurrent processes may have access to shared data and
resources.
• If there is no controlled access to shared data, some processes will
obtain an inconsistent view of the shared data.
 Consider two processes P1 and P2, accessing shared data. while P1 is
updating data, it is preempted (because of timeout, for example) so
that P2 can run. Then P2 try to read the data, which are partly
modified.
 Results in data inconsistency
• In such cases, the outcome of the action performed by concurrent
processes will then depend on the order in which their execution is
interleaved.
• Maintaining data consistency requires mechanisms to ensure the
orderly execution of cooperating processes
Sem II, 2011...IT3106... Operating
4
Systems...G5-G8
Producer-Consumer Problem
• Paradigm for cooperating processes;
– producer process produces information that is
consumed by a consumer process.
• We need buffer of items that can be filled by producer
and emptied by consumer. Buffer can be
– Unbounded-buffer places no practical limit on the size of
the buffer. Consumer may wait, producer never waits.
– Bounded-buffer assumes that there is a fixed buffer size.
Consumer waits for new item, producer waits if buffer is
full.
– Producer and Consumer must synchronize.
Sem II, 2011...IT3106... Operating Systems.. 5
.G5-G8
Producer-Consumer Problem

Sem II, 2011...IT3106... Operating Systems.. 6


.G5-G8
Producer-Consumer Problem(contd)

 Suppose that we wanted to provide a solution to the


consumer-producer problem that fills all the buffer.
We can do so by having an integer counter that
keeps track of the number of items in the buffer.
 Initially, the counter is set to 0. It is incremented by
the producer after it produces a new item and is
decremented by the consumer after it consumes a
item.
Sem II, 2011...IT3106... Operating Systems.. 7
.G5-G8
//Producer //Consumer
#define N 100 void consumer(void)

Consumer process - Empties filled buffers


int counter = 0; {
void producer(void) int item;
{ while (TRUE)
int item; {
while (TRUE) if (counter == 0)
{ sleep();
Producer process - creates filled buffers

item = produce_item(); item=remove_item(item);


if (count == N) counter = counter -1;
sleep(); if (count ==0)
insert_item(item); wakeup(producer);
counter = counter + 1; consume_item(item);
if (count == 1) }
wakeup(consumer); }
}
}
 The statements counter+1; and counter-1; must be performed
atomically.
 Atomic/Indivisible operation means an operation that
Sem II, 2011...IT3106... Operating
completes its entirety without interruption.
Systems...G5-G8
8
The Producer Consumer Problem
Note that counter++;  this line is NOT what it seems!!

is really --> register = counter


register = register + 1
counter = register

At a micro level, the following scenario could occur using this code:
TO; Producer Execute register1 = counter register1 = 5
T1; Producer Execute register1 = register1 + 1 register1 = 6
T2; Consumer Execute register2 = counter register2 = 5
T3; Consumer Execute register2 = register2 – 1 register2 = 4
T4; Producer Execute counter = register1 counter = 6
T5; Consumer Execute counter = register2 counter = 4

Is output(the value of counter) is equal to 6?5?4?


The outcome of concurrent thread execution depends on the particular
order in which the access takes place = race condition.
Race condition

 Race condition: The situation where several processes


access and manipulate shared data concurrently.
 The final value of the shared data depends upon which
process finishes last.
 The key to preventing trouble here and in many other
situations involving shared memory, shared files, and
shared everything else is to find some way to prohibit more
than one process from reading and writing the shared data
at the same time .
 To prevent race conditions, concurrent processes must be
coordinated or be synchronized.
Sem II, 2011...IT3106... Operating Systems.. 10
.G5-G8
Race condition updating a variable
on variable x after p1 and p2 are
Which process effect is observed

executed?

Sem II, 2011...IT3106... Operating Systems.. 11


.G5-G8
Critical section to prevent a race condition

 Multiprogramming allows logical parallelism, uses devices efficiently


but we lose correctness when there is a race condition.
 So we forbid logical parallelism inside critical section so we lose some
parallelism but we regain correctness.

Sem II, 2011...IT3106... Operating Systems...G5-G8 12


The Critical-Section
• A Critical Section is : A section of code, common to n
cooperating processes, in which
the processes may be accessing
common/shared/ variables.
• When a process executes code that manipulates shared data (or
resource), we say that the process is in it’s Critical Section (for
that shared data).
• Problem – ensure that when one process is executing in its CS, no
other process is allowed to execute in its CS.

• The execution of critical sections must be mutually exclusive: at


any time, only one process is allowed to execute in its critical
section (even with multiple processors).
Sem II, 2011...IT3106... Operating Systems...G5-G8
13
The Critical-Section(Contd)
 Each process must first request permission to enter its
critical section.
 The section of code implementing this request is
called the Entry Section (ES).

 A Critical Section Environment contains:


 Entry Section:-Code requesting entry into the
critical section.
 Critical Section:-Code in which only one process
can execute at any one time.
 Exit Section:-The end of the critical section,
releasing or allowing others in.
 Remainder Section:-Rest of the code AFTER the
critical section.
Sem II, 2011...IT3106... Operating
14
Systems...G5-G8
Two Processes
Software

oHere’s an example of a simple piece of code containing the


components required in a critical section.

Entry Section
do {
while ( turn != i );
/* critical section */ Critical Section

turn = j; Exit Section


/* remainder section
*/
} while(TRUE); Remainder Section
Solution to Critical-Section Problem
 A solution to a critical –section problem must
ENFORCE all of the following three rules

Mutual Exclusion: No more than one process can execute in its


critical section at one time.

Progress: If no one is in the critical section and someone


wants in, then those processes not in their
remainder section must be able to decide in a finite
time who should go in.
Bounded Wait: A bound must exist on the number of times that
other processes are allowed to enter their critical
sections after a process has made a request to
enter its critical section and before that request is
granted
Sem II, 2011...IT3106... Operating Systems...G5-G8
16
Solution to CS Problem – Mutual Exclusion
Advantages
 Applicable to any number of processes on either a single
processor or multiple processors sharing main memory
 It is simple and therefore easy to verify
 It can be used to support multiple critical sections

Disadvantages
 Busy-waiting consumes processor time
 Starvation is possible when a process leaves a critical section
and more than one process is waiting.
 Deadlock(explain why?)

Sem II, 2011...IT3106... Operating Systems...G5-G8


17
Synchronization Hardware
 Many systems provide hardware support for critical
section code.
 Uniprocessors – could disable interrupts:
• Currently running code would execute without preemption(if
all interrupts are disabled).
• Generally too inefficient on multiprocessor systems(why?).
 Modern machines provide special atomic (non-
interruptible) hardware instructions:
• Either test memory word and set value at once.
• Or swap contents of two memory words.
Sem II, 2011...IT3106... Operating Systems.. 18
.G5-G8
Mutual Exclusion with busy waiting

1. Disabling interrupts:- here each process will disable all interrupts just
after entering its critical region and re-enable them just before leaving
it. Disabling Interrupts: Works for the Uni-Processor case
only. WHY?
Are the three Critical Section

 With interrupts disabled, no clock interrupts can occur.


Requirements Met?

 The CPU is only switched from process to process as a result of


clock or other interrupts, after all, and with interrupts turned
off the CPU will not be switched to another process.

Process Pi:
 Thus, once a process has
repeat
disabled interrupts, it can disable interrupts
examine and update the shared critical section
enable interrupts
memory without fear that any remainder section
other process will intervene. forever
Sem II, 2011...IT3106... Operating Systems...G5-G8 19
Mutual Exclusion with busy waiting(CONTD)

 Drawbacks of Disabling Interrupts:


1. If the user process did not turned on the interrupts, this could
be the end of the system.
2. 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. That is, critical section is now atomic but not mutually
exclusive (interrupts are not disabled on other processors).
 In general, disabling interrupts is often a useful technique
within the operating system itself but is not appropriate as a
general mutual exclusion mechanism for user processes

Sem II, 2011...IT3106... Operating Systems...G5-G8 20


Mutual Exclusion with busy waiting(CONTD)

2. Lock Variables:- is a software solution which uses a single,


shared (lock)variable, initially 0.
 When a process wants to enter its critical region, it first
tests the lock.
 If the lock is 0, the process sets it to 1 and enters the
critical region.
 If the lock is already 1, the process just waits until it
becomes 0. Thus, a 0 means that no process is in its
critical region, and a 1 means that some process is in its
critical region.
Problem

 Unfortunately, this idea may lead two processes to


have concurrent access to the critical region. How?
Sem II, 2011...IT3106... Operating Systems...G5-G8 21
Solution to Critical Section Problem using Locks
 Suppose that one process reads
the lock and sees that it is 0.
do {  Before it can set the lock to 1,
acquire lock another process is scheduled,
critical runs, and sets the lock to 1.
 When the first process runs
section
again, it will also set the lock to
release lock 1, and two processes will be in
remainder section their critical regions at the same
time.
} Are the three Critical Section
while (TRUE); Requirements Met?

Sem II, 2011...IT3106... Operating Systems.. 22


.G5-G8
Mutual Exclusion with busy waiting(CONTD)
3. Strict Alternation:-the integer variable turn, initially 0, keeps
track of whose turn it is to enter the critical region and examine
or update the shared memory.
 Initially, process 0 inspects turn, finds it to be 0, and enters
its critical region.
 Process 1 also finds it to be 0 and therefore sits in a tight
loop continually testing turn to see when it becomes 1.
 Continuously testing a variable until some value appears is
called busy waiting.
 It should usually be avoided, since it wastes CPU time. Only
when there is a reasonable expectation that the wait will be
short is busy waiting used.
 A lock that uses busy waiting is called a spin lock.
Sem II, 2011...IT3106... Operating Systems.. 23
.G5-G8
Mutual Exclusion with busy waiting(CONTD)
• When process 0 leaves the critical region, it sets turn to 1, to allow
process 1 to enter its critical region.
• Suppose that process 1 finishes its critical region quickly, so both
processes are in their noncritical regions, with turn set to 0. Now process
0 executes its whole loop quickly, exiting its critical region and setting
turn to 1. At this point turn is 1 and both processes are executing in their
noncritical regions.
• Suddenly, process 0 finishes its noncritical region and goes back to the
top of its loop. Unfortunately, it is not permitted to enter its critical region
now, because turn is 1 and process 1 is busy with its noncritical region. It
hangs in its while loop until process 1 sets turn to 0.

n
ents l Sectio
Process 0 Process 1

?
Met
while (TRUE){ while (TRUE) {

ca
Requ ree Criti
while(turn != 0) ; while(turn != 1);
critical_region(); critical_region();

irem
th
turn = 1; turn = 0;

the
noncritical_region(); noncritical_region();

Are
} } Systems..
Sem II, 2011...IT3106... Operating
.G5-G8
24
Peterson’s Solution

o Two process solution


o Each processes sets a flag to request entry. Then each process toggles a
bit to allow the other in first.
o This code is executed for each process I
· Assumes atomic load, store, test instruction. For instance, if a
store and test occur simultaneously, the test gets EITHER the old
or the new, but not some combination.
• The two processes share two variables:
 int turn
 boolean flag[2];
initially flag [0] = flag [1] = false.
 flag [i] = true  Pi ready to enter its critical section

• The variable turn indicates whose turn it is to enter the critical


section.
• The flag array is used to indicate if a process is ready to enter the
critical section. flag[i] = true implies
Sem II, 2011...IT3106... that process Pi is ready!
Operating Systems...G5-G8 25
Peterson’s Solution(contd)

Algorithm for Process Pi


do {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);

CRITICAL SECTION

t? o n
Me ecti
nts cal S
flag[i] = FALSE;

ire e Criti
me
Re thre
REMAINDER SECTION

qu
the
Are
}while(1);

Sem II, 2011...IT3106... Operating Systems.. 26


.G5-G8
Semaphores
o Semaphore is a Synchronization tool that does not require
busy waiting at a user level
o Two standard operations modify S: wait() and signal()
o If a process is waiting for a signal, it is suspended until that
signal is sent
o Wait and signal operations cannot be interrupted.
o Queue is used to hold processes waiting on the semaphore
o A Special variable, S called a semaphore is used for wait (S) {
signaling. The variable has an integer value. while S <= 0;
– May be initialized to a nonnegative number // no-op
– Wait operation decrements the semaphore value S--;
– Signal operation increments semaphore value }
signal (S) {
o Less complicated S++;
Sem II, 2011...IT3106... Operating Systems...G5-G8 }
27
Semaphores(contd)
oSemaphores can be used to force synchronization ( precedence
) if the preceeder does a signal at the end, and the follower
does wait at beginning. For example, here we want P1 to
execute before P2.

P1: P2:
statement 1; wait ( synch );
signal ( synch ); statement 2;

o We don't want to loop on busy, so will suspend instead:

Sem II, 2011...IT3106... Operating Systems.. 28


.G5-G8
Semaphore Implementation
• Must guarantee that no two processes can execute wait () and
signal () on the same semaphore at the same time
• Thus, implementation becomes the critical section problem
where the wait and signal code are placed in the critical
section.
– Could now have busy waiting in critical section
implementation
• But implementation code is short
• Little busy waiting if critical section rarely occupied
• Note that applications may spend lots of time in critical
sections and therefore this is not a good solution.

Sem II, 2011...IT3106... Operating Systems...G5-G8 29


Semaphore Implementation with no Busy waiting
o With each semaphore there is an  Implementation of wait:
associated waiting queue. Each entry wait (S){
in a waiting queue has two data value--;
items: if (value < 0) {
add this process to waiting
– value (of type integer) queue
– pointer to next record in the list block(); }
o Two operations: }
– block – place the process  Implementation of signal:
invoking the operation on the Signal (S){
appropriate waiting queue. value++;
– wakeup – remove one of if (value <= 0) {
processes in the waiting queue remove a process P from the
waiting queue
and place it in the ready queue.
wakeup(P); }
}
Sem II, 2011...IT3106... Operating Systems.. 30
.G5-G8
Deadlock and Starvation
• Deadlock – two or more processes are waiting indefinitely for an
event that can be caused by only one of the waiting processes
• Let S and Q be two semaphores initialized to 1
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. .
. .
. .
signal (S); signal (Q);
signal (Q); signal (S);
• Starvation – indefinite blocking. A process may never be removed
from the semaphore queue in which it is suspended.
• To better understand what deadlock is refer the next slide.

Sem II, 2011...IT3106... Operating Systems...G5-G8


31
Problems with Semaphores
• Semaphores provide a powerful tool for enforcing mutual exclusion
and coordinate processes.
• But wait(S) and signal(S) are scattered among several processes.
Hence, difficult to understand their effects.
• Usage must be correct in all the processes (correct order, correct
variables, no omissions).
• One bad (or malicious) process can fail the entire collection of
processes.
• Drawbacks of software solutions
 Software solutions are very delicate .
 Processes that are requesting to enter their critical section are busy
waiting (consuming processor time needlessly).
 If critical sections are long, it would be more efficient to block
processes that are waiting.
Sem II, 2011...IT3106... Operating Systems.. 32
.G5-G8
Monitors
o High-level synchronization construct that allows the safe sharing
of an abstract data type among concurrent processes.
o Only one process may be active within the monitor at a time
monitor monitor-name
{ // shared variable declarations
procedure P1 (…) { …. }

procedure Pn (…) {……}

Initialization code ( ….) { … }



}
}
Sem II, 2011...IT3106... Operating Systems.. 33
.G5-G8 Schematic view of a Monitor
Monitors(contd)
o To allow a process to wait within the monitor, a condition variable
must be declared, as
condition x, y;
o Condition variable can only be used with the operations wait and
signal.
– The operation
x.wait();
means that the process invoking this operation is suspended
until another process invokes
x.signal();
– The x.signal operation resumes exactly one suspended process.
If no process is suspended, then the signal operation has no
effect.

Sem II, 2011...IT3106... Operating Systems.. 34


.G5-G8
Monitor with Condition Variables

Sem II, 2011...IT3106... Operating Systems.. 35


.G5-G8
Some Interesting
Problems
Classical Problems of Synchronization

• Bounded-Buffer Problem
• Readers and Writers Problem Reading
Assignment
• Dining-Philosophers Problem

Read About
1. What problem is related to each of the above issues?
2. How the problem can be alleviated?

Sem II, 2011...IT3106... Operating Systems.. 36


.G5-G8
THE READERS/WRITERS PROBLEM: Some Interesting
Problems
This is the same as the Producer / Consumer problem except - we
now can have many concurrent readers and one exclusive
writer.
Locks: are shared (for the readers) and exclusive (for the
writer).
Two possible ( contradictory ) guidelines can be used:
o No reader is kept waiting unless a writer holds the lock (the
readers have precedence).
o If a writer is waiting for access, no new reader gains access
(writer has precedence).
o ( NOTE: starvation can occur on either of these rules if they are
followed rigorously.)

Sem II, 2011...IT3106... Operating


37
Systems...G5-G8
The Readers-Writers Problem(contd)

• Multiple readers or a single writer can use DB.

X X X
writer reader writer reader
reader

writer reader
reader
reader
writer
reader reader

Sem II, 2011...IT3106... Operating


38
Systems...G5-G8
THE DINING PHILOSOPHERS Some Interesting
PROBLEM Problems

5 philosophers with 5 chopsticks sit around a circular table. They


each want to eat at random times and must pick up the chopsticks
on their right and on their left.
Clearly deadlock is rampant ( and starvation possible.)

Several solutions are possible:

• Allow only 4 philosophers to be hungry at


a time.

• Allow pickup only if both chopsticks are


available. ( Done in critical section )

• Odd # philosopher always picks up left


chopstick 1st, even # philosopher always
picks up right chopstick 1st.
Sem II, 2011...IT3106... Operating
39
Systems...G5-G8
Some Interesting
Problems

Synchronization Examples
(Read section 6.8 on page 217-230)

ng !
eadi
s

are r
c e s

you
ro i o n

hile
f P a t

bts w
d o n i z
En chro

r dou
sk fo
y n

to a
S

ree
Be f
Sem II, 2011...IT3106... Operating Systems...G5-G8 40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy