Module 7: Process Synchronization - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Module 7: Process Synchronization

Description:

Operating Systems Lecture 22 Semaphores Classic Synchronization Problems – PowerPoint PPT presentation

Number of Views:148
Avg rating:3.0/5.0
Slides: 19
Provided by: Marily533
Category:

less

Transcript and Presenter's Notes

Title: Module 7: Process Synchronization


1
Operating SystemsLecture 22 Semaphores Classic
Synchronization Problems
2
Semaphores
  • Synchronization tool that does not require busy
    waiting.
  • Semaphore S integer variable
  • can only be accessed via two indivisible (atomic)
    operations (Note order of wait instructions
    corrected from last set of slides).
  • wait (S)
  • S--
  • if (S lt 0) then block(P)
  • signal (S)
  • S
  • if (S lt 0) then wakeup(P)

3
Critical Section of n Processes
  • Shared data
  • semaphore mutex //initially mutex 1
  • Process Pi do wait(mutex)
    critical section
  • signal(mutex) remainder section
    while (1)

4
Questions about Semaphore
  • Suppose mutex is initialized to 1.
  • What does mutex 1 mean?
  • What does mutex 0 mean?
  • What does mutex lt 0 mean?
  • When should mutex be initialized to value gt 1?

5
Semaphore Implementation
  • Define a semaphore as a record
  • typedef struct
  • int value struct process L
    semaphore
  • Assume two simple operations
  • block suspends the process that invokes it.
  • wakeup(P) resumes the execution of a blocked
    process P.

6
Implementation
  • Semaphore operations now defined as
  • void wait(semaphore S) S.value--
  • if (S.value lt 0)
  • add this process to S.L block( )
  • void signal(semaphore S) S.value
  • if (S.value lt 0)
  • remove a process P from S.L wakeup(P)

7
Notes on Implementation
  • If the semaphore has a negative value, the
    magnitude of the value indicates the number of
    processes waiting on that semaphore.
  • One can use a FIFO queue to ensure bounded
    waiting. (A LIFO queue can lead to starvation).
  • The wait and signal must be executed atomically.
  • In a single processor environment, can disable
    interrupts during the wait and signal function
    calls.
  • In a multiprocessor environment, can use a
    solution to the critical section problem
    discussed earlier. Either a hardware solution
    (e.g. TestAndSet) if available, or a software
    solution.

8
What is the Problem here?
  • .
  • 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)

9
Two Types of Semaphores
  • Counting semaphore integer value can range over
    an unrestricted domain.
  • Binary semaphore integer value can range only
    between 0 and 1 can be simpler to implement.
  • Can implement a counting semaphore S as a binary
    semaphore.

10
Implementing S as a Binary Semaphore
  • Data structures
  • binary-semaphore S1, S2
  • int C
  • Initialization
  • S1 1
  • S2 0
  • C initial value of semaphore S

11
Implementing S
  • wait operation
  • wait (S)
  • wait(S1)
  • C--
  • if (C lt 0)
  • signal(S1)
  • wait(S2)
  • signal(S1)
  • signal operation
  • signal (S)
  • wait(S1)
  • C
  • if (C lt 0)
  • signal(S2)
  • else
  • signal(S1)

12
Bounded-Buffer Problem
  • Assume n buffer slots for data
  • Shared datasemaphore full, empty,
    mutexInitiallyfull 0, empty n, mutex 1

13
Bounded-Buffer Problem Producer Process
  • do
  • produce an item in nextp
  • wait(empty)
  • wait(mutex)
  • add nextp to buffer
  • signal(mutex)
  • signal(full)
  • while (1)

14
Bounded-Buffer Problem Consumer Process
  • do
  • wait(full)
  • wait(mutex)
  • remove an item from buffer to nextc
  • signal(mutex)
  • signal(empty)
  • consume the item in nextc
  • while (1)

15
Readers-Writers Problem
  • Shared datasemaphore mutex, wrt
  • int readcountInitiallymutex 1, wrt 1,
    readcount 0
  • Writer's code
  • wait(wrt)
  • writing is performed
  • signal(wrt)

16
Readers-Writers Problem Reader Process
  • wait(mutex)
  • readcount
  • if (readcount 1)
  • wait(wrt)
  • signal(mutex)
  • reading is performed
  • wait(mutex)
  • readcount--
  • if (readcount 0)
  • signal(wrt)
  • signal(mutex)

17
Dining-Philosophers Problem
  • Shared data
  • semaphore chopstick5
  • Initially all values are 1

18
Dining-Philosophers Problem
  • Philosopher i
  • do
  • wait(chopsticki)
  • wait(chopstick(i1) 5)
  • eat
  • signal(chopsticki)
  • signal(chopstick(i1) 5)
  • think
  • while (1)
Write a Comment
User Comments (0)
About PowerShow.com