Title: Chapter 4 Semaphores
1Chapter 4 Semaphores
- Syntax and Semantics
- Basic Problems and Techniques
- The Dinning Philosophers
- Readers and Writers
- Resource Allocation and Scheduling
- Pthreads
2Syntax and Semantics
- A Semaphore is a special kind of shared variable
that is manipulated only by two atomic
operations, P and V. - The value of a semaphore is a nonnegative
integer. - P(s) ltawait (sgt0) ss-1gt
- V(s) ltss1gt
- A general semaphore is one that can take on any
nonnegative value. - A binary semaphore is one whose value is always
either 0 or 1. -
3Critical Sections Mutual Exclusion
4Barriers Signaling Events
5Producers and Consumers Split Binary Semaphores
- Empty and full are together called a split binary
semaphore because at most one of these two can be
1 at a time.
6Bounded Buffers Resource Counting
- Put items in the rear and fetch items in the
front of the array. - Bufrear data rear(rear1) n
- Results buffront front(front1)n
7Single Producer / Consumer
- How about if there are two or more producers and
consumers?
8Multiple Producers / Consumers
- Enforce mutual exclusion!!
9The Dining Philosophers
- Five philosophers thinking or eating. Has to get
two forks to eat. Interesting but not hygiene.
10The Dining Philosophers (cntd)
- Process Philosophers i0 to 4
- while(true)
- think
- acquire forks
- eat
- release forks
-
-
- Have to avoid deadlocks. A necessary condition
there is a circular waiting.
11The Dining Philosophers (cntd)
12The Readers / Writers Problem
- Two kinds of processes readers and writers
share a database. - Readers wait until no writers are inside.
- Writers wait until no readers or other writers
are accessing. - Mutual exclusion solution
- Condition synchronization solution
- Passing the baton.
13An Exclusion Problem
14An Exclusion Problem (cntd)
15Exclusion Using Semaphores
- Readers preference. If some readers are
accessing the DB, both another reader and a
writer arrive, the new reader get preference over
the writer.
16Using Condition Synchronization
17Passing the Baton
- A process within the CS holding a baton passes it
to another process.
18Passing the Baton (cntd)
19Passing the Baton (cntd)
- Simplifying the SIGNAL statement
20Alternative Scheduling Policies
- Last slide still gives readers preference.
- To give writers preference
- New readers are delayed if a writer is waiting
and, - Changing the readers first statement
- If (nwgt0 or dwgt0) drdr1 V(e) P(r)
- A delayed reader is awakened only if no writer is
waiting. - Switching the order of the first two arms of the
if statement in writers - If (dw gt 0) dwdw-1 V(w)
- Else if (drgt0) drdr-1 V(r)
- Else V(e)
21Fair Access
- Delay a new reader when a writer is waiting
- Delay a new writer when a reader is waiting
- Awaken one waiting writer (if any) when a reader
finishes - Awaken all waiting readers (if any) when a writer
finishes other wise awaken one waiting writer
(if any).
22Resource Allocation and Scheduling
- Multiple processes are competing some shared
resource. Decide who acquires the resource. - Request (parameters)
- ltawait (request can be satisified)
- take units
- Release (parameters)
- ltreturn unitsgt
23Using the Passing-the-Baton
- Request (parameters)
- P(e)
- if (request cannot be satisfied) DELAY
- take units
- SIGNAL
- Release(parameters)
- P(e)
- return units
- SIGNAL
24Shortest-Job-Next Allocation
- Requesting by calling request (time, id), where
time is an integer that specifies how long the
process will use the resource and id is an
integer that identifies the requesting process. - A process can be delayed forever if there is a
continual stream of requests specifying shorter
usage times. - Small modification a process that has been
delayed a long time is given preference, called
aging technique.
25Implementation using Passing the Baton Technique
- Request (time, id)
- P(e)
- If(!free) DELAY
- Free false
- SIGNAL
- Release()
- P(e)
- Freetrue
- SIGNAL
26Using Semaphores
27Generalizing the Problem
- If we have more than one unit resources.
- Replace free by an integer avail that records the
number of available units. - In request(), test if amount lt avail,
- If so, allocate amount units.
- Otherwise, record how many units are required
before delaying - In release(), increase avail by amount.
- See page 184 for details.
28Pthread
- POSIX Portable Operating Systems Interface.
POSIX thread. - Having the Fork-Join pattern.
- To use it
- include ltpthread.hgt
- Initialize the attributes
- pthread_attr_init(tattr)
- Pthread_attr_setscope(tattr, PTHREAD_SCOPE_SYSTEM
) - Pthread_create()
- Pthread_exit()
- Pthread_join(tid, value_ptr)
29Semaphores
- Sem_t mutex
- Sem_init (mutex, SHARED, 1)
- Sem_wait (mutex) // P(mutex)
- Critical section
- Sem_post (mutex) // V(mutex)
30Producer and Consumer (one buffer) using Pthreads