Chapter 5 - Concurrency: Mutual exclusion and synchronization. After studying this chapter, you should be able to: Discuss basic concepts related to concurrency, such as race conditions, OS concerns, and mutual exclusion requirements; understand hardware approaches to supporting mutual exclusion; define and explain semaphores; define and explain monitors;...
Trang 1Chapter 5 Concurrency: Mutual
Exclusion and
Synchronization
Operating Systems: Internals and Design
Trang 3Multiple Processes
Systems is managing multiple processes
– Multiprocessing
– Distributed Processing
– Managing the interaction of all of these
processes
Trang 4– Extension of modular design
– OS themselves implemented as a set of processes or threads
Trang 5Key Terms
Trang 6Interleaving and Overlapping Processes
be interleaved on uniprocessors
Trang 7Interleaving and Overlapping Processes
on multi-processors
Trang 8Difficulties of Concurrency
resources
results are not deterministic and
reproducible
Trang 11Enforce Single Access
may enter the function at a time then:
– P2 tries to enter but is blocked – P2 suspends
Trang 12Race Condition
– Multiple processes or threads read and write data items
– They do so in a way where the final result
depends on the order of execution of the
processes
race last
Trang 13Operating System
Concerns
raised by the existence of concurrency?
– Keep track of various processes
– Allocate and de-allocate resources
– Protect the data and resources against
interference by other processes.
– Ensure that the processes and outputs are independent of the processing speed
Trang 14Process Interaction
Trang 15Competition among Processes for Resources
Three main control problems:
– Critical sections
Trang 16Requirements for Mutual Exclusion
the critical section for a resource
section must do so without interfering with other processes
Trang 17Requirements for Mutual Exclusion
a critical section when there is no other
process using it
process speeds or number of processes
for a finite time only
Trang 20while (true) {
/* disable interrupts */; /* critical section */;
/* enable interrupts */;
/* remainder */;
}
Trang 22Compare&Swap
Instruction
int compare_and_swap (int *word,
int testval, int newval)
Trang 23Mutual Exclusion (fig 5.2)
Trang 25Exchange Instruction
(fig 5.2)
Trang 26Hardware Mutual Exclusion: Advantages
either a single processor or multiple
processors sharing main memory
sections
Trang 27Hardware Mutual Exclusion: Disadvantages
leaves a critical section and more than one process is waiting
– Some process could indefinitely be denied
access.
Trang 29– An integer value used for signalling among processes
on a semaphore, all of which are atomic:
– initialize,
Trang 30Semaphore Primitives
Trang 31Binary Semaphore
Primitives
Trang 32Strong/Weak Semaphore
on the semaphore
– In what order are processes removed from the queue?
order of removal from the queue
Trang 33Example of Strong
Semaphore Mechanism
Trang 34Example of Semaphore
Mechanism
Trang 35Mutual Exclusion Using
Semaphores
Trang 36Processes Using
Semaphore
Trang 37Producer/Consumer
Problem
• General Situation:
– One or more producers are generating data and
placing these in a buffer
– A single consumer is taking items out of the buffer
one at time
– Only one producer or consumer may access the
buffer at any one time
– Ensure that the Producer can’t add data into full buffer and consumer can’t remove data from empty buffer
Producer/Consumer Animation
Trang 39Buffer
Trang 40Incorrect Solution
Trang 41Possible Scenario
Trang 42Correct Solution
Trang 43Semaphores
Trang 44Bounded Buffer
Trang 45Semaphores
Trang 46Functions in a Bounded Buffer
/* consume item w
*/
}
•
Trang 47– Demonstrates the bounded-buffer consumer/producer problem using semaphores.
Trang 49construct that provides equivalent
functionality to that of semaphores and
that is easier to control
languages, including
– Concurrent Pascal, Pascal-Plus,
– Modula-2, Modula-3, and Java.
Trang 51variables within a monitor
– only accessible by the monitor.
process on condition c
process blocked after a cwait on the same
condition
Trang 52Structure of a Monitor
Trang 53Bounded Buffer Solution
Using Monitor
Trang 54Solution Using Monitor
Trang 55Bounded
Buffer Monitor
Trang 57Process Interaction
two fundamental requirements must be
– Added bonus: It works with shared memory
and with distributed systems
Trang 58Message Passing
normally provided in the form of a pair of primitives:
Trang 59– Sender must send before receiver can receive
a send or receive primitive?
– Sender and receiver may or may not be
blocking (waiting for message)
Trang 60Blocking send, Blocking receive
message is delivered
processes
Trang 61– Neither party is required to wait
Trang 62which process should receive the
message
– Direct addressing
– Indirect Addressing
Trang 63Direct Addressing
of the destination process
time which process a message is
expected
parameter to return a value when the
receive operation has been performed
Trang 64Indirect addressing
structure consisting of queues
mailbox and the other process picks up the message from the mailbox
Trang 65Indirect Process Communication
Trang 66General Message Format
Trang 67Mutual Exclusion Using
Messages
Trang 68Producer/Consumer
Messages
Trang 70Readers/Writers Problem
processes
– Some processes only read the data area,
some only write to the area
1 Multiple readers may read the file at once.
2 Only one writer at a time may write
3 If a writer is writing to the file, no reader may
read it.
interaction of readers and writers.
Trang 71Readers have Priority
Trang 72Writers have Priority
Trang 74Message Passing