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

Post on 03-Jan-2016

242 views 0 download

Tags:

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

Semaphores and Semaphores and Bounded BufferBounded Buffer

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.

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()

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

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();

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);

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

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

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

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

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

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 …… ……

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);

}} }}

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

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

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

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

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

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

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

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

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

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

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

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

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);

}} }}

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

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)

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;

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);

}}

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);

}}

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);

}}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}}

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;

}}

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