semaphores and bounded buffer€¦ · semaphores semaphore. is a type of generalized lock...

59
Semaphores and Bounded Buffer Sarah Diesburg Operating Systems CS 3430

Upload: others

Post on 22-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Semaphores and Bounded Buffer

Sarah Diesburg Operating Systems

CS 3430

Page 2: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Semaphores

Semaphore is a type of generalized lock –Consist of a nonnegative integer value –Two operations

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

V(): an atomic operation that increments semaphore 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 –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Origin of Semaphores

Defined by Dijkstra in the 60s Main synchronization primitives used

in UNIX The P operation is an abbreviation

for proberen (Dutch), meaning “to test”

The V operation stands for verhogen, meaning “to increment”

Page 4: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Semaphores vs. Integers

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

–Cannot read or write semaphore values (Except at the initialization times)

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

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

wakeup from V()

Page 5: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Binary Semaphores

A binary semaphore is initialized to 1

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

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

Page 6: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

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

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

Page 7: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

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

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

litter_box = 1

Page 8: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

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

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

litter_box = 1 0

Page 9: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

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

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

litter_box = 0

Page 10: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

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

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

litter_box = 0

Page 11: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

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

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

litter_box = 0 1

Page 12: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

Page 13: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0

Page 14: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0

Page 15: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0

wait

Page 16: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0

Page 17: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 1 wait_right = 0

Page 18: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 1 0 wait_right = 0

Page 19: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0

Page 20: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0

wait

Page 21: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0

Page 22: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0 1

Page 23: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 1 0

Page 24: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore wait_left = 0; semaphore wait_right = 0; Left_Paw() { Right_Paw() { slide_left(); P(wait_left); V(wait_left); slide_left(); P(wait_right); slide_right(); slide_right(); V(wait_right); } }

wait_left = 0 wait_right = 0

Page 25: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Two Uses of Semaphores

2. Scheduling –Semaphore usually has an initial value

of 0 semaphore s1 = 0; semaphore s2 = 0; A() { B() { write(x); P(s1); V(s1); read(x); P(s2); write(y); read(y); V(s2); } }

Page 26: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Producer-Consumer with a Bounded Buffer

A classic problem A producer put things into a shared

buffer A consumer takes them out

Page 27: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Problem Constraints

The solution involves both scheduling and mutual exclusion

Constraints –The consumer must wait if buffers are

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

full (scheduling constraint) –Only one thread can manipulate the

buffer at a time (mutual exclusion)

Page 28: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = N; semaphore nLoadedBuffers = 0;

Page 29: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = N; semaphore nLoadedBuffers = 0; Producer() { P(mutex); // put 1 item in the buffer V(mutex); }

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

Page 30: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = N; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); }

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

Page 31: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = N; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

Page 32: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 nFreeBuffers = 2 nLoadedBuffers = 0

Page 33: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 nFreeBuffers = 2 nLoadedBuffers = 0

Page 34: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 nFreeBuffers = 2 nLoadedBuffers = 0

Page 35: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 nFreeBuffers = 2 1 nLoadedBuffers = 0

Page 36: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 0 nFreeBuffers = 1 nLoadedBuffers = 0

Page 37: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 1 nLoadedBuffers = 0

Page 38: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 1 nLoadedBuffers = 0

Page 39: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 1 0 nLoadedBuffers = 0

Page 40: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 41: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 1 nFreeBuffers = 0 nLoadedBuffers = 0

Page 42: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 43: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 44: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0 1

Page 45: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 1 0

Page 46: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 47: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 1 nFreeBuffers = 0 nLoadedBuffers = 0

Page 48: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 49: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 50: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 51: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 52: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 1 nFreeBuffers = 0 nLoadedBuffers = 0

Page 53: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 nFreeBuffers = 0 nLoadedBuffers = 0

Page 54: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 nFreeBuffers = 0 nLoadedBuffers = 0

Page 55: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 nFreeBuffers = 0 1 nLoadedBuffers = 0

Page 56: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 nFreeBuffers = 1 0 nLoadedBuffers = 0

Page 57: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 1 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 58: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0

Page 59: Semaphores and Bounded Buffer€¦ · Semaphores Semaphore. is a type of generalized lock –Consist of a nonnegative integer value –Two operations P(): an atomic operation that

Developing the Solution

Each constraint needs a semaphore

semaphore mutex = 1; semaphore nFreeBuffers = 2; semaphore nLoadedBuffers = 0; Producer() { P(nFreeBuffers); P(mutex); // put 1 item in the buffer V(mutex); V(nLoadedBuffers); }

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

mutex = 0 nFreeBuffers = 0 nLoadedBuffers = 0