Chapter 4 Semaphores - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Chapter 4 Semaphores

Description:

A delayed reader is awakened only if no writer is waiting. ... Using Semaphores. 27. Generalizing the Problem. If we have more than one unit resources. ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 31
Provided by: lpe7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4 Semaphores


1
Chapter 4 Semaphores
  • Syntax and Semantics
  • Basic Problems and Techniques
  • The Dinning Philosophers
  • Readers and Writers
  • Resource Allocation and Scheduling
  • Pthreads

2
Syntax 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.

3
Critical Sections Mutual Exclusion
4
Barriers Signaling Events
5
Producers 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.

6
Bounded 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

7
Single Producer / Consumer
  • How about if there are two or more producers and
    consumers?

8
Multiple Producers / Consumers
  • Enforce mutual exclusion!!

9
The Dining Philosophers
  • Five philosophers thinking or eating. Has to get
    two forks to eat. Interesting but not hygiene.

10
The 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.

11
The Dining Philosophers (cntd)
12
The 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.

13
An Exclusion Problem
14
An Exclusion Problem (cntd)
15
Exclusion 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.

16
Using Condition Synchronization
17
Passing the Baton
  • A process within the CS holding a baton passes it
    to another process.

18
Passing the Baton (cntd)
19
Passing the Baton (cntd)
  • Simplifying the SIGNAL statement

20
Alternative 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)

21
Fair 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).

22
Resource 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

23
Using the Passing-the-Baton
  • Request (parameters)
  • P(e)
  • if (request cannot be satisfied) DELAY
  • take units
  • SIGNAL
  • Release(parameters)
  • P(e)
  • return units
  • SIGNAL

24
Shortest-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.

25
Implementation using Passing the Baton Technique
  • Request (time, id)
  • P(e)
  • If(!free) DELAY
  • Free false
  • SIGNAL
  • Release()
  • P(e)
  • Freetrue
  • SIGNAL

26
Using Semaphores
27
Generalizing 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.

28
Pthread
  • 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)

29
Semaphores
  • Sem_t mutex
  • Sem_init (mutex, SHARED, 1)
  • Sem_wait (mutex) // P(mutex)
  • Critical section
  • Sem_post (mutex) // V(mutex)

30
Producer and Consumer (one buffer) using Pthreads
Write a Comment
User Comments (0)
About PowerShow.com