Lecture 9: Semaphores and Some Classical IPC Problems Ch' 2'3 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Lecture 9: Semaphores and Some Classical IPC Problems Ch' 2'3

Description:

When you create the semaphore, you can initialize its value to ... ( think busy nightclubs/bouncer) Thread i. critical section; 10. COP 4600 Operating Systems ... – PowerPoint PPT presentation

Number of Views:361
Avg rating:3.0/5.0
Slides: 11
Provided by: Ai51
Category:

less

Transcript and Presenter's Notes

Title: Lecture 9: Semaphores and Some Classical IPC Problems Ch' 2'3


1
Lecture 9 Semaphores and Some Classical IPC
Problems (Ch. 2.3)
2
Semaphores (cont)
  • A semaphore is like an integer, with three
    differences
  • When you create the semaphore, you can initialize
    its value to any integer, but after that the only
    operations you are allowed to perform are
    increment (increase by one) and decrement
    (decrease by one). You cannot read the current
    value of the semaphore.
  • 2. When a thread decrements the semaphore, if the
    result is negative, the thread blocks itself and
    cannot continue until another thread increments
    the semaphore.
  • 3. When a thread increments the semaphore, if
    there are other threads waiting, one of the
    waiting threads gets unblocked.

3
Consequences
  • In general, there is no way to know before a
    thread decrements a semaphore whether it will
    block or not.
  • After a thread increments a semaphore and another
    thread gets woken up, both threads continue
    running concurrently. There is no way to know
    which thread, if either, will continue
    immediately.
  • When you signal a semaphore, you dont
    necessarily know whether another thread is
    waiting, so the number of unblocked threads may
    be zero or one.

4
Syntax (in pseudo-language)
  • sem semaphore (value)
  • Multiple equivalent notations
  • sem.signal() sem.wait()
  • sem.up() sem.down()
  • sem.V() sem.P()

5
Basic Synchronization Patterns
  • Serialization
  • Rendez-vous
  • Mutual exclusion
  • Multiplex
  • Barrier (for some of you to figure out)

6
Serialization
  • Thread A
  • statement a1
  • Thread B
  • statement b1

Requirement statement a1 runs before statement
b1.
7
Rendez-vous
Thread A statement a1 statement a2
Thread B statement b1 statement b2
  • Requirement Thread A has to wait for Thread B
    and vice-versa a1 happens before b2 and b1
    happens before a2.

8
Mutual Exclusion
Thread A count count 1
Thread B count count 1
  • Requirement only one thread accesses the shared
    variable at a time.
  • Question what would be the problem is mutual
    exclusion not enforced in this case?

9
Multiplex
Thread i critical section
  • Requirement allow multiple threads to run in the
    critical section at the same time, but not more
    than n threads. (think busy nightclubs/bouncer)

10
Producer/Consumer with Semaphores
Write a Comment
User Comments (0)
About PowerShow.com