MIT6_004s09_lec21

MIT6_004s09_lec21 - MIT OpenCourseWare http:/ocw.mit.edu...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
MIT OpenCourseWare http://ocw.mit.edu For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms . 6.004 Computation Structures Spring 2009
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
L21 – Processes & Synchronization 1 6.004 – Spring 2009 4/28/09 Processes, Synchronization, & Deadlock Lab #7 due Thursday modified 4/27/09 11:15 L21 – Processes & Synchronization 2 6.004 – Spring 2009 4/28/09 Interprocess Communication Why communicate? ± Concurrency ± Asynchrony ± Processes as a programming 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 Code Stack Data Shared Data Code Stack Data P PRODUCER C CONSUMER loop: <xxx>; send(c); goto loop loop: c = rcv(); <yyy>; goto loop L21 – Processes & Synchronization 3 6.004 – Spring 2009 4/28/09 Synchronous Communication PRODUCER 2 <xxx> 3 <xxx> <xxx> 1 send 1 send 2 send 3 CONSUMER <yyy> 1 rcv 1 <yyy> 2 rcv 2 <yyy> 3 rcv 3 loop: <xxx>; send(c); goto loop loop: c = rcv(); <yyy>; goto loop rcv i send i +1 send i rcv i Can’t CONSUME data before it’s PRODUCED ±² ³² Precedence Constraints: ± precedes ³ Producer can’t “OVERWRITE” data before it’s consumed L21 – Processes & Synchronization 4 6.004 – Spring 2009 4/28/09 FIFO Buffering RELAXES interprocess synchronization constraints. Buffering relaxes the following OVERWRITE constraint to: P C N-character FIFO buffer <xxx>; send(c 0 ); rcv(); //c 0 <yyy>; <xxx>; send(c 1 ); rcv(); //c 1 <yyy>; <xxx>; send(c 2 ); rcv(); //c 2 <yyy>; <xxx>; send(c 3 ); time c 0 c 1 c 2 c 0 c 1 c 2 c 0 c 1 c 2 c 0 c 1 c 2 3 rcv i send i+N c 0 c 1 c 2 3 c 0 c 1 c 0 Read ptr Write ptr c 0 “Ring Buffer:”
Background image of page 2
L21 – Processes & Synchronization 5 6.004 – Spring 2009 4/28/09 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 */ int in=0, out=0; SHARED MEMORY: 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/09 Semaphores (Dijkstra) Programming construct for synchronization: ± NEW DATA TYPE: semaphore , integer-valued semaphore s = K; /* initialize s to K */ ± NEW OPERATIONS (defined on semaphores): ± wait(semaphore s) stall current process if (s <= 0), otherwise s = s – 1 ± signal(semaphore s) s = s + 1, (can have side effect of le±ing other processes proceed) ± SEMANTIC GUARANTEE: A semaphore s initialized to K enforces 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 the i th call to signal completes.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

MIT6_004s09_lec21 - MIT OpenCourseWare http:/ocw.mit.edu...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online