Download Processes, Synchronization - Computation Structures - Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!
L21 – Processes & Synchronization 1
4/28/
Processes, Synchronization, & Deadlock
Lab #7 due Thursday
modified 4/27/09 11:
L21 – Processes & Synchronization 2
6.004 – Spring 2009
4/28/
Interprocess Communication
Why communicate?
- ^ Concurrency • ^ Asynchrony • ^ Processes as aprogramming primitive • ^ Data/Event driven
How to communicate?
- ^ Shared Memory(overlapping contexts)... • ^ Supervisor calls • ^ Synchronization instructions,(hardware support)
Classic Example:“^ PRODUCER-CONSUMER” Problem:Real-World Examples:UNIX pipeline,Word processor/Printer Driver,Preprocessor/Compiler,Compiler/Assembler
P^1
P^2 CodeStackData
SharedData CodeStackData
PRODUCER^ P
C
CONSUMER
loop:
;send(c);goto loop
loop:
c = rcv();;goto loop
L21 – Processes & Synchronization 3
4/28/
Synchronous Communication
PRODUCER
send
send
send
CONSUMER^
rcv^1
rcv^2
rcv^3
loop:^
;send(c);goto loop
loop:^
c = rcv();;goto loop
rcvi^
send
i
send +
i^
rcvi
-^ Can’t CONSUME databefore it’s PRODUCED
^
Precedence
Constraints:“
precedes
- Producer can’t“OVERWRITE” databefore it’s consumed
L21 – Processes & Synchronization 4
6.004 – Spring 2009
4/28/
FIFO Buffering
RELAXES interprocesssynchronization constraints.Buffering relaxes the followingOVERWRITE constraint to:
P^
C
N-characterFIFO buffer ;send(c
); 0
rcv(); //c
0 ;
;send(c
); 1
rcv(); //c
1 ;
;send(c
); 2
rcv(); //c
2 ; ;send(c
); 3
time
cc^0
c^2
c^ c^0
c^2
c^ c^0
c^2
cc^0
c^2 c^3
rcvi^
send
i+N^ c^0
ccc 1 23
cc^ c^0 c^ c^00
Read ptrWrite ptr
c^0
“Ring Buffer:”
L21 – Processes & Synchronization 5
4/28/
Example: Bounded Buffer Problem
send(char c){
buf[in] = c;in = (in+1)% N; }
char rcv(){^
char
c; c = buf[out];out = (out+1)% N;return c; }
PRODUCER:
CONSUMER:
char buf[N];
/ The buffer /
SHARED MEMORY: int in=0, out=0; Problem: Doesn’t enforce precedence constraints(e.g. rcv( ) could be invoked prior to any send() )
L21 – Processes & Synchronization 6
6.004 – Spring 2009
4/28/
Semaphores (Dijkstra)
Programming construct for synchronization:
semaphore
, integer-valued
semaphore
s^
=^ K;
/*^
initialize
s^
to^
K^ */
- ^ NEW OPERATIONS (defined on semaphores):
s)
stall current process if (s <= 0), otherwise s = s – 1 • signal(semaphore
s)
s = s + 1, (can have side effect of letting other processes proceed)
- ^ SEMANTIC GUARANTEE: A semaphore s initialized to Kenforces the constraint:
wait(s)
i+K
signal(s)
i
This is a
precedence relationship, meaning that the (i+K)
th
call to wait cannot proceed before
theth (^) i call to signal completes.
Often you will seeP(s) used for wait(s)
and V(s) used for signal(s)!P = “proberen”(test) or“pakken”(grab)V= “verhogen”(increase)
L21 – Processes & Synchronization 7
4/28/
Semaphores for Resource Allocation
ABSTRACT PROBLEM:• POOL of K resources• Many processes, each needs resource for occasional
uninterrupted periods• MUST guarantee that at most K resources are in use at any time.
Semaphore Solution:
In shared memory:^ semaphore s = K;
/ K resources*
In each process:^ ...wait(s);
/ Allocate one*
/ use it for a*
*while /
signal(s);
/ return it to*
pool
Invariant: Semaphore value = number of resources left in pool
L21 – Processes & Synchronization 8
6.004 – Spring 2009
4/28/
Bounded Buffer Problem
w/Semaphores
send(char
c)
{
buf[in]
=^
c;
in^
=^ (in+1)%N; signal(chars); }
char
rcv() {^
char
c; wait(chars);c^ =
buf[out]; out
=^
(out+1)%N; return
c;
}
PRODUCER:
CONSUMER:
char buf[N];
/*^
The
buffer
int in=0,
out=0;
semaphore
chars=0;
SHARED MEMORY:
RESOURCE managed by semaphore: Characters in FIFO.DOES IT WORK?
L21 – Processes & Synchronization 13
4/28/
Semaphores for Mutual Exclusion…Debit(int account, int amount){ t = balance[account];balance[account] = t – amount;} RESOURCE managed by “lock” semaphore: Access to critical sectionISSUES:Granularity of lock
1 lock for whole balance database?1 lock per account?1 lock for all accounts ending in 004?
Implementation of wait() and signal() functions
“^
precedes
or
precedes
(i.e., they don’t overlap)
signal(lock); /* Finished with lock */
semaphore lock = 1;
wait(lock);
/* Wait for exclusive access */
L21 – Processes & Synchronization 14
6.004 – Spring 2009
4/28/
Producer/Consumer Atomicity ProblemsConsider multiple PRODUCER processes:
P^1
C
N-characterFIFO buffer
P^2
...buf[in]
=^
c;
in^
=^ (in+1)
%^
N;
...buf[in]
=^
c;
in = (in+1)
%^
N;
P^1
P^2
BUG: Producers interfere with each other.
L21 – Processes & Synchronization 15
4/28/
Bounded Buffer Problem w/^Semaphores
send(char c){
wait(space);wait(mutex);buf[in] = c;in = (in+1)%N;signal(mutex);signal(chars); }
char rcv(){^
char c;wait(chars);wait(mutex);c = buf[out];out = (out+1)%N;signal(mutex);signal(space);return c;}
PRODUCER:
CONSUMER:
char
buf[N];
/ The buffer /
int
in=0, out=0; SHARED MEMORY: semaphore chars=0, space=N;semaphore mutex=1;
even more
L21 – Processes & Synchronization 16
6.004 – Spring 2009
4/28/
The Power of Semaphores
send(char c){
wait(space);wait(mutex)buf[in] = c;in = (in+1)%N;signal(mutex);signal(chars); }
char
rcv() {^
char
c; wait(chars);wait(mutex);c^ =
buf[out]; out
=^
(out+1)%N; signal(mutex);signal(space);return
c;
}
PRODUCER:
CONSUMER:
char buf[N];
/*^
The
buffer
int in=0, out=0;semaphore chars=0,
space=N;
SHARED MEMORY: semaphore mutex=1;
A singlesynchronizationprimitive that enforcesboth: Precedencerelationships:^ send
i^
rcvi rcvi^
send
i+N
Mutual-exclusionrelationships:^ protect variables^ in
and
out
L21 – Processes & Synchronization 17
4/28/
Semaphore Implementations
Semaphore implementation must address a basic arbitration problem:how to choose among simultaneously waiting processes when a signaloccurs. This involves some basic atomicity assumption in theimplementation technology.Approaches:
- ^ SVC implementation, using atomicity of kernel handlers. Works intimeshared processor sharing a single uninterruptable kernel. • ^ Implementation by a special instruction (e.g. “test and set”), usingatomicity of single instruction execution. Works with shared-busmultiprocessors supporting atomic read-modify-write bustransactions. • ^ Implementation using atomicity of individual read or write operations.Complex, clever, 2-phase scheme devised by Dijkstra. Unused inpractice.
Bootstrapping: A simple lock (“binary semaphore”) allows easyimplementation of full semaphore support.
L21 – Processes & Synchronization 18
6.004 – Spring 2009
4/28/
Semaphores as Supervisor Callwait_h( ){ int *addr;addr = User.Regs[R0];
/* get arg */
if (addr <= 0) {User.Regs[XP] = User.Regs[XP] – 4;sleep(addr);} elseaddr = *addr - 1;} signal_h( ){ int *addr;addr = User.Regs[R0];
/* get arg */
*addr = *addr + 1;wakeup(addr);}
Calling sequence:… ||^
put
address
of
lock
||^
into
R
CMOVE(lock,
R0)
SVC(WAIT) SVC call is not interruptible^ since it is executed insupervisory mode.
L21 – Processes & Synchronization 19
4/28/
Atomicity Guaranteed, e.g. byBus protocols
H/W support for Semaphores
TCLR(RA, literal, RC)
test and clear location
PC^
PC + 4
EA^
Reg[Ra] + literal
Reg[Rc]
^ MEM[EA]
MEM[EA]
^0
Executed ATOMICALLY (cannot be interrupted)Can easily implement mutual exclusion using binary semaphore
wait: TCLR(R31, lock, R0)
BEQ(R0,wait) … critical section … CMOVE(1,R0)ST(R0, lock, R31)
wait(lock)signal(lock)
L21 – Processes & Synchronization 20
6.004 – Spring 2009
4/28/
Synchronization: The Dark SideThe indiscriminate use of synchronization constraints
can
introduce its own set of problems, particularly when aprocess requires access to more than oneprotected resource. Transfer(int account1, int account2, int amount){ wait(lock[account1]);wait(lock[account2]);balance[account1] = balance[account1] - amount;balance[account2] = balance[account2] + amount;signal(lock[account2]);signal(lock[account1]);}
Transfer(6001, 6004, 50) Transfer(6004, 6001, 50)
DEAD-LOCK!
L21 – Processes & Synchronization 25
6.004 – Spring 2009
4/28/
Summary
Communication among asynchronous processes requiressynchronization….
- ^ Precedence constraints: a partial ordering among operations • ^ Semaphores as a mechanism for enforcing precedenceconstraints • ^ Mutual exclusion (critical sections, atomic transactions) as acommon compound precedence constraint • ^ Solving Mutual Exclusion via binary semaphores • ^ Synchronization
serializes
operations, limits parallel execution.
Many alternative synchronization mechanisms exist!Deadlocks:
- ^ Consequence of undisciplined use of synchronization mechanism • ^ Can be avoided in special cases, detected and corrected in others.