L20-4up

L20-4up - L20 – Processes& Synchronization 1 6.004 –...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: L20 – Processes & Synchronization 1 6.004 – Fal 2010 11/18/10 Processes, Synchronization, & Deadlock Lab #7 due tonight L20 – Processes & Synchronization 2 6.004 – Fal 2010 11/18/10 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: l PRODUCER-CONSUMER z 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 L20 – Processes & Synchronization 3 6.004 – Fal 2010 11/18/10 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: l ! precedes # z • Producer can ` t l OVERWRITE z data before it ` s consumed L20 – Processes & Synchronization 4 6.004 – Fal 2010 11/18/10 FIFO Bu ff ering RELAXES interprocess synchronization constraints. Bu ff ering relaxes the following OVERWRITE constraint to: P C N-character FIFO bu ff er <xxx>; send(c ); rcv(); //c <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 c 3 rcv i send i+N c 0 c 1 c 2 c 3 c 0 c 1 c 0 c 0 c 0 Read ptr Write ptr c 0 l Ring Bu ff er: z L20 – Processes & Synchronization 5 6.004 – Fal 2010 11/18/10 Example: Bounded Bu ff er 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() ) L20 – Processes & Synchronization 6 6.004 – Fal 2010 11/18/10 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 e ff ect 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....
View Full Document

This document was uploaded on 03/07/2011.

Page1 / 7

L20-4up - L20 – Processes& Synchronization 1 6.004 –...

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

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