semaphores and bounded buffer andy wang operating systems cop 4610 / cgs 5765

13
Semaphores and Semaphores and Bounded Buffer Bounded Buffer Andy Wang Andy Wang Operating Systems Operating Systems COP 4610 / CGS 5765 COP 4610 / CGS 5765

Upload: melvyn-hardy

Post on 12-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Semaphores and Semaphores and Bounded BufferBounded Buffer

Andy WangAndy Wang

Operating SystemsOperating Systems

COP 4610 / CGS 5765COP 4610 / CGS 5765

Page 2: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

SemaphoresSemaphores

SemaphoreSemaphore is a type of generalized is a type of generalized locklock– Consist of a positive integer valueConsist of a positive integer value– Two operationsTwo operations

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

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

Page 3: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Origin of SemaphoresOrigin of Semaphores

Defined by Dijkstra in the last 60sDefined by Dijkstra in the last 60s Main synchronization primitives used Main synchronization primitives used

in UNIXin UNIX The P operation is an abbreviation for The P operation is an abbreviation for

proberenproberen (Dutch), meaning “to test” (Dutch), meaning “to test” The V operation stands for The V operation stands for

verhogenverhogen, meaning “to increment”, meaning “to increment”

Page 4: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

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 5: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

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() sets the value to 1V() sets the value to 1– Wakes up a thread waiting at P(), if anyWakes up a thread waiting at P(), if any

Page 6: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Two Uses of SemaphoresTwo Uses of Semaphores

Mutual exclusionMutual 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 s = 1;semaphore s = 1;

P(s);P(s);

// critical section// critical section

V(s);V(s);

Page 7: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Two Uses of SemaphoresTwo Uses of Semaphores

Scheduling Scheduling – 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 8: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

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 9: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Problem ConstraintsProblem Constraints

The solution involves both scheduling The solution involves both scheduling and mutual exclusionand mutual exclusion

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

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

(scheduling constraint)(scheduling 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 10: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Developing the SolutionDeveloping the Solution

Each constraint Each constraint needs a semaphoreneeds a semaphore

semaphore mutex = 1;semaphore mutex = 1;

semaphore nFreeBuffers = N;semaphore nFreeBuffers = N;

semaphore nLoadedBuffers = 0;semaphore nLoadedBuffers = 0;

Page 11: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Developing the SolutionDeveloping the Solution Each constraint Each constraint

needs a semaphoreneeds a semaphoresemaphore 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 bufferV(mutex);V(mutex);

}}

Consumer() {Consumer() {

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

}}

Page 12: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Developing the SolutionDeveloping the Solution Each constraint Each constraint

needs a semaphoreneeds a semaphoresemaphore 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 bufferV(mutex);V(mutex);

}}

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

}}

Page 13: Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765

Developing the SolutionDeveloping the Solution Each constraint Each constraint

needs a semaphoreneeds a semaphoresemaphore 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 bufferV(mutex);V(mutex);V(nLoadedBuffers);V(nLoadedBuffers);

}}

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

}}