Professional Documents
Culture Documents
Operating System Btech Process Synchronization Delhi Technological University Instructor: Divya Sethia Divyashikha@dce - Edu
Operating System Btech Process Synchronization Delhi Technological University Instructor: Divya Sethia Divyashikha@dce - Edu
Btech
Process Synchronization
Delhi Technological university
Instructor: Divya Sethia
divyashikha@dce.edu
Producer
while (true)
register1 = count
register1 = register1 + 1
count = register1
• count-- could be implemented as
register2 = count
register2 = register2 - 1
count = register2
Concurrent execution of counter++ and counter– is equivalent to sequential
execution of lower level statements interleaved in some arbitary order
Order within each high-level statement is preserved
Race Condition
• count++ could be implemented as in machine language
register1 = count
register1 = register1 + 1
count = register1
• count-- could be implemented as
register2 = count
register2 = register2 - 1
count = register2
• Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = count {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = count {register2 = 5}
S3: consumer execute register2 = register2 - 1 {register2 = 4}
S4: producer execute count = register1 {count = 6 }
S5: consumer execute count = register2 {count = 4}
• Incorrect state counter =4 indicating 4 buffers are full
where there are 5 buffers full
• If T4 T5 are interchanged counter = 6
• We allowed both processes to manipulate counter
concurrently
Race Condition: several processes access and manipulate
same data concurrently and outcome of execution
depends on particular order in which access takes
place.
Guard against race condition: Only one process allowed
to manipulate variable counter (require
synchronization)
Critical Section
Solution to Critical-Section Problem
1. Mutual Exclusion - If process Pi is executing in its critical
section, then no other processes can be executing in their
critical sections
2. Progress - If no process is executing in its critical section
and there exist some processes that wish to enter their
critical section, then the selection of the processes that
will enter the critical section next cannot be postponed
indefinitely
3. Bounded Waiting - 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
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the N processes
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
} while (TRUE);
To enter critical section Pi sets flag[i]=true and sets turn =j asserting that if other
process wishes to enter CS it can do so.
If both processes try to enter at the same time, turn will be set to both I and j roughly
at same time. But only one of the assignments will last – the other will occur but will
be overwritten imm.
Eventually value of turn decides which of two processes is allowed to enter its CS first.
// critical section
lock = FALSE;
// remainder section
} while ( TRUE);
Test and Set evaluation
P0 P1
Lock = False Lock=False
1. TS(&Lock) 2. TS(&L) when invoked L=true
Target/Lock = False Target /Lock = True
Rv = target = false rv = target = true
Target/Lock = true target = true
Ret rv = false ret rv=true
3.Breaks while loop and 5. while loop continues
enters CS breaks only after lock = F
4.L = false
Swap Instruction
• Definition:
// critical section
lock = FALSE;
// remainder section
} while ( TRUE);
CS satisfaction
• Previous synch techniques provide mutual
exclusion but do not satisfy bounded waiting
requirement
• Boolean waiting[n], boolean lock set to false
• Pi can enter CS either waiting[i] = False or key
== false
• Key is local
Bounded Wait Mutual Exclusion with Test and Set
Semaphore
• Test Set and Swap hardware solution not easy to use by
programmer.
• Less complicated
Semaphore
• Definition of wait():
wait (S) {
while S <= 0
; // no-op
S--;
}
• Definition of signal():
signal (S) {
S++;
}
Semaphore(Cont.)
• All modifications to integer value of semaphore in wait() and
signal() operations must be executed indivisibly.
Semaphore Usage(Cont.)
• While a process is in its critical section, any other process that tries
to enter its critical section must loop continuously in entry code. -
continual looping is clearly a problem in real multiprogramming
system, where a single CPU is shared among many processes.
• Busy waiting wastes CPU cycles that some other process might be
able to use productively. This type of semaphore is also called a
spinlock because process “spins” while waiting for the lock.
• But rather than engaging in busy waiting, process can block itself.
typedef struct{
int value;
struct process *list;
}semaphore;
• Implementation of signal:
Signal (S){
value++;
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); } }
Semaphore
• If semaphore is negative => number of processes waiting on that
semaphore since in wait the decrement is done before blocking
Semaphore
• Multiprocessor environment interrupts must
be disabled on every processor else
instructions from diff processes maybe
interleaved. This is difficult and also
diminishes performance.
• Alternate on SMPs use spinlocks to ensure
wait and signal are performed atomically.
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 P0 and P1 be the two processes, each accessing two
semaphores, S and Q, initialized to 1
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. .
signal (S); signal (Q);
signal (Q); signal (S);
• Suppose that P0 executes wait(S) and then P1 execute wait(Q).
When P0 executes wait(Q), it must wait until P1 executes signal(Q).
Similarly, when P1 executes wait(S), it must wait until P0 executes
signal(S). Since these signal() operations can not be executed, P0
and P1 are deadlocked.
Classical Problems of
Synchronization
• Bounded-Buffer Problem
• Dining-Philosophers Problem
Bounded-Buffer Problem
do {
// produce an item
wait (empty);
wait (mutex);
// add the item to the buffer
signal (mutex);
signal (full);
} while (true);
Bounded Buffer Problem (Cont.)
• The structure of the consumer process
do {
wait (full);
wait (mutex);
Readers-Writers Problem
• A data set is shared among a number of concurrent processes
–Readers: only read the data set; they do not perform any updates
–Writers: can both read and write.
• Problem – allow multiple readers to read at the same time. Only
one single writer can access the shared data at the same time.
readers-writers problem has several variations, all involving priorities.
-First readers-writers problem: no reader will be kept waiting unless
a writer has already obtained permission to use the shared object.
-Second readers-writers problem: once the writer is ready, that
writer performs its write as soon as possible.
• A solution to either problem may result in starvation. In the first
case, writers may starve; in the second case, readers may starve.
Readers-Writers Problem (Cont.)
• Shared Data
– Data set
– Semaphore mutex initialized to 1.
– Semaphore wrt initialized to 1.
– Integer readcount initialized to 0.
semaphore mutex, wrt; int readcount;
• mutex semaphore is used to ensure mutual exclusion when
variable readcount is updated. The readcount variable keeps track
of how many processes are currently reading the object.
• The semaphore wrt functions as a mutual exclusion semaphore for
writers. It is common to both reader and writer processes. Used by
first and last reader that enters or exits the critical section. It is not
used by readers who enter/exit while other readers are in their
critical sections.
do {
wait (wrt) ;
// writing is performed
signal (wrt) ;
} while (true)
Readers-Writers Problem (Cont.)
• The structure of a reader process
do {
wait (mutex) ;
readcount ++ ;
if (readercount == 1) wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (redacount == 0) signal (wrt) ;
signal (mutex) ;
} while (true)
• Five philosophers who spend their lives thinking and eating, share
a circular table surrounded by five chairs, each belonging to one
philosopher. In the center of the table is a bowl of rice, and the
table is laid with five single chopsticks.
• When a philosopher thinks, she does not interact with her
colleagues. From time to time, a philosopher gets hungry and tries
to pick up two chopsticks that are closest to her. A philosopher
may pick up only one chopstick at a time.
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (true) ;
Semaphore problems
• If used incorrectly can lead to problems
• Interchange order of wait and signal causes several
processes running in the CS simul.
Monitors Cont.
• Syntax of a monitor:
monitor monitor-name {
// shared variable declarations
procedure P1 (…) { …. }
…
Condition Variables
• Synchronization mechanisms are provided by the condition
construct
condition x, y;
• Two operations can be invoked on a condition variable:
– x.wait () – a process that invokes the operation is
suspended.
– x.signal () – resumes one of processes (if any) that
invoked x.wait ()
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
End of Chapter 6