L20-4up

L20-4up - Processes Synchronization Deadlock Interprocess...

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

View Full Document Right Arrow Icon
L20 – Processes & Synchronization 1 6.004 – Fall 2010 11/18/10 Processes, Synchronization, & Deadlock Lab #7 due tonight L20 – Processes & Synchronization 2 6.004 – Fall 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: 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 L20 – Processes & Synchronization 3 6.004 – Fall 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: ! precedes # Producer can t OVERWRITE data before it s consumed L20 – Processes & Synchronization 4 6.004 – Fall 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 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 1 c 2 c 3 rcv i send i+N 1 c 2 c 3 c 1 c 0 c 0 Read ptr Write ptr Ring Bu ff er:
Image of page 1

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

View Full Document Right Arrow Icon
L20 – Processes & Synchronization 5 6.004 – Fall 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 – Fall 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. Often you will see P(s) used for wait(s) and V(s) used for signal(s)! P = proberen (test) or pakken (grab) V= verhogen (increase) L20 – Processes & Synchronization 7 6.004 – Fall 2010 11/18/10 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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern