Chapter05-new
Chapter05-new
Chapter 5
Concurrency: Mutual
Exclusion and
Synchronization
Dave Bremer
Otago Polytechnic, N.Z.
©2008, Prentice Hall
Learning Objectives
Roadmap
• Principles of Concurrency
• Mutual Exclusion: Hardware Support
• Semaphores
• Monitors
• Message Passing
• Readers/Writers Problem
Multiple Processes
• Central to the design of modern Operating
Systems is managing multiple processes/threads
– Multiprogramming: Multiple processes on a single
processor
– Multiprocessing: Multiple processes on multiple
processors
– Distributed Processing: Multiple processes on
multiple distributed processors
• Big Issue is Concurrency
– Managing the interaction of all of these processes
Concurrency
Arises in Three Different Contexts:
Multiple
Applications
Structured
Applications Operating
invented to allow System
processing time to be
shared among active extension of modular
Structure
applications design and structured
programming.
Application can be
programmed as multiple
processes or threads
OS themselves
implemented as a set of
processes or threads
Concurrency & Shared Data
• Cooperation by Sharing
• Deadlock, Starvation & mutual exclusion still present
• Data inconsistency may arise
• Example: A bookkeeping application in which two data
items should be maintained such that: a=b
• Consider two processes, P1 and P2
– Normal order P1: a=a+2; b=b+2; P2: b=b*4; a=a*4;
– Abnormal orderP1: a=a+2; P2: b=b*4; P1:b=b+2; P2: a=a*4;
– Prove that the second case wont maintain the
relationship a=b.
Processes Aware of each other
• Cooperation by communication
– Methods provided by OS or language libraries
• No requirement of mutual exclusion
• Deadlock and starvation still possible
– Process A waits for B and B waits for A
– Process A, B want to communicate with C. B
never gets chance to communicate.
Requirements for
Mutual Exclusion
1. Only one process at a time is allowed in
the critical section for a resource
2. A process that halts in its noncritical
section must do so without interfering
with other processes
3. A process requiring access to its critical
section MUST NOT be delayed
indefinitely: No deadlock or starvation
Requirements for
Mutual Exclusion (1)
4. A process must not be delayed access to
a critical section when there is no other
process using it
Programming Reminder:
while (expression)
{ /*do nothing”/ } /*This will loop infinitely if the expression is true*/
Dekker’s Algorithm-First Attempt (contd..)
For Process P1
• Two types:
– 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
• Also known as mutex locks (they are locks that provide mutual
exclusion)
Pi (Pi=1, 2, 3,
….,n) boolean mutex=1; //initialized to 1
Pi calls wait
Wait();
<Critical Section> wait (mutex) {
Signal (); if (mutex==1)
Pi calls calls signal mutex==0;
< remainder
section> else
Op: place this process in block queue of mutex
}
signal (mutex) {
if (mutex queue is empty)
mutex==1;
else
Op: remove one process from mutex queue
}
Counting Semaphores
Bounded-Buffer Problem
Bounded buffers: Producer /Consumer can be seen in e.g.
streaming filters or packet switching in networks.
Dining-Philosophers Problem
Dining philosophers could be a sequence of active database
transactions that have a circular wait-for-lock dependence.