Semaphores - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Semaphores

Description:

... requires busy waiting. ... an integer value and a list of processes waiting on the semaphore ... are deadlocked since P0 waits for P1 to execute ... – PowerPoint PPT presentation

Number of Views:1969
Avg rating:3.0/5.0
Slides: 9
Provided by: marily214
Category:

less

Transcript and Presenter's Notes

Title: Semaphores


1
Semaphores
  • A semaphore S is an integer variable that can
    only be accessed via two indivisible (atomic)
    operations wait and signal
  • wait (S)
  • while S? 0 // no-op S--
  • signal (S)
  • S
  • Only one process may modify S at a time

2
n-Process Critical Section Problem
  • Shared data
  • semaphore mutex // initially mutex 1
  • Process Pi do wait(mutex)
    critical section
  • signal(mutex) remainder section
    while (1)
  • Satisfy mutual exclusion and progress
  • Bounded waiting depend on semaphore
    implementation

3
Semaphore as a General Synchronization Tool
  • Two processes P1 and P2 , want P2 to execute S2
    after P1 executes S1
  • Let P1 and P2 share a common semaphore synch,
    initialized to 0
  • P1 P2
  • S1 wait(synch)
  • signal(synch) S2

4
Semaphore Implementation
  • The semaphore definition requires busy waiting.
    This type of semaphore is also called a spinlock.
  • Waste CPU cycles on uniprocessor systems
  • Useful in multiprocessor systems when the process
    only wait for a short period of time because no
    context switch is required.
  • Solution let processes block themselves instead
    of busy waiting
  • Put process in a waiting queue associated with
    the semaphore
  • Switch the process state to waiting
  • Transfer control to CPU scheduler
  • When another process executes signal, restart a
    process waiting on the semaphore

5
Semaphore Implementation
  • Define a semaphore as a structure
  • typedef struct
  • int value struct process L
    semaphore
  • Each semaphore has an integer value and a list of
    processes waiting on the semaphore
  • Assume two operations
  • block() change the process that invokes it to
    waiting state
  • wakeup(P) change process P from waiting state to
    ready state and put it in the ready queue.

6
Semaphore Implementation
  • Semaphore operations now defined as
  • wait(S) S.value--
  • if (S.value lt 0)
  • add this process to S.L
    block() // process changed to waiting state
  • signal(S) S.value
  • if (S.value lt 0)
  • remove a process P from S.L
    wakeup(P) // process P changed to ready state
  • When semaphore value is negative, its magnitude
    is the number of processes waiting on the
    semaphore
  • To ensure bounded waiting, implement the list of
    waiting processes as a FIFO queue

7
Implementation Issues
  • Wait and signal must be atomic no two processes
    can execute wait and signal on the same semaphore
    at the same time a critical-section problem!
  • Two solutions
  • 1. Inhibit interrupts during the wait and signal
    operations (only work for uniprocessor systems)
  • 2. Use software solutions for the
    critical-section problem (e.g. bakery algorithm)
    where the critical sections consist of the wait
    and signal procedures
  • Still have busy waiting, but time spent in
    critical section (i.e., executing signal/wait)
    will be short

8
Deadlock and Starvation
  • Deadlock two or more processes are waiting
    indefinitely for an event that can be caused by
    only one of the waiting processes.
  • An example
  • Let S and Q be two semaphores initialized
    to 1
  • P0 P1
  • wait(S) wait(Q)
  • wait(Q) wait(S)
  • ? ?
  • signal(S) signal(Q)
  • signal(Q) signal(S)
  • P0 and P1 are deadlocked since P0 waits for P1 to
    execute signal(Q)
  • and P1 waits for P0 to execute signal(S)
  • Starvation or indefinite blocking a process may
    wait indefinitely in a semaphore queue.
Write a Comment
User Comments (0)
About PowerShow.com