CoSc 450: Programming Paradigms 06

Semaphores CoSc 450: Programming Paradigms 06

Semaphore Purpose: To prevent inefficient spin of the await statement.

Idea: Instead of spinning, the calls a method that puts it in a special queue of PCBs, the “blocked on semaphore” queue. 71447_CH08_Chapter08.qxd 1/28/09 12:27 AM Page 422

422 Chapter 8 Process Management

The PCB contains additional information to help the schedule the CPU. An example is a unique process identification number assigned by the system, labeled Process ID in Figure 8.18, that serves to reference the process. Sup- pose a user wants to terminate a process before it completes execution normally, and he knows the ID number is 782. He could issue a KILL(782) command that would cause the operating system to search through the queue of PCBs, find the PCB with ID 782, remove it from the queue, and deallocate it. Another example of information stored in the PCB is a record of the total amount of CPU time used so far by the suspended process. If the CPU becomes available and the operating system must decide which of several suspended processes gets the CPU, it can use the recorded time to make a fairFigure decision. 8.19 As a job progresses through the system toward completion, it passes through several states, as Figure 8.19 shows. The figure is in the form of a state transition diagram and is another example of a finite state machine. Each transition is labeled with the event that causes the change of state.

Waiting Figure 8.19 for I/O The state transition diagram for a I/O complete I/Orequest job in an operating system. Select to run

Start Ready Running Finish Create Terminate Time out process process

When a user submits a job for processing, the operating system creates a process for it by allocating a new PCB and attaching it to a queue of processes that are waiting for CPU time. It loads the program into main memory and sets the copy of PC in the PCB to the address of the first instruction of the process. That puts the job in the ready state. Eventually, the operating system should select the job to receive some processing time. It sets the alarm clock to generate an after a quantum of time and puts the copies of the registers from the PCB into the CPU. That puts the job in the running state. While in the running state, three things can happen: (1) The running process Transitions from the running state may time out if it is still executing when the alarm clock . If so, the oper- ating system attaches the process’s PCB to the ready queue, which puts it back in the ready state. (2) The process may complete its execution normally, in which case the last instruction it executes is an SVC to request that the operating system termi- nate it. (3) The process may need some input, in which case it executes an SVC for the request. The operating system would transfer the request to the appropriate I/O device and put the PCB in another queue of processes that are waiting for their I/O operations to complete. That puts the process in the waiting-for-I/O state. CoSc 450: Programming Paradigms 06

Add fourth state for a process 1. Ready in the ready queue

2. Running in the cpu

3. Waiting for I/O in the I/O-wait queue

4. Blocked in the blocked-on-semaphore queue Figure 8.29

Waiting for I/O I/O complete I/O request Select to run

Start Ready Running Finish Create Terminate Time out process process

(s) Waiting wait(s) for s State Changes of a Process

- ª - inactive ready - running completed

HY H HH ? HH H blocked

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.1 ≠

p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 !" p3..5 p8..10 want p ⇒# ∨ ≡ $ want p CoSc 450: Programming¬ Paradigms 06 !" ⇒# $ q9 ! ⇒# ← $ turn = 1Semaphore type Semaphore! S hasturn two= 2 fields: " S.V S.L

CoSc 450: Programming Paradigms 06 Semaphore atomic operations Memorize this

p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 !" p3..5 p8..10 want p ⇒# ∨ ≡ $ want p ¬ !" ⇒# $ q9 ! ⇒# ← $ turn = 1 ! turn = 2 " CoSc 450: Programming S .ParadigmsV 06 Semaphore Satomic.L operations Memorize this

S (k, /0 ) ← wait(S) S.V > 0 S.V S.V 1 ← − S.L S.L p ← ∪ p.state ← signal(S) S.L = /0 S.V S.V + 1 ← q S.L S.L S.L q ← − { } q.state ←

p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " p2 p8 turn = 2 ∧ ¬ ⇒ ¬ turn = 2 ! " " ⇒# $ turn = 2 p6 " ⇒# !"$ p3..5 p8..10 want p p6 ⇒# ∨ ≡ $ want p !" p3..5 p8..10 want¬ p ⇒# ∨ ≡!"$ want p ⇒# $ ¬ q9 !" ⇒# ! $ q9 ⇒# ← $ = ! turn 1 ⇒# ← $ turn = 1 ! turn = 2 ! turn = 2 " CoSc 450: Programming S .ParadigmsV 06 " S.V Semaphore Satomic.L operations S.L Memorize this

S (k, /0 ) ← S (k, /0 ) ← wait(S) wait(S) S.V > 0 S.V > 0 S.V S.V 1 S.V S.V 1 ← − ← − S.L S.L p S.L S.L p ← ∪ ← ∪ p.state p.state ← ← signal(S) signal(S) S.L = /0 S.L = /0 S.V S.V + 1 S.V S.V + 1 ← ← q S.L q S.L S.L S.L q S.L S.L q ← − { } ← − { } q.state q.state ← ←

p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 p2 p8 turn = 2 ∧ ¬ ⇒ !"¬ p3..5 p8..10 want p ! ⇒#" ∨ ≡ $ want p p2 p8 turn = 2 ¬ ∧ ¬ ⇒ ¬ turn = 2 !" ! " ⇒# $ " q9 ⇒# $ turn = 2 p6 ! " ⇒# ← $ ⇒# !"$ p3..5 turnp8..=101 want p p6 ⇒# ∨ ≡ $ want p! turn = 2 !" p3..5 p8..10 want¬ p ⇒# ∨ ≡!"$ " want p ⇒# S.V $ ¬ q9 !" ⇒# ! $ S.L q9 ⇒# ← $ = ! turn 1 ⇒# ← $ turn = 1 ! turn = 2 ! turn = 2 " S (k, /0 ) CoSc 450: Programming S .ParadigmsV ← 06 " S.V Semaphore Satomic.L wait operations(S) S.L Memorize this S.V > 0 S.V S.V 1 ← − S (k, /0 )S.L S.L p ← ← ∪ S (k, /0 ) p.state ← ← wait(S) wait(S) S.V >signal0 (S) S.V > 0 S.V S.SV.L =1 /0 S.V S.V 1 ← −S.V S.V + 1 ← − ← S.L S.L p S.L S.L p ← ∪ q S.L ← ∪ p.state p.state ← S.L S.L q ← − { } ← q.state signal(S) ← signal(S) S.L = /0 S.L = /0 S.V S.V + 1 S.V S.V + 1 ← ← q S.L q S.L S.L S.L q S.L S.L q ← − { } ← − { } q.state q.state ← ←

CoSc 450: Programming Paradigms 06

Binary semaphore The value S.V is only allowed to be 0 or 1.

Also called “mutex” for . CoSc 450: Programming Paradigms 06

Critical section The problem is trivial when you have semaphores. 6.1: Critical section with semaphores (two processes) binary semaphore S (1, ) ¿ ; p q loop forever loop forever p1: non-critical section q1: non-critical section p2: wait(S) q2: wait(S) p3: critical section q3: critical section p4: signal(S) q4: signal(S)

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.2 ≠

p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 !" p3..5 p8..10 want p p2 p8 turn = 2 ⇒# ∨ ≡ $ ∧ ¬ ⇒ ¬ want p ¬ ! " !" ⇒# $ turn = 2 q9 " ! ⇒# $ ⇒# ← $ p6 turn = 1 !" p3..5 p8..10 want p! turn = 2 ⇒# ∨ ≡ $ want p " ¬ S.V !" ⇒# $ CoScq 9450: Programming Paradigms S.L 06 ! ⇒# ← $ Class exercise turn = 1 Construct the first part of the state transition diagram: ! turn = 2 S (k, /0 ) p, p, q, q, p, p," (p and q), ... ← S.V wait(S) Algorithm 6.1: Critical section with semaphores (two processes) S.L binary semaphore S (1, ) . > ¿ ;S V 0 p S.V q S.V 1 loop forever loop forever ← − p1: non-critical section q1: non-critical section p2: wait(S) q2: wait(S)S.L S.L p ← ∪ p3: criticalS section(k, /0 ) q3: criticalp section.state p4: signal(S)← q4: signal(S) ← wait(S) signal(S) S.V > 0 S.L = /0 S.V S.V + 1 S.V S.V 1 ← ← − M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.2 . S.L S.L p ≠q S L ← ∪ S.L S.L q p.state ← − { } ← q.state ← signal(S) S.L = /0 S.V S.V + 1 ← q S.L S.L S.L q ← − { } q.state ←

p1,q1 (1, /0 ) p1,q1 p2,q1 p1,q1 (1, /0 ) (1, /0 ) (1, /0 ) p2,q1 p2,q1 p3,q1 (1, /0 ) (0, /0 ) (1, /0 ) p1,q1 p3,q1 (1, /0 ) p1,q1 p3,q2 p(03,,/0q)1 (1, /0 ) (0, /0 ) (0, /0 ) p2,q1 p3,q2 p1,q1 (p11, ,/0 q) 1 p2,q1 p3,q3 p(03,,/0q)2 (1, /0 ) ! (1, /0 ) (1, /0 ) (0, q ) (0, /0 ) p3,q1 p2,q1 { } p3,q3! p1,q1 (p02, ,/0 q) 1 (p13,,/0 q)1 (0, q ) p4,q3! p3,q3! (1, {/0 ) } p(31,,q/0 2) (0, /0 ) A (0, q ) B (0, q ) C D p3,q1 { } p4,{q3}! (0, /0 ) p1,q1 p p2,q1 p p3,q1 q (p03,,/0 q)2 (0, q ) p(11,,/0q)3 p4,q3! (1, {/0 ) } p(30,,q/0 3)! (p03,,q/0 )2 (0, /0 ) (0, q ) p1,{q3} (0, q ) (0, /0 )q p2,q1 p3,q1 F p3{,q}2 E p3,q3! (0, /0 ) p(12,,/0q)3 p (p01,,/0 q)3 p p(40,,q/0 3)! p (p03,,qq3! ) { } (0, /0 ) (0, q ) (0, q ) (0, /0 ) p2,q3 { } { } p3,q1 p3,q2 p3,q3 p4,q3! (0, /0 q) ! (0, /0 ) (0, /0 ) p(1,,q3q ) (p04,,qq3! ) p1,q4 p2,q3 0 { } (0, /0 {) } (0, q ) (0, /0 ) (p01,,/0q)4 { } p3,q2 p3,q3! p4,q3! p1,q3 (0, /0 ) p ,q p1,q3 (0, /0 ) (0, q ) (20, 3q ) (0, /0 ) p1,{q4} (0, /0 {) } (0, /0 ) p3,q3 (0, /0 ) States !E and F havep4,q no3! transitionp1 on,q3 q becausep q2, ,isqq 3 p1,q4 2 3 blocked.(0, q ) q3’ means(0, qq3) cannot execute.(0, /0 ) (00,,/0 /0 )) { } { } (0, /0 ) p4,q3! p1,q3 p2,q3 p11,,qq44 (0, q ) (00,,/0 /0 )) { } (0, /0 ) (0, /0 ) p1,q3 p2,q3 p1,q4 (0, /0 ) (0, /0 ) (0, /0 )

p2,q3 p1,q4 (0, /0 ) (0, /0 ) p1,q4 (0, /0 )

p2 p8 turn = 2 ∧ ¬ ⇒ ¬ ! " turn = 2 " ⇒# $ p6 !" p3..5 p8..10 want p p2 p8 turn = 2 ⇒# ∨ ≡ $ ∧ ¬ ⇒ ¬ want p ! " ¬ !" ⇒# $ turn = 2 q9 " ! ⇒# $ ⇒# ← $ p6 turn = 1 !" p3..5 p8..10 want p! turn = 2 ⇒# ∨ ≡ $ want p " ¬ S.V !" ⇒# $ CoScq 9450: Programming Paradigms S.L 06 ! ⇒# ← $ turn = 1 Class exercise ! turn = 2 Construct the complete state transitionS ( kdiagram, /0 ) " ← S.V Algorithm 6.2: Critical section withwait semaphores(S) (two proc., abbrev.) S.L binary semaphore S (1S, .V) > 0 ¿ ; p S.V S.Vq 1 ← − loop forever loop forever p1: wait(S) q1: wait(S)S.L S.L p ← ∪ p2: signal(S) S (k, /0 ) q2: signal(S)p.state ← ← wait(S) signal(S) S.V > 0 S.L = /0 S.V S.V + 1 S.V S.V 1 ← ← − S.L S.L p q S.L ← ∪ S.L S.L q p.state ← − { } M. Ben-Ari. Principles of Concurrent← and Distributed Programming, Secondq. editionstate c M. Ben-Ari 2006 Slide 6.3 ≠← signal(S) S.L = /0 S.V S.V + 1 ← q S.L S.L S.L q ← − { } q.state ←

p1,q1 p1,q1 (1, /0 ) (1, /0 ) p1,q1 p1,q1 p2,q1 (1, /0 ) (p12,,/0 q)1 (1, /0 ) (1, /0 ) p2,q1 p2,q1 p3,q1 (1, /0 ) (p13,,/0 q)1 (0, /0 ) (0, /0 ) p3,q1 p ,q p3,q2 3 1 (0, /0 ) (p03,,/0 q)2 (0, /0 ) (0, /0 ) p3,q2 p3,q3 p3,q2 (0, /0 ) ! (0, q ) (p03,,/0 q)3! { } p3,q3! (0, q ) p4,q3! p3,{q3} (0, q ) ! (0,{ q} ) (p04,,qq3!) { } { } p4,q3! (0, q ) p1,q3 { } (0, q ) p4,q3! (0,{/0 )} (p01,,qq3 ) p1,q3 { } p2,q3 (0, /0 ) (0, /0 ) p1,q3 (0, /0 ) (p02,,/0 q)3 p2,q3 p1,q4 (0, /0 ) (0, /0 ) p2,q3 (0, /0 ) CoSc 450: Programming Paradigms p1,q4 06 (p01,,/0 q)4 p2,q1 (0, /0 ) (0, /0 ) (0, /0 ) p1,q4 p2,q1 (p02,,/0 q)1 q p2,q2! (0, /0 ) (0, /0 ) (0, q ) p2,q1 { } p p2,q2 p p p1,q2! (p02,,/0 q)2! (0, q ) p1,q1 (0, q ) (0,{/0 )} { } (1, /0 ) q p2,q2! p1,q2 p2!,q2 (p01,,qq2 ) q(0, /0 ) { } (0, q ) p2,q1 (0, /0 ) { } (1, /0 ) q p1,q2 p p2!,q2 ( , ,) (0, p ) p02!/0 q2 { } p3,q1 (0, q ) p ,{q } (0, /0 ) 2! 2 (0, q ) { } p3,q2 (0, /0 ) p3,q3! (0, q ) { }

p4,q3! (0, q ) { } p1,q3 (0, /0 )

p2,q3 (0, /0 )

p1,q4 (0, /0 )

CoSc 450: Programming Paradigms 06

Correctness

p2,q2

p wait(S) p signal(S) q p q

CoSc 450: Programming Paradigms 06

Correctness

p2,q2

p wait(S) p signal(S) q p q

CoSc 450: Programming Paradigms 06

Correctness

p2,q2

p wait(S) p signal(S) q p q

CoSc 450: Programming Paradigms 06

Correctness

p2,q2

p wait(S) p signal(S) q p q

p1,q1 p1,q1 (1, /0 ) (1, /0 ) p1,q1 p1,q1 p2,q1 (1, /0 ) (p12,,/0 q)1 (1, /0 ) (1, /0 ) p2,q1 p2,q1 p3,q1 (1, /0 ) (p13,,/0 q)1 (0, /0 ) (0, /0 ) p3,q1 p ,q p3,q2 3 1 (0, /0 ) (p03,,/0 q)2 (0, /0 ) (0, /0 ) p3,q2 p3,q3 p3,q2 (0, /0 ) ! (0, q ) (p03,,/0 q)3! { } p3,q3! (0, q ) p4,q3! p3,{q3} (0, q ) ! (0,{ q} ) (p04,,qq3!) { } { } p4,q3! (0, q ) p1,q3 { } (0, q ) p4,q3! (0,{/0 )} (p01,,qq3 ) p1,q3 { } p2,q3 (0, /0 ) (0, /0 ) p1,q3 (0, /0 ) (p02,,/0 q)3 p2,q3 p1,q4 (0, /0 ) (0, /0 ) p2,q3 (0, /0 ) p2,q2 CoSc 450: Programming Paradigms p1,q4 06 (p01,,/0 q)4 p2,q1 (0, /0 ) (0, /0 ) (0, /0 ) p1,q4 p2,q1 (p02,,/0 q)1 q p2,q2! (0, /0 ) p wait(S) (0, /0 ) (0, q ) p p2,q1 signal(S) q { } p p2,q2 p q p p p1,q2! (p02,,/0 q)2! (0, q ) p1,q1 (0, q ) (0,{/0 )} { } (1, /0 ) q p2,q2! p1,q2 p2!,q2 (p01,,qq2 ) q(0, /0 ) { } (0, q ) p2,q1 (0, /0 ) { } (1, /0 ) q p1,q2 p p2!,q2 ( , ,) (0, p ) p02!/0 q2 { } p3,q1 (0, q ) p ,{q } (0, /0 ) 2! 2 (0, q ) { } p3,q2 (0, /0 ) p3,q3! (0, q ) { }

p4,q3! (0, q ) { } p1,q3 (0, /0 )

p2,q3 (0, /0 )

p1,q4 (0, /0 )

CoSc 450: Programming Paradigms 06

Complete execution of wait(S) When wait(S) causes a process to block, the wait statement has not been completely executed.

The wait(S) statement completes its execution when it is unblocked.

p1,q1 p1,q1 (1, /0 ) (1, /0 ) p1,q1 p1,q1 p2,q1 (1, /0 ) (p12,,/0 q)1 (1, /0 ) (1, /0 ) p2,q1 p2,q1 p3,q1 (1, /0 ) (p13,,/0 q)1 (0, /0 ) (0, /0 ) p3,q1 p ,q p3,q2 3 1 (0, /0 ) (p03,,/0 q)2 (0, /0 ) (0, /0 ) p3,q2 p3,q3 p3,q2 (0, /0 ) ! (0, q ) (p03,,/0 q)3! { } p3,q3! (0, q ) p4,q3! p3,{q3} (0, q ) ! (0,{ q} ) (p04,,qq3!) { } { } p4,q3! (0, q ) p1,q3 { } (0, q ) p4,q3! (0,{/0 )} (p01,,qq3 ) p1,q3 { } p2,q3 (0, /0 ) (0, /0 ) p1,q3 (0, /0 ) (p02,,/0 q)3 p2,q3 p1,q4 (0, /0 ) (0, /0 ) CoSc 450: Programmingp2, qParadigms3 (0, /0 ) 06 p2,q2 p1,q4 (p01,,/0 q)4 p2,q1 Complete execution of wait(S)(0, /0 ) (0, /0 ) (0, /0 ) p1,q4 p2,q1 (p02,,/0 q)1 q p2,q2! (0, /0 ) p wait(S) (0, /0 ) (0, q ) p p2,q1 signal(S) q { } p p2,q2 p q p p p1,q2! (p02,,/0 q)2! (0, q ) p1,q1 (0, q ) (0,{/0 )} { } (1, /0 ) q p2,q2! p1,q2 p2!,q2 (p01,,qq2 ) q(0, /0 ) { } (0, q ) p2,q1 (0, /0 ) { } (1, /0 ) q p1,q2 p p2!,q2 ( , ,) (0, p ) p02!/0 q2 { } p3,q1 (0, q ) p ,{q } (0, /0 ) 2! 2 (0, q ) { } p3,q2 (0, /0 ) p3,q3! (0, q ) { }

p4,q3! (0, q ) { } p1,q3 (0, /0 )

p2,q3 (0, /0 )

p1,q4 (0, /0 )

CoSc 450: Programming Paradigms 06

The strength of a semaphore A strong semaphore uses a queue (FIFO) of blocked processes. The process unblocked is the one in the queue for the longest time.

A weak semaphore uses a of blocked processes. The process unblocked is unpredictable.

The semaphore policy is distinct from the scheduling policy in the ready queue. CoSc 450: Programming Paradigms 06

Semaphores in C++

C++ has the mutex binary semaphore class.

wait(S) is s.lock()

signal(S) is s.unlock()

Demo Algorithm-6-1.cpp Algorithm-6-1.cpp

#include #include #include #include #include "Util450.cpp" using namespace std; volatile int n = 0; mutex s; Algorithm-6-1.cpp void pRun() { int temp; for (int i = 0; i < 10; i++) { randomDelay(20); cout << "p.i == " << i << endl; // Preprotocol s.lock(); // Critical section temp = n; randomDelay(10); n = temp + 1; // Postprotocol s.unlock(); } } Algorithm-6-1.cpp void qRun() { int temp; for (int i = 0; i < 10; i++) { randomDelay(10); cout << "q.i == " << i << endl; // Preprotocol s.lock(); // Critical section temp = n; randomDelay(10); n = temp + 1; // Postprotocol s.unlock(); } } Algorithm-6-1.cpp int main(int argc, char **argv) { p(pRun); thread q(qRun); p.join(); q.join(); cout << "The value of n is " << n << endl; return EXIT_SUCCESS; } CoSc 450: Programming Paradigms 06

Counting Semaphores in C++

C++ does not have a counting semaphore class.

You can build one using a mutex and a condition variable (chapter 7).

Demo Algorithm-6-1b.cpp

Predict when s is initialized to 2. Predict when s is initialized to 0. Algorithm-6-1b.cpp

#include #include #include #include #include "Util450.cpp" Semaphore class implemented in Util450.cpp using namespace std; volatile int n = 0; Semaphore s(1); Algorithm-6-1b.cpp void pRun() { int temp; for (int i = 0; i < 10; i++) { randomDelay(20); cout << "p.i == " << i << endl; // Preprotocol s.wait(); // Critical section temp = n; randomDelay(10); n = temp + 1; // Postprotocol s.signal(); } } Algorithm-6-1b.cpp void qRun() { int temp; for (int i = 0; i < 10; i++) { randomDelay(10); cout << "q.i == " << i << endl; // Preprotocol s.wait(); // Critical section temp = n; randomDelay(10); n = temp + 1; // Postprotocol s.signal(); } } Algorithm-6-1b.cpp int main(int argc, char **argv) { thread p(pRun); thread q(qRun); p.join(); q.join(); cout << "The value of n is " << n << endl; return EXIT_SUCCESS; } CoSc 450: Programming Paradigms 06

Semaphores in Java Constructor:

• The first parameter is the integer value of S.V • The second optional parameter is true for fair scheduling (FIFO, a strong semaphore).

• The default value is false (a weak semaphore). CoSc 450: Programming Paradigms 06

Semaphores in Java wait(S) is s.acquire() signal(S) is s.release()

Demo Algorithm0601.java Algorithm0601.java public class Algorithm0601 extends Thread {

static volatile int n = 0; static Semaphore s = new Semaphore(1); private int processID;

Algorithm0601(int pID) { processID = pID; } Algorithm0601.java public void run() { int temp; if (processID == 1) { // Process p for (int i = 0; i < 10; i++) { try { randomDelay(20); System.out.println("p.i == " + i); // Preprotocol s.acquire(); // Critical section randomDelay(20); temp = n; randomDelay(20); n = temp + 1; // Postprotocol s.release(); } catch (InterruptedException e) { } } Algorithm0601.java

} else if (processID == 2) { // Process q for (int i = 0; i < 10; i++) { try { randomDelay(20); System.out.println("q.i == " + i); // Preprotocol s.acquire(); // Critical section randomDelay(20); temp = n; randomDelay(20); n = temp + 1; // Postprotocol s.release(); } catch (InterruptedException e) { } } } } Algorithm0601.java

public static void main(String[] args) { Algorithm0601 p = new Algorithm0601(1); Algorithm0601 q = new Algorithm0601(2); p.start(); q.start(); try { p.join(); q.join(); } catch (InterruptedException e) { } System.out.println("The value of n is " + n); } } CoSc 450: Programming Paradigms 06

Semaphore invariants For proving correctness without model checking

p2,q2

CoSc p 450: Programmingwait(S) Paradigms 06 p signal(S) q p qTheorem 6.1 Memorize this. k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

p2,q2

CoSc p 450: Programmingwait(S) Paradigms 06 p signal(S) q p qTheorem 6.1 Memorize this. k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

p2,q2

CoSc p 450: Programmingwait(S) Paradigms 06 p signal(S) q p qTheorem 6.1 Memorize this. k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

p2,q2

p wait(S) p signal(S) q p q k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ CoScS.V =450:k + #signalProgramming(S) #wait Paradigms(S) 06 − Theorem 6.1 S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − wait(S) S.V signal(S) S.V #wait(S)

#CS #CS+ S.V = 1 #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V − # $ #CS+ S.V = 1

p2,q2

p wait(S) p signal(S) q p q k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ CoScS.V =450:k + #signalProgramming(S) #wait Paradigms(S) 06 − Theorem 6.1 S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − wait(S) S.V signal(S) S.V #wait(S)

#CS #CS+ S.V = 1 #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V − # $ #CS+ S.V = 1

p2,q2

p wait(S) p signal(S) q p q k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ CoScS.V =450:k + #signalProgramming(S) #wait Paradigms(S) 06 − Theorem 6.1 S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − wait(S) S.V signal(S) S.V #wait(S)

#CS #CS+ S.V = 1 #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V − # $ #CS+ S.V = 1

p2,q2

p wait(S) p signal(S) q p q

k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − CoSc wait 450:(S) Programming Paradigms S.V06 signal(S) S.V Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; #CS ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) #CS+ S.V = 1 p2: signal(S)p q2: signal(S)q loop forever loop forever p1: wait(S) q1: wait(S) p2: signal(S) q2: signal(S) #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 − ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 # $ ≠ #CS+ S.V = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

p2,q2

p wait(S) p signal(S) q p q

k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − CoSc wait 450:(S) Programming Paradigms S.V06 signal(S) S.V Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; #CS ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) #CS+ S.V = 1 p2: signal(S)p q2: signal(S)q loop forever loop forever p1: wait(S) q1: wait(S) p2: signal(S) q2: signal(S) #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 − ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 # $ ≠ #CS+ S.V = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

p2,q2

p wait(S) p signal(S) q p q

k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − CoSc wait 450:(S) Programming Paradigms S.V06 signal(S) S.V Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; #CS ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) #CS+ S.V = 1 p2: signal(S)p q2: signal(S)q loop forever loop forever p1: wait(S) q1: wait(S) p2: signal(S) q2: signal(S) #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 − ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 # $ ≠ #CS+ S.V = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

p2,q2

p wait(S) p signal(S) q p q

k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − CoSc wait 450:(S) Programming Paradigms S.V06 signal(S) S.V Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; #CS ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) #CS+ S.V = 1 p2: signal(S)p q2: signal(S)q loop forever loop forever p1: wait(S) q1: wait(S) p2: signal(S) q2: signal(S) #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 − ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 # $ ≠ #CS+ S.V = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

p2,q2

p wait(S) p signal(S) q p q

k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − CoSc wait 450:(S) Programming Paradigms S.V06 signal(S) S.V Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; #CS ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) #CS+ S.V = 1 p2: signal(S)p q2: signal(S)q loop forever loop forever p1: wait(S) q1: wait(S) p2: signal(S) q2: signal(S) #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 − ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 # $ ≠ #CS+ S.V = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

p2,q2

p wait(S) p signal(S) q p q

k = S.V 0 ≥ #signal(S)= signal(S) #wait(S)= wait(S) S.V 0 ≥ S.V = k + #signal(S) #wait(S) −

S.V 0 ≥ wait(S) S.V S.V > 0 signal(S) S.V S.V = k + #signal(S) #wait(S) − CoSc wait 450:(S) Programming Paradigms S.V06 signal(S) S.V Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; #CS ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) #CS+ S.V = 1 p2: signal(S)p q2: signal(S)q loop forever loop forever p1: wait(S) q1: wait(S) p2: signal(S) q2: signal(S) #CS = #wait(S) #signal(S) − k = 1 # $ #CS = 1 S.V M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 − ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 # $ ≠ #CS+ S.V = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

CoSc 450: Programming Paradigms 06 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p2: signal(S)p q2: signal(S)q #CS = 1 S.V loop forever loop forever − p1: S.Vwait(S)0 q1: wait(S) ⇒# p2: signal(S)≥ % q2: signal(S) #CS 1 ≤

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

CoSc 450: Programming Paradigms 06 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p2: signal(S)p q2: signal(S)q #CS = 1 S.V loop forever loop forever − p1: S.Vwait(S)0 q1: wait(S) ⇒# p2: signal(S)≥ % q2: signal(S) #CS 1 ≤

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

CoSc 450: Programming Paradigms 06 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p2: signal(S)p q2: signal(S)q #CS = 1 S.V loop forever loop forever − p1: S.Vwait(S)0 q1: wait(S) ⇒# p2: signal(S)≥ % q2: signal(S) #CS 1 ≤

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

CoSc 450: Programming Paradigms 06 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p2: signal(S)p q2: signal(S)q #CS = 1 S.V loop forever loop forever − p1: S.Vwait(S)0 q1: wait(S) ⇒# p2: signal(S)≥ % q2: signal(S) #CS 1 ≤

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − CoSc 450: ProgrammingS.V 0 Paradigms 06 ⇒# ≥ % #CS 1 ≤ Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) p1: wait(S) binary semaphore S (1,q1:) wait(S) p2:¿ signal(S); q2: signal(S) p2: signal(S)p q2: signal(S)q loop forever loop forever ⇒# % p1,q1p1:wait(S) S.V = 0 q1: wait(S) p2: signal(S) q2: signal(S) #CS+ S.V = 1 ⇒# % #CS = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p ⇒# % S =(0, p ) S =(0, p,q { } ∨ { } #CS = 1 S.V = 1 0 = 1 ⇒# − − % #CS = 1

#CS = 1 S.V − CoSc 450: ProgrammingS.V 0 Paradigms 06 ⇒# ≥ % #CS 1 ≤ Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) p1: wait(S) binary semaphore S (1,q1:) wait(S) p2:¿ signal(S); q2: signal(S) p2: signal(S)p q2: signal(S)q loop forever loop forever ⇒# % p1,q1p1:wait(S) S.V = 0 q1: wait(S) p2: signal(S) q2: signal(S) #CS+ S.V = 1 ⇒# % #CS = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p ⇒# % S =(0, p ) S =(0, p,q { } ∨ { } #CS = 1 S.V = 1 0 = 1 ⇒# − − % #CS = 1

#CS = 1 S.V − CoSc 450: ProgrammingS.V 0 Paradigms 06 ⇒# ≥ % #CS 1 ≤ Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) p1: wait(S) binary semaphore S (1,q1:) wait(S) p2:¿ signal(S); q2: signal(S) p2: signal(S)p q2: signal(S)q loop forever loop forever ⇒# % p1,q1p1:wait(S) S.V = 0 q1: wait(S) p2: signal(S) q2: signal(S) #CS+ S.V = 1 ⇒# % #CS = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p ⇒# % S =(0, p ) S =(0, p,q { } ∨ { } #CS = 1 S.V = 1 0 = 1 ⇒# − − % #CS = 1

#CS = 1 S.V − CoSc 450: ProgrammingS.V 0 Paradigms 06 ⇒# ≥ % #CS 1 ≤ Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) p1: wait(S) binary semaphore S (1,q1:) wait(S) p2:¿ signal(S); q2: signal(S) p2: signal(S)p q2: signal(S)q loop forever loop forever ⇒# % p1,q1p1:wait(S) S.V = 0 q1: wait(S) p2: signal(S) q2: signal(S) #CS+ S.V = 1 ⇒# % #CS = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p ⇒# % S =(0, p ) S =(0, p,q { } ∨ { } #CS = 1 S.V = 1 0 = 1 ⇒# − − % #CS = 1

#CS = 1 S.V − CoSc 450: ProgrammingS.V 0 Paradigms 06 ⇒# ≥ % #CS 1 ≤ Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) p1: wait(S) binary semaphore S (1,q1:) wait(S) p2:¿ signal(S); q2: signal(S) p2: signal(S)p q2: signal(S)q loop forever loop forever ⇒# % p1,q1p1:wait(S) S.V = 0 q1: wait(S) p2: signal(S) q2: signal(S) #CS+ S.V = 1 ⇒# % #CS = 1

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠ p ⇒# % S =(0, p ) S =(0, p,q { } ∨ { } #CS = 1 S.V = 1 0 = 1 ⇒# − − % #CS = 1

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

#CS = 1 S.V − S.V 0 ⇒# ≥ % #CS 1 ≤ ⇒# % CoSc 450:p1,q 1Programming ParadigmsS.V = 0 06 #CS+ S.V = 1 ⇒# % #CS = 1 Algorithm 6.2 Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) Algorithm 6.2: Critical section with semaphores (twobinary proc., semaphore abbrev.) S (1, ) binary semaphore S (1, ) ¿ ; ¿ ; p q p loop forever q loop forever Algorithm 6.2: Critical section with semaphores (two proc., abbrev.) loop forever p1: wait(S)loop forever q1: wait(S) binary semaphore S (1, ) p p1: wait(S) p2:¿ signal(S)q1:; wait(S) q2: signal(S) p q p p2: signal(S) q2: signal(S) ⇒# % loop forever loop forever S =(0, p ) S =(0, p,q { } ∨ p1: { wait(S)} q1: wait(S) #CS =p2:1 Ssignal(S).V = 1 0 = 1 q2: signal(S) ⇒# − − % #CS = 1 ⇒# % q S =(0, p ) { } M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 q signal(S) ≠ ⇒# M. Ben-Ari.% Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 p ≠

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.3 ≠

CoSc 450: Programming Paradigms 06

The CS problem with more than two processes

Algorithm 6.3: Critical section with semaphores (N proc.) binary semaphore S (1, ) ¿ ; loop forever p1: non-critical section p2: wait(S) p3: critical section p4: signal(S)

Mutual exclusion: yes free: yes Starvation free: Only if the semaphore is strong.

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.5 ≠ CoSc 450: Programming Paradigms 06

The CS problem with more than two processes

Algorithm 6.3: Critical section with semaphores (N proc.) binary semaphore S (1, ) ¿ ; loop forever p1: non-critical section p2: wait(S) p3: critical section p4: signal(S)

Mutual exclusion: yes Deadlock free: yes Starvation free: Only if the semaphore is strong.

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.5 ≠ CoSc 450: Programming Paradigms 06

The CS problem with more than two processes

Algorithm 6.3: Critical section with semaphores (N proc.) binary semaphore S (1, ) ¿ ; loop forever p1: non-critical section p2: wait(S) p3: critical section p4: signal(S)

Mutual exclusion: yes Deadlock free: yes Starvation free: Only if the semaphore is strong.

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.5 ≠ CoSc 450: Programming Paradigms 06

The CS problem with more than two processes

Algorithm 6.3: Critical section with semaphores (N proc.) binary semaphore S (1, ) ¿ ; loop forever p1: non-critical section p2: wait(S) p3: critical section p4: signal(S)

Mutual exclusion: yes Deadlock free: yes Starvation free: Only if the semaphore is strong.

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.5 ≠ Algorithm 6.4: Critical section with semaphores (N proc., abbrev.) binary semaphore S (1, ) ¿ ; loop forever p1: wait(S) p2: signal(S)

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.6 ≠ Scenario for Starvation

n Process p Process q Process r S 1 p1: wait(S) q1: wait(S) r1: wait(S) (1, ) ; 2 p2: signal(S) q1: wait(S) r1: wait(S) (0, ) ; 3 p2: signal(S) q1: blocked r1: wait(S) (0, q ) { } 4 p1: signal(S) q1: blocked r1: blocked (0, q, r ) { } 5 p1: wait(S) q1: blocked r2: signal(S) (0, q ) { } 6 p1: blocked q1: blocked r2: signal(S) (0, p, q ) { } 7 p2: signal(S) q1: blocked r1: wait(S) (0, q ) { }

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.7 ≠ CoSc 450: Programming Paradigms 06

Concurrent merge sort Sort the first half and the second half concurrently.

Constrain the scheduling so that the merge operation can start only after the two sorts have completed.

This is a prerequisite scheduling problem. Algorithm 6.5: Mergesort integer array A binary semaphore S1 (0, ) ¿ ; binary semaphore S2 (0, ) ¿ ; sort1 sort2 merge p1: sort 1st half of A q1: sort 2nd half of A r1: wait(S1) p2: signal(S1) q2: signal(S2) r2: wait(S2) p3: q3: r3: merge halves of A

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.8 ≠ CoSc 450: Programming Paradigms 06

The producer consumer problem Assumptions • Operation append(d, buffer) appends data d • Operation take(buffer) deletes an item and returns it • The capacity of buffer is infinite • Must not delete from an empty buffer CoSc 450: Programming Paradigms 06

The producer consumer problem Assumptions • Operation append(d, buffer) appends data d • Operation take(buffer) deletes an item and returns it • The capacity of buffer is infinite • Must not delete from an empty buffer CoSc 450: Programming Paradigms 06

The producer consumer problem Assumptions • Operation append(d, buffer) appends data d • Operation take(buffer) deletes an item and returns it • The capacity of buffer is infinite • Must not delete from an empty buffer CoSc 450: Programming Paradigms 06

The producer consumer problem Assumptions • Operation append(d, buffer) appends data d • Operation take(buffer) deletes an item and returns it • The capacity of buffer is infinite • Must not delete from an empty buffer CoSc 450: Programming Paradigms 06

The producer consumer problem Assumptions • Operation append(d, buffer) appends data d • Operation take(buffer) deletes an item and returns it • The capacity of buffer is infinite • Must not delete from an empty buffer Algorithm 6.6: Producer-consumer (infinite bufier) infinite queue of dataType bufier empty queue ¿ semaphore notEmpty (0, ) ¿ ; producer consumer dataType d dataType d loop forever loop forever p1: d produce q1: wait(notEmpty) ¿ p2: append(d, bufier) q2: d take(bufier) ¿ p3: signal(notEmpty) q3: consume(d)

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.9 ≠ CoSc 450: Programming Paradigms 06

Class exercise Construct the beginning of the state transition diagram for p, p, q, q, ... and q, p, p, ... for the abbreviated algorithm.

Algorithm 6.7: Producer-consumer (infinite bufier, abbreviated) infinite queue of dataType bufier empty queue ¿ semaphore notEmpty (0, ) ¿ ; producer consumer dataType d dataType d loop forever loop forever p1: append(d, bufier) q1: wait(notEmpty) p2: signal(notEmpty) q2: d take(bufier) ¿

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.11 ≠

p1,q1 (0, /0 ) p1,q1 p1,q1 p2,q1 (0, /0 ) (0, /0 ) (0, /0 ) p2,q1 p ,q p1,q1 p1,1q11 p2,q1 (0, /0 ) (0, /0 ) (1(,0/0,)/0 ) (0, /0 ) p2,q1 pp11,q,q11 p p1p,2q,2q1 p p1,q1 (0, /0 ) ((10, ,/0 )/0 ) (0(,0/0,)/0 ) (1, /0 ) p1,q2 p1,,q1 p2,q1q p1p,1q,2q!1 qp1 q2 q (0, /0 ) (0(,1,q/0 )) ((01,,/0 /0 )) (0, /0 ) { } p1,q2! p p2p,1q,2q!2 p pp11,,qq22! p1,q1 (0, q ) (0(,0,q/0 )) ((00,, /0 q)) (1{, /0 }) { } { } p2,q2! p1,q2! pp21,,qq22!! (0p,1,qq2) (0, q ) ((00,, qq )) { } { } (0, /0 ) { } { } p2,q2! p2,q2! p1,q2! (0, q ) (0, q ) { } { } (0, q ) { } p2,q2! (0, q ) { }

p1,q1 (0, /0 )

p2,q1 (0, /0 )

p1,q1 (1, /0 )

p1,q2 (0, /0 )

p1CoSc,q2! 450: Programming Paradigms 06 (0, q ) { }

p2,q2! The producer-consumer problem (0, q ) Invariant{ } p1,q1 nonEmpty.V = #buffer signal(notEmpty) nonEmpty.V wait(notEmpty) nonEmpty.V

p1,q1 (0, /0 )

p2,q1 (0, /0 )

p1,q1 (1, /0 )

p1,q2 (0, /0 )

p1CoSc,q2! 450: Programming Paradigms 06 (0, q ) { }

p2,q2! The producer-consumer problem (0, q ) Invariant{ } p1,q1 nonEmpty.V = #buffer signal(notEmpty) nonEmpty.V wait(notEmpty) nonEmpty.V

p1,q1 (0, /0 )

p2,q1 (0, /0 )

p1,q1 (1, /0 )

p1,q2 (0, /0 )

p1CoSc,q2! 450: Programming Paradigms 06 (0, q ) { }

p2,q2! The producer-consumer problem (0, q ) Invariant{ } p1,q1 nonEmpty.V = #buffer signal(notEmpty) nonEmpty.V wait(notEmpty) nonEmpty.V

p1,q1 (0, /0 )

p2,q1 (0, /0 )

p1,q1 (1, /0 )

p1,q2 (0, /0 )

p1CoSc,q2! 450: Programming Paradigms 06 (0, q ) { }

p2,q2! The producer-consumer problem (0, q ) Invariant{ } p1,q1 nonEmpty.V = #buffer signal(notEmpty) nonEmpty.V wait(notEmpty) nonEmpty.V

CoSc 450: Programming Paradigms 06

The bounded buffer producer-consumer problem Assumptions • The capacity of buffer is finite • Must not delete from an empty buffer • Must not insert into a full buffer

Implementation • A circular queue (Exercise for the student) CoSc 450: Programming Paradigms 06

The bounded buffer producer-consumer problem Assumptions • The capacity of buffer is finite • Must not delete from an empty buffer • Must not insert into a full buffer

Implementation • A circular queue (Exercise for the student) CoSc 450: Programming Paradigms 06

The bounded buffer producer-consumer problem Assumptions • The capacity of buffer is finite • Must not delete from an empty buffer • Must not insert into a full buffer

Implementation • A circular queue (Exercise for the student) CoSc 450: Programming Paradigms 06

The bounded buffer producer-consumer problem Assumptions • The capacity of buffer is finite • Must not delete from an empty buffer • Must not insert into a full buffer

Implementation • A circular queue (Exercise for the student) Circular Bufier

Data Data

®®®®®®®®®®®® ®®®® ®®®®®®®®®®®® ®®®® 6 6 in out

®®®®®®®®®®®® ®®®®®®®®®®®® 6 6 inout

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.41 ≠ Algorithm 6.19: Producer-consumer (circular bufier) dataType array [0..N] bufier integer in, out 0 ¿ semaphore notEmpty (0, ) ¿ ; semaphore notFull (N, ) ¿ ; producer consumer dataType d dataType d loop forever loop forever p1: d produce q1: wait(notEmpty) ¿ p2: wait(notFull) q2: d bufier[out] ¿ p3: bufier[in] d q3: out (out+1) modulo N ¿ ¿ p4: in (in+1) modulo N q4: signal(notFull) ¿ p5: signal(notEmpty) q5: consume(d)

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.42 ≠ CoSc 450: Programming Paradigms 06

The dining philosophers CoSc 450: Programming Paradigms 06

Activity of each philosopher

• Pick up forks • Eat • Put down forks • Think Algorithm 6.9: Dining philosophers (outline)

loop forever p1: think p2: preprotocol p3: eat p4: postprotocol

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.14 ≠ CoSc 450: Programming Paradigms 06

Dining philosophers implementation

• Each philosopher is a process • Each fork is a semaphore CoSc 450: Programming Paradigms 06

Array of forks and philosophers

ph[2] f[3] f[2]

ph[3] ph[1]

f[4] f[1]

ph[4] ph[0] f[0] Algorithm 6.10: Dining philosophers (first attempt) semaphore array [0..4] fork [1,1,1,1,1] ¿ loop forever p1: think p2: wait(fork[i]) p3: wait(fork[i+1]) p4: eat p5: signal(fork[i]) p6: signal(fork[i+1])

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠ p1,q1 (0, /0 ) p2,q1 (0, /0 ) p1,q1 (1, /0 ) p1,q2 (0, /0 ) p1,q2! (0, q ) { } p2,q2! (0, q ) { } p1,q1 nonEmpty.V = #buffer signal(notEmpty) CoSc 450: ProgrammingnonEmpty Paradigms.V 06 wait(notEmpty) AlgorithmnonEmpty 6.10.V Algorithm 6.10: Dining philosophersAlgorithm (first attempt) 6.10: Dining philosophers (first attempt) semaphore array [0..4] fork [1,1,1,1,1]semaphore array [0..4] fork [1,1,1,1,1] ¿ ¿ loop forever loop forever p1: think p1: think p2: wait(fork[i]) p2: wait(fork[i]) p3: wait(fork[i+1]) p3: wait(fork[i+1]) i p4: eat p4: eat p5: signal(fork[i]) #Pi = #wait(fork[ip5:]) #signalsignal(fork[i])(fork[i]) − p6: signal(fork[i+1]) #waitp6: signal(fork[i+1])#signal = 1 S.V # − − $ #Pi = 1 S.V − S.V 0 ⇒# ≥ $ #Pi 1 ≤

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠

p1,q1 (0, /0 ) p2,q1 (0, /0 ) p1,q1 (1, /0 ) p1,q2 (0, /0 ) p1,q2! (0, q ) { } p2,q2! (0, q ) { } p1,q1 nonEmpty.V = #buffer signal(notEmpty) CoSc 450: ProgrammingnonEmpty Paradigms.V 06 wait(notEmpty) AlgorithmnonEmpty 6.10.V Algorithm 6.10: Dining philosophersAlgorithm (first attempt) 6.10: Dining philosophers (first attempt) semaphore array [0..4] fork [1,1,1,1,1]semaphore array [0..4] fork [1,1,1,1,1] ¿ ¿ loop forever loop forever p1: think p1: think p2: wait(fork[i]) p2: wait(fork[i]) p3: wait(fork[i+1]) p3: wait(fork[i+1]) i p4: eat p4: eat p5: signal(fork[i]) #Pi = #wait(fork[ip5:]) #signalsignal(fork[i])(fork[i]) − p6: signal(fork[i+1]) #waitp6: signal(fork[i+1])#signal = 1 S.V # − − $ #Pi = 1 S.V − S.V 0 ⇒# ≥ $ #Pi 1 ≤

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠

p1,q1 (0, /0 ) p2,q1 (0, /0 ) p1,q1 (1, /0 ) p1,q2 (0, /0 ) p1,q2! (0, q ) { } p2,q2! (0, q ) { } p1,q1 nonEmpty.V = #buffer signal(notEmpty) CoSc 450: ProgrammingnonEmpty Paradigms.V 06 wait(notEmpty) AlgorithmnonEmpty 6.10.V Algorithm 6.10: Dining philosophersAlgorithm (first attempt) 6.10: Dining philosophers (first attempt) semaphore array [0..4] fork [1,1,1,1,1]semaphore array [0..4] fork [1,1,1,1,1] ¿ ¿ loop forever loop forever p1: think p1: think p2: wait(fork[i]) p2: wait(fork[i]) p3: wait(fork[i+1]) p3: wait(fork[i+1]) i p4: eat p4: eat p5: signal(fork[i]) #Pi = #wait(fork[ip5:]) #signalsignal(fork[i])(fork[i]) − p6: signal(fork[i+1]) #waitp6: signal(fork[i+1])#signal = 1 S.V # − − $ #Pi = 1 S.V − S.V 0 ⇒# ≥ $ #Pi 1 ≤

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠

p1,q1 (0, /0 ) p2,q1 (0, /0 ) p1,q1 (1, /0 ) p1,q2 (0, /0 ) p1,q2! (0, q ) { } p2,q2! (0, q ) { } p1,q1 nonEmpty.V = #buffer signal(notEmpty) CoSc 450: ProgrammingnonEmpty Paradigms.V 06 wait(notEmpty) AlgorithmnonEmpty 6.10.V Algorithm 6.10: Dining philosophersAlgorithm (first attempt) 6.10: Dining philosophers (first attempt) semaphore array [0..4] fork [1,1,1,1,1]semaphore array [0..4] fork [1,1,1,1,1] ¿ ¿ loop forever loop forever p1: think p1: think p2: wait(fork[i]) p2: wait(fork[i]) p3: wait(fork[i+1]) p3: wait(fork[i+1]) i p4: eat p4: eat p5: signal(fork[i]) #Pi = #wait(fork[ip5:]) #signalsignal(fork[i])(fork[i]) − p6: signal(fork[i+1]) #waitp6: signal(fork[i+1])#signal = 1 S.V # − − $ #Pi = 1 S.V − S.V 0 ⇒# ≥ $ #Pi 1 ≤

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠ M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠

CoSc 450: Programming Paradigms 06

Algorithm 6.10 Algorithm 6.10: Dining philosophersAlgorithm (first attempt) 6.10: Dining philosophers (first attempt) semaphore array [0..4] fork [1,1,1,1,1]semaphore array [0..4] fork [1,1,1,1,1] ¿ ¿ loop forever loop forever p1: think p1: think p2: wait(fork[i]) P0 p2: wait(fork[i]) p3: wait(fork[i+1]) P1 p3: wait(fork[i+1]) p4: eat p4: eat P2 p5: signal(fork[i]) p5: signal(fork[i]) P3 p6: signal(fork[i+1]) p6: signal(fork[i+1]) P4 waitC(cond) p cond M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠ p.state M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ← ≠ monitor.lock ← signalC(cond) cond = /0 " cond q q.state ← empty(cond) cond

wait(S) waitC(cond) signal(S) signalC(cond) cond signal(S) signalC(cond) waitC(cond) p cond p.state ← monitor.lock ← signalC(cond) cond = /0 " cond q q.state ←

CoSc 450: Programming Paradigms 06

Algorithm 6.10 Algorithm 6.10: Dining philosophersAlgorithm (first attempt) 6.10: Dining philosophers (first attempt) semaphore array [0..4] fork [1,1,1,1,1]semaphore array [0..4] fork [1,1,1,1,1] ¿ ¿ loop forever loop forever p1: think p1: think p2: wait(fork[i]) P0 p2: wait(fork[i]) p3: wait(fork[i+1]) P1 p3: wait(fork[i+1]) p4: eat p4: eat P2 p5: signal(fork[i]) p5: signal(fork[i]) P3 p6: signal(fork[i+1]) p6: signal(fork[i+1]) P4 waitC(cond) p cond M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ≠ p.state M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.16 ← ≠ monitor.lock ← signalC(cond) cond = /0 " cond q q.state ← empty(cond) cond

wait(S) waitC(cond) signal(S) signalC(cond) cond signal(S) signalC(cond) waitC(cond) p cond p.state ← monitor.lock ← signalC(cond) cond = /0 " cond q q.state ←

CoSc 450: Programming Paradigms 06

Algorithm 6.11 Solves the deadlock problem by simulating a room with a room semaphore that only allows four philosophers in the room at the same time.

To be starvation free, the room semaphore must be strong, but the fork semaphores can be weak. Algorithm 6.11: Dining philosophers (second attempt) semaphore array [0..4] fork [1,1,1,1,1] ¿ semaphore room 4 ¿ loop forever p1: think p2: wait(room) p3: wait(fork[i]) p4: wait(fork[i+1]) p5: eat p6: signal(fork[i]) p7: signal(fork[i+1]) p8: signal(room)

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.17 ≠ CoSc 450: Programming Paradigms 06

Algorithm 6.12 Solves the deadlock problem by having the fourth philosopher pick up his right fork first, and then his left fork. If he blocks on picking up his right fork, his left fork will be available for philosopher 3.

This is an asymmetric solution. One philosopher acts differently from the others. Algorithm 6.12: Dining philosophers (third attempt) semaphore array [0..4] fork [1,1,1,1,1] ¿ philosopher 4 loop forever p1: think p2: wait(fork[0]) p3: wait(fork[4]) p4: eat p5: signal(fork[0]) p6: signal(fork[4])

M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition c M. Ben-Ari 2006 Slide 6.18 ≠