Presentation is loading. Please wait.

Presentation is loading. Please wait.

6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.

Similar presentations


Presentation on theme: "6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization."— Presentation transcript:

1 6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization

2 6.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore Implementation with no Busy waiting Processes block themselves instead of busy waiting Unblocked when some other process releases semaphore Maintain a waiting queue for each semaphore value (of type integer) pointer to next record in the list Two operations: block – place the process invoking the operation on the appropriate waiting queue. wakeup – remove one of processes in the waiting queue and place it in the ready queue.

3 6.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore Implementation with no Busy waiting (Cont.) Implementation of acquire(): Implementation of release():

4 6.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore Implementation with no Busy waiting (Contd.) Semaphore value can be negative Can use any order for waking up blocked processes Some might cause unbounded wait Important to ensure atomicity of acquire() and release() Disable interrupts during acquire() and release() Works for single processor environments Use software solutions to the critical sections problem

5 6.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Deadlocks Deadlock -- Two or more processes are waiting indefinitely for an event that can be triggered by only one of the waiting processes Let S and Q be two semaphores initialized to 1 Solutions to be discussed in next chapter Starvation – Process waiting indefinitely within a semaphore

6 6.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Priority Inversion Semaphores can mess up process priorities A lower priority process may hold a semaphore that a higher priority process needs A more complex scenario Low, medium and high priority processes Low priority process holds a semaphore, high priority process needs to acquire Low priority process is pre-empted by medium priority process that does not need the semaphore Priority-Inheritance protocol A process holding a semaphore will inherit the highest priority that is waiting on the resource

7 6.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

8 6.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded-Buffer Problem Buffer with ‘N’ slots Each slot holds one item Producers continuously generate items and want to insert into the buffer Must wait if buffer has no empty slots Consumers continuously remove items from the buffer Must wait if buffer has no items Only one producer/consumer should be accessing the buffer at any given time

9 6.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded-Buffer Solution Outline Semaphore mutex initialized to the value 1 mutex is for synchronization (prevent simultaneous access to shared buffer) Semaphore full initialized to the value 0 full indicates how many items are there in the buffer Semaphore empty initialized to the value N empty indicates number of empty slots in buffer

10 6.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded-buffer producer

11 6.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded-buffer consumer

12 6.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded-buffer factory

13 6.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded-Buffer

14 6.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded-Buffer insert()

15 6.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Bounded-buffer remove()

16 6.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Readers-Writers Problem A data set is shared among a number of concurrent processes Readers – only read the data set; they do not perform any updates Writers – can both read and write Problem Allow multiple readers to read at the same time Only single writer can access the shared data at the same time No simultaneous reads and writes allowed

17 6.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Readers-Writers Solution readerCount – Integer indicating how many readers are currently reading the shared data Manipulated only by readers Initialized to zero Semaphore mutex initialized to 1 Synchronizing access to the readerCount variable (ensuring only one reader is manipulating readerCount) Semaphore db initialized to 1 Synchronizing access to the data item Ensuring only one writer is in the critical region or any number of readers

18 6.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Readers-Writers Problem Interface for read-write locks

19 6.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Writer Structure

20 6.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Reader Structure

21 6.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Shared DB

22 6.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition ReadLock Methods

23 6.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition WriteLock Methods

24 6.24 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Dining Philosophers Problem 5 philosopher seated on a round table Each philosopher is in a loop of alternate thinking and eating 5 chopsticks Each philosopher has one on each side Philosopher can eat when she has two chopsticks Picks up one chopstick after another Puts them back one after another Array of semaphores (1 for each chopstick)

25 6.25 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Dining-Philosophers Problem Semaphore chopStick [5] initialized to 1

26 6.26 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Philosopher Structure

27 6.27 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Deadlocks What if all philosophers acquire their right (or left) chopstick Nobody can progress – Deadlock !!! Solutions: Allow philosopher to pick up chopsticks if both are available (i.e., guard the array of semaphores in a critical section) Asymmetric solution – Odd philosophers acquire left chopstick first whereas even philosophers acquire right chopstick first

28 6.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Problems with Semaphores Correct use of semaphore operations: Correct  mutex.acquire() …. mutex.release() Incorrect  mutex.acquire () or mutex.release() (or both) Omitting either mutex.acquire() or mutex.release()

29 6.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Monitors Refer to the handout given in class. If you do not have the handout, please see the instructor.

30 6.30 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition End of Chapter 6


Download ppt "6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization."

Similar presentations


Ads by Google