Semaphores and Bounded Buffer. Semaphores Semaphore is a type of generalized lock –Defined by...

64
Semaphores and Semaphores and Bounded Buffer Bounded Buffer

Transcript of Semaphores and Bounded Buffer. Semaphores Semaphore is a type of generalized lock –Defined by...

Page 1: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Semaphores and Semaphores and Bounded BufferBounded Buffer

Page 2: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

SemaphoresSemaphores

SemaphoreSemaphore is a type of generalized lock is a type of generalized lock– Defined by Dijkstra in the last 60sDefined by Dijkstra in the last 60s– Main synchronization primitives used in UNIXMain synchronization primitives used in UNIX– Consist of a positive integer valueConsist of a positive integer value– Two operationsTwo operations

P()P(): an atomic operation that waits for semaphore to : an atomic operation that waits for semaphore to become positive, then decrement it by 1become positive, then decrement it by 1

V()V(): an atomic operation that increments semaphore : an atomic operation that increments semaphore by 1 and wakes up a waiting thread at P(), if any.by 1 and wakes up a waiting thread at P(), if any.

Page 3: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Semaphores vs. IntegersSemaphores vs. Integers

No negative valuesNo negative values Only operations are P() and V()Only operations are P() and V()

– Cannot read or write semaphore valuesCannot read or write semaphore values– Except at the initialization timesExcept at the initialization times

Operations are atomicOperations are atomic– Two P() calls cannot decrement the Two P() calls cannot decrement the

value below zerovalue below zero– A sleeping thread at P() cannot miss a A sleeping thread at P() cannot miss a

wakeup from V()wakeup from V()

Page 4: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Binary SemaphoresBinary Semaphores

A A binary semaphorebinary semaphore is initialized to is initialized to 11

P() waits until the value is 1P() waits until the value is 1– Then set it to 0Then set it to 0

V() V() setssets the value to 1 the value to 1– Wakes up a thread waiting at P(), if anyWakes up a thread waiting at P(), if any

Page 5: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

1. Mutual exclusion1. Mutual exclusion– Lock was designed to do thisLock was designed to do this

lock->acquire();lock->acquire();

// critical section// critical section

lock->release();lock->release();

Page 6: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

1.1. Mutual exclusionMutual exclusion1.1. The lock function can be realized with a The lock function can be realized with a

binary semaphore: semaphore subsumes binary semaphore: semaphore subsumes lock.lock. Semaphore has an initial value of 1Semaphore has an initial value of 1 P() is called before a critical sectionP() is called before a critical section V() is called after the critical sectionV() is called after the critical section

semaphore litter_box = 1;semaphore litter_box = 1;P(litter_box);P(litter_box);// critical section// critical sectionV(litter_box);V(litter_box);

Page 7: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

1. Mutual exclusion1. Mutual exclusion– Semaphore has an initial value of 1Semaphore has an initial value of 1– P() is called before a critical sectionP() is called before a critical section– V() is called after the critical sectionV() is called after the critical section

semaphore litter_box = 1;semaphore litter_box = 1;

P(litter_box);P(litter_box);

// critical section// critical section

V(litter_box);V(litter_box);

litter_box = 1

Page 8: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

1. Mutual exclusion1. Mutual exclusion– Semaphore has an initial value of 1Semaphore has an initial value of 1– P() is called before a critical sectionP() is called before a critical section– V() is called after the critical sectionV() is called after the critical section

semaphore litter_box = 1;semaphore litter_box = 1;

P(litter_box); // purrr…P(litter_box); // purrr…

// critical section// critical section

V(litter_box);V(litter_box);

litter_box = 1 0

Page 9: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

1. Mutual exclusion1. Mutual exclusion– Semaphore has an initial value of 1Semaphore has an initial value of 1– P() is called before a critical sectionP() is called before a critical section– V() is called after the critical sectionV() is called after the critical section

semaphore litter_box = 1;semaphore litter_box = 1;

P(litter_box); P(litter_box);

// critical section// critical section

V(litter_box);V(litter_box);

litter_box = 0

Page 10: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

1. Mutual exclusion1. Mutual exclusion– Semaphore has an initial value of 1Semaphore has an initial value of 1– P() is called before a critical sectionP() is called before a critical section– V() is called after the critical sectionV() is called after the critical section

semaphore litter_box = 1;semaphore litter_box = 1;

P(litter_box); // meow…P(litter_box); // meow…

// critical section// critical section

V(litter_box);V(litter_box);

litter_box = 0

Page 11: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

1. Mutual exclusion1. Mutual exclusion– Semaphore has an initial value of 1Semaphore has an initial value of 1– P() is called before a critical sectionP() is called before a critical section– V() is called after the critical sectionV() is called after the critical section

semaphore litter_box = 1;semaphore litter_box = 1;

P(litter_box);P(litter_box);

// critical section// critical section

V(litter_box);V(litter_box);

litter_box = 0 1

Page 12: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Synchronization: Enforcing some order 2. Synchronization: Enforcing some order between threadsbetween threads

T1 T2T1 T2 do X do X wait for Xwait for X do Ydo Y wait for Y wait for Y do Z do Z wait for Zwait for Z …… ……

Page 13: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Synchronization 2. Synchronization – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

Page 14: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 0wait_right = 0

Page 15: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 0wait_right = 0

Page 16: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left);P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 0wait_right = 0

wait

Page 17: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left);P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 0wait_right = 0

Page 18: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left);P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 0 1wait_right = 0

Page 19: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left);P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 1 0wait_right = 0

Page 20: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left();slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 0wait_right = 0

Page 21: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left();slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 0wait_right = 0

wait

Page 22: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right();slide_right();slide_right();slide_right(); V(wait_right); V(wait_right);

}} }}

wait_left = 0wait_right = 0

Page 23: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right);V(wait_right);

}} }}

wait_left = 0wait_right = 0 1

Page 24: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right);V(wait_right);

}} }}

wait_left = 0wait_right = 1 0

Page 25: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Scheduling 2. Scheduling – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore wait_left = 0;semaphore wait_left = 0;semaphore wait_right = 0;semaphore wait_right = 0;

Left_Paw() {Left_Paw() { Right_Paw() {Right_Paw() {slide_left();slide_left(); P(wait_left); P(wait_left);V(wait_left);V(wait_left); slide_left(); slide_left();P(wait_right);P(wait_right); slide_right(); slide_right();slide_right();slide_right(); V(wait_right);V(wait_right);

}} }}

wait_left = 0wait_right = 0

Page 26: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Two Uses of SemaphoresTwo Uses of Semaphores

2. Synchronization 2. Synchronization – Semaphore usually has an initial value Semaphore usually has an initial value

of 0of 0

semaphore s1 = 0;semaphore s1 = 0;semaphore s2 = 0;semaphore s2 = 0;

A() {A() { B() {B() {write(x);write(x); P(s1); P(s1);V(s1);V(s1); read(x); read(x);P(s2);P(s2); write(y); write(y);read(y);read(y); V(s2); V(s2);

}} }}

Page 27: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Producer-Consumer with a Producer-Consumer with a Bounded BufferBounded Buffer

A classic problemA classic problem A producer put things into a shared A producer put things into a shared

bufferbuffer A consumer takes them outA consumer takes them out

Page 28: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Problem ConstraintsProblem Constraints

The solution involves both The solution involves both synchronization and mutual exclusionsynchronization and mutual exclusion

ConstraintsConstraints– The consumer must wait if buffers are The consumer must wait if buffers are

empty (synchronization constraint)empty (synchronization constraint)– The producer must wait if buffers are full The producer must wait if buffers are full

(synchronization constraint)(synchronization constraint)– Only one thread can manipulate the Only one thread can manipulate the

buffer at a time (mutual exclusion)buffer at a time (mutual exclusion)

Page 29: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = N;semaphore nFreeBuffers = N;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Page 30: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = N;semaphore nFreeBuffers = N;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

}}

Consumer() {Consumer() {

P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);

}}

Page 31: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = N;semaphore nFreeBuffers = N;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);

}}

Page 32: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = N;semaphore nFreeBuffers = N;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

Page 33: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1nFreeBuffers = 2nLoadedBuffers = 0

Page 34: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1nFreeBuffers = 2nLoadedBuffers = 0

Page 35: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1nFreeBuffers = 2nLoadedBuffers = 0

Page 36: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1nFreeBuffers = 2 1nLoadedBuffers = 0

Page 37: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1 0nFreeBuffers = 1nLoadedBuffers = 0

Page 38: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 1nLoadedBuffers = 0

Page 39: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 1nLoadedBuffers = 0

Page 40: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 1 0nLoadedBuffers = 0

Page 41: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0

Page 42: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0 1nFreeBuffers = 0nLoadedBuffers = 0

Page 43: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1 0nFreeBuffers = 0nLoadedBuffers = 0

Page 44: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0

Page 45: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0 1

Page 46: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 1 0

Page 47: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0

Page 48: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0 1nFreeBuffers = 0nLoadedBuffers = 0

Page 49: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1 0nFreeBuffers = 0nLoadedBuffers = 0

Page 50: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0

Page 51: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0

Page 52: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0

Page 53: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0 1nFreeBuffers = 0nLoadedBuffers = 0

Page 54: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1nFreeBuffers = 0nLoadedBuffers = 0

Page 55: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1nFreeBuffers = 0nLoadedBuffers = 0

Page 56: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1nFreeBuffers = 0 1nLoadedBuffers = 0

Page 57: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1nFreeBuffers = 1 0nLoadedBuffers = 0

Page 58: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 1 0nFreeBuffers = 0nLoadedBuffers = 0

Page 59: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0

Page 60: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a needs a semaphoresemaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = 2;semaphore nFreeBuffers = 2;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Producer() {Producer() {

P(nFreeBuffers);P(nFreeBuffers);

P(mutex);P(mutex);

// put 1 item in the buffer// put 1 item in the buffer

V(mutex);V(mutex);

V(nLoadedBuffers);V(nLoadedBuffers);

}}

Consumer() {Consumer() {P(nLoadedBuffers);P(nLoadedBuffers);P(mutex);P(mutex);// take 1 item from the // take 1 item from the // buffer// bufferV(mutex);V(mutex);V(nFreeBuffers);V(nFreeBuffers);

}}

mutex = 0nFreeBuffers = 0nLoadedBuffers = 0

Page 61: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Implementing SemaphoreImplementing Semaphore

How to implement semaphore?How to implement semaphore?– Almost exactly like lock.Almost exactly like lock.– Using spinlock or queueUsing spinlock or queue

What hardware support is needed?What hardware support is needed?– Interrupt disableInterrupt disable– Test-and-setTest-and-set

Page 62: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Implementing SemaphoreImplementing Semaphoreclass semaphore {class semaphore {

int value;int value;}}

semaphore::semaphore(int i) {semaphore::semaphore(int i) { value = i;value = i;}}semaphore::p() {semaphore::p() {

// disable interrupts// disable interruptswhile (value == 0) {while (value == 0) {

// enable interrupts// enable interrupts// disable interrupts// disable interrupts

}}value --;value --;// enable interrupts// enable interrupts

}}

semaphore::v() {semaphore::v() {

// disable interrupts// disable interrupts

value ++;value ++;

// enable interrupts// enable interrupts

}}

Page 63: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Implementing Semaphore with Implementing Semaphore with test and settest and set

class semaphore {class semaphore {int value;int value;

}}

semaphore::semaphore(int i) {semaphore::semaphore(int i) { value = i;value = i;}}semaphore::p() {semaphore::p() {

while (test_and_set(guard));while (test_and_set(guard));while (value == 0) {while (value == 0) {

// queue the thread// queue the thread// guard = 0 and // guard = 0 and

sleepsleep}}value --;value --;guard = 0;guard = 0;

}}

semaphore::v() {semaphore::v() {

while (test_and_set(guard));while (test_and_set(guard));

if (anyone waiting) {if (anyone waiting) {

// wake up one thread// wake up one thread

// put in on ready queue// put in on ready queue

} else {} else {

value ++;value ++;

}}

guard = 0;guard = 0;

}}

Page 64: Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.

Semaphore in UNIXSemaphore in UNIX

Managing concurrent access to shared memory.Managing concurrent access to shared memory.

Semaphore system callsSemaphore system calls

– Creation: Creation: semgetsemget( … )( … )

– Incr/Decr/set : Incr/Decr/set : semopsemop(…)(…)

– Deletion: Deletion: semctlsemctl(semid, 0, IPC_RMID, 0);(semid, 0, IPC_RMID, 0);

See examples: seminit.c, sema.c semb.c See examples: seminit.c, sema.c semb.c