Top Banner
6.1 Silberschatz, Galvin and Gagne ©2009 perating System Concepts with Java – 8 th Edition Module 6: Process Synchronization
30

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

Jan 19, 2018

Download

Documents

Jacob Whitehead

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():
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 6.1 Silberschatz, Galvin and Gagne 2009 Operating System Concepts with Java  8 th Edition Module 6: Process Synchronization.

6.1 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Module 6: Process Synchronization

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

6.2 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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.

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

6.3 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Semaphore Implementation with no Busy waiting (Cont.)

Implementation of acquire():

Implementation of release():

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

6.4 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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

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

6.5 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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

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

6.6 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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

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

6.7 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Classical Problems of Synchronization

Bounded-Buffer Problem

Readers and Writers Problem

Dining-Philosophers Problem

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

6.8 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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

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

6.9 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Bounded-Buffer Solution Outline

Semaphore mutex initialized to the value 1mutex 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 Nempty indicates number of empty slots in buffer

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

6.10 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Bounded-buffer producer

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

6.11 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Bounded-buffer consumer

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

6.12 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Bounded-buffer factory

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

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

Bounded-Buffer

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

6.14 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Bounded-Buffer insert()

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

6.15 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Bounded-buffer remove()

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

6.16 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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

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

6.17 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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

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

6.18 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Readers-Writers Problem

Interface for read-write locks

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

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

Writer Structure

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

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

Reader Structure

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

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

Shared DB

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

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

ReadLock Methods

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

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

WriteLock Methods

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

6.24 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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 anotherPuts them back one after another

Array of semaphores (1 for each chopstick)

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

6.25 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Dining-Philosophers Problem

Semaphore chopStick [5] initialized to 1

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

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

Philosopher Structure

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

6.27 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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

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

6.28 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th 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()

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

6.29 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

Monitors

Refer to the handout given in class. If you do not have the handout, please see the instructor.

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

6.30 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition

End of Chapter 6