chapter 5 concurrency: mutual exclusion and synchronization
DESCRIPTION
Chapter 5 Concurrency: Mutual Exclusion and Synchronization. Operating Systems: Internals and Design Principles. Seventh Edition By William Stallings. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/1.jpg)
Chapter 5Concurrency:
Mutual Exclusion and
Synchronization
Operating
Systems:Internals
and Design
Principles
Seventh EditionBy William Stallings
![Page 2: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/2.jpg)
Operating Systems:Internals and Design Principles
“ Designing correct routines for controlling concurrent activities proved to
be one of the most difficult aspects of systems programming. The ad hoc techniques used by programmers of early multiprogramming and real-time systems were always vulnerable to subtle programming errors whose effects could be observed only when certain relatively rare sequences of actions occurred. The errors are particularly difficult to locate, since the precise conditions under which they appear are very hard to reproduce.”
—THE COMPUTER SCIENCE AND ENGINEERING RESEARCH STUDY,
MIT Press, 1980
![Page 3: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/3.jpg)
Multiple ProcessesOperating System design is concerned with the management of processes and threads:
MultiprogrammingMultiprocessingDistributed Processing
![Page 4: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/4.jpg)
ConcurrencyArises in Three Different
Contexts:
Multiple Applications
invented to allow
processing time to be
shared among active
applications
Structured Applications
extension of modular
design and structured
programming
Operating System
Structure
OS themselves
implemented as a set of
processes or threads
![Page 5: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/5.jpg)
![Page 6: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/6.jpg)
Principles of Concurrency
Interleaving and overlapping can be viewed as examples of concurrent
processing both present the same problems
Uniprocessor – the relative speed of execution of processes cannot be predicted
depends on activities of other processes the way the OS handles interrupts scheduling policies of the OS
![Page 7: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/7.jpg)
Difficulties of Concurrency
Sharing of global resourcesDifficult for the OS to manage the
allocation of resources optimallyDifficult to locate programming errors
as results are not deterministic and reproducible
![Page 8: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/8.jpg)
Race Condition Occurs when multiple processes or
threads read and write data items The final result depends on the
order of execution the “loser” of the race is the
process that updates last and will determine the final value of the variable
![Page 9: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/9.jpg)
Race Condition (Example 1)• Two processes share the global variable a
– P1 and P2, • At some point in its execution,
– P1 updates a to the value 1, – P2 updates a to the value 2.
• Thus, the two tasks are in a race to write variable a . • Results
– the “loser” of the race (the process that updates last) determines the final value of a .
![Page 10: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/10.jpg)
Race Condition (Example 2)• Two processes share the global variable b, c
– P3 and P4, – b = 1 and c = 2 .
• At some point in its execution, – P3: b= b + c – P4: c = b + c .
• Results– If P3 executes first,
• b = 3 and c = 5 . – If P4 executes first,
• b = 4 and c = 3 .
![Page 11: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/11.jpg)
Operating System Concerns
Design and management issues raised by the existence of concurrency: The OS must:
be able to keep track of various processes allocate and de-allocate resources for each
active process protect the data and physical resources of
each process against interference by other processes
ensure that the processes and outputs are independent of the processing speed
![Page 12: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/12.jpg)
![Page 13: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/13.jpg)
Resource Competition
Concurrent processes come into conflict when they are competing for use of the same resource
for example: I/O devices, memory, processor time, clockIn the case of competing processes three control problems must be faced:
• the need for mutual exclusion
• deadlock• starvation
![Page 14: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/14.jpg)
Mutual Exclusion
Figure 5.1 Illustration of Mutual Exclusion
![Page 15: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/15.jpg)
The enforcement of mutual exclusion creates two additional control problems.
deadlock . P1 and P2, and two resources, R1 and R2.
Suppose that each process needs access to both resources to perform part of its function. • Situation: R1 to P2, and R2 to P1. • Each process is waiting for one of the two resources.• Neither will release the resource that it already owns until it has acquired
the other resource and performed the function requiring both resources.
starvation . P1, P2, P3 each require periodic access to resource R.
Situation• P1 has R, and both P2 and P3 waiting for R. • When P1 exits its critical section, P3 access to R. • When P3 exits its critical section, P1 access to R.
P2 may indefinitely be denied access to the resource, even though there is no deadlock situation.
![Page 16: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/16.jpg)
Requirements for Mutual Exclusion
Must be enforced A process that halts must do so without
interfering with other processes No deadlock or starvation A process must not be denied access to a critical
section when there is no other process using it No assumptions are made about relative process
speeds or number of processes A process remains inside its critical section for a
finite time only
![Page 17: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/17.jpg)
Common Conc
urrency
Mechanis
ms
![Page 18: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/18.jpg)
Semaphore
There is no way to inspect or
manipulate semaphores other than these three
operations
A variable that has an integer
value upon which only three
operations are defined:
1) May be initialized to a nonnegative integer value
2) The semWait operation decrements the value
3) The semSignal operation increments the value
![Page 19: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/19.jpg)
Semaphore Principle Two or more processes can cooperate by means of simple
signals a process can be forced to stop at a specified place until it has
received a specific signal. For signaling, special variables called semaphores are
used. To transmit a signal via semaphore s , a process executes the
primitive semSignal(s) . To receive a signal via semaphore s , a process executes the
primitive semWait(s) ; if the corresponding signal has not yet been transmitted, the
process is suspended until the transmission takes place.
![Page 20: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/20.jpg)
Consequences
There is no way to know before a
process decrements a
semaphore whether it will block or not
There is no way to know which process will
continue immediately on a
uniprocessor system when two
processes are running
concurrently
You don’t know whether another process is waiting so the number of
unblocked processes may be
zero or one
![Page 21: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/21.jpg)
Semaphore Primitives
![Page 22: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/22.jpg)
How Semaphore works?• To begin, the semaphore has a zero or positive value (e.g., 5).
– semWait is called 5 times– 5 processes that can issue a wait and immediately continue to execute.
• If the value is zero, either by initialization or because a number of processes equal to the initial semaphore value have issued a wait, the next process to issue a wait is blocked, and the semaphore value goes negative. – Each subsequent wait drives the semaphore value further into minus territory.
• The negative value equals the number of processes waiting to be unblocked. – Each signal unblocks one of the waiting processes (In the queue) when the
semaphore value is negative.
![Page 23: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/23.jpg)
Binary Semaphore Primitives
![Page 24: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/24.jpg)
Strong/Weak Semaphores
A queue is used to hold processes waiting on the semaphore
• the process that has been blocked the longest is released from the queue first (FIFO)
Strong Semaphores
• the order in which processes are removed from the queue is not specified
Weak Semaphores
![Page 25: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/25.jpg)
Example of Semaphore Mechanism
![Page 26: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/26.jpg)
Mutual Exclusion
![Page 27: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/27.jpg)
Shared Data Protected
by a Semaphore
![Page 28: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/28.jpg)
Producer/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
The Problem:• ensure that the
producer can’t add data into full buffer and consumer can’t remove data from an empty buffer
![Page 29: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/29.jpg)
Buffer Structure
![Page 30: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/30.jpg)
• n = in – out : keep track of the number of items in the buffer, using the integer variable
• The semaphore s is used to enforce mutual exclusion;
• The semaphore n # of item
![Page 31: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/31.jpg)
Finite Circular Buffer
![Page 32: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/32.jpg)
![Page 33: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/33.jpg)
Solution
Using
Semaph
oresFigure 5.13 A Solution to the Bounded-Buffer Producer/Consumer Problem Using Semaphores
![Page 34: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/34.jpg)
Monitors Programming language construct that provides
equivalent functionality to that of semaphores and is easier to control
Implemented in a number of programming languages
including Concurrent Pascal, Pascal-Plus, Modula-2, Modula-3, and Java
Has also been implemented as a program library Software module consisting of one or more
procedures, an initialization sequence, and local data
![Page 35: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/35.jpg)
Monitor Characteristics
Local data variables are
accessible only by the monitor’s procedures and
not by any external
procedure
Process enters monitor by
invoking one of its procedures
Only one process may be
executing in the monitor at a time
![Page 36: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/36.jpg)
Synchronization Achieved by the use of condition
variables that are contained within the monitor and accessible only within the monitor
Condition variables are operated on by two functions:
cwait(c): suspend execution of the calling process on condition c
csignal(c): resume execution of some process blocked after a cwait on the same condition
![Page 37: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/37.jpg)
Structure of a Monitor
Figure 5.15 Structure of a Monitor
![Page 38: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/38.jpg)
Problem Solution Using a Monitor
Figure 5.16 A Solution to the Bounded-Buffer Producer/Consumer Problem Using a Monitor
![Page 39: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/39.jpg)
Message Passing When processes interact with one another
two fundamental requirements must be satisfied:
Message Passing is one approach to providing both of these functions
works with distributed systems and shared memory multiprocessor and uniprocessor systems
synchronization• to enforce
mutual exclusion
communication
• to exchange information
![Page 40: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/40.jpg)
Message Passing The actual function is normally provided in
the form of a pair of primitives:send (destination, message)receive (source, message)
A process sends information in the form of a message to another process designated by a destination
A process receives information by executing the receive primitive, indicating the source and the message
![Page 41: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/41.jpg)
Message Passing
Table 5.5 Design Characteristics of Message Systems for Interprocess Communication and Synchronization
![Page 42: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/42.jpg)
Synchronization
Communication of a message between two
processes implies synchronization between the two
When a receive primitive is executed in a process there are two possibilities:
if a message has previously been sent the message is received and execution continues
if there is no waiting message the process is
blocked until a message arrives or the process continues to execute,
abandoning the attempt to receivethe receiver
cannot receive a message until it has been sent by another process
![Page 43: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/43.jpg)
Blocking Send, Blocking Receive
Both sender and receiver are blocked until the message is delivered
Sometimes referred to as a rendezvous
Allows for tight synchronization between processes
![Page 44: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/44.jpg)
Nonblocking Send
• sender continues on but receiver is blocked until the requested message arrives
• most useful combination• sends one or more messages to a variety of
destinations as quickly as possible• example -- a service process that exists to
provide a service or resource to other processes
Nonblocking send, blocking receive
• neither party is required to wait
Nonblocking send, nonblocking receive
![Page 45: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/45.jpg)
Addressing Schemes for specifying processes
in send and receive primitives fall into two categories:
Direct addressi
ng
Indirect addressi
ng
![Page 46: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/46.jpg)
Direct Addressing Send primitive includes a specific
identifier of the destination process Receive primitive can be handled in
one of two ways: require that the process explicitly
designate a sending process effective for cooperating concurrent processes
implicit addressing source parameter of the receive primitive
possesses a value returned when the receive operation has been performed
![Page 47: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/47.jpg)
Indirect Addressing
Messages are sent to a shared data
structure consisting of queues that can temporarily hold
messages
Queues are referred to as
mailboxes
One process sends a message to the mailbox and the
other process picks up the message from
the mailbox
Allows for greater
flexibility in the use of messages
![Page 48: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/48.jpg)
Indirect ProcessCommunication
![Page 49: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/49.jpg)
General Message Format
![Page 50: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/50.jpg)
Mutual Exclusion
![Page 51: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/51.jpg)
Message Passing Example
Figure 5.21 A Solution to the Bounded-Buffer Producer/Consumer Problem Using Messages
![Page 52: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/52.jpg)
Readers/Writers Problem
A data area is shared among many processes some processes only read the data area, (readers)
and some only write to the data area (writers) Conditions that must be satisfied:
1. any number of readers may simultaneously read the file
2. only one writer at a time may write to the file
3. if a writer is writing to the file, no reader may read it
![Page 53: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/53.jpg)
Readers Have Priority
Figure 5.22 A Solution to the Readers/Writers Problem Using Semaphore: Readers Have Priority
Solution
![Page 54: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/54.jpg)
Solution:Writers Have Priority
Figure 5.23 A Solution to the Readers/Writers Problem Using Semaphore: Writers Have Priority
![Page 55: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/55.jpg)
State of the Process Queues
![Page 56: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/56.jpg)
Message Passing
Figure 5.24 A Solution to the Readers/Writers Problem Using Message Passing
![Page 57: Chapter 5 Concurrency: Mutual Exclusion and Synchronization](https://reader035.vdocument.in/reader035/viewer/2022062218/568164aa550346895dd6a730/html5/thumbnails/57.jpg)
SummaryMessages• Useful for the enforcement of mutual exclusion discipline
Operating system themes are:• Multiprogramming, multiprocessing, distributed processing• Fundamental to these themes is concurrency
• issues of conflict resolution and cooperation arise
Mutual Exclusion• Condition in which there is a set of concurrent processes, only one of
which is able to access a given resource or perform a given function at any time
• One approach involves the use of special purpose machine instructions
Semaphores• Used for signaling among processes and can be readily used to
enforce a mutual exclusion discipline