Concurrency: Mutual Exclusion and Synchronization: Operating Systems: Internals and Design Principles

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 61

Operating

Systems: Chapter 5
Internals Concurrency: Mutual
and Design
Principles Exclusion and
Synchronization
Seventh Edition
By William Stallings
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


Multiple Processes
 Operating System design is concerned
with the management of processes and
threads:
 Multiprogramming
 Multiprocessing
 Distributed Processing
Concurrency
Arises in Three Different Contexts:
C o n c u r re n c y
Key

Te r
ms

Table 5.1 Some Key Terms Related to Concurrency


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
Difficulties of Concurrency
 Sharing of global resources
 Difficult
for the OS to manage the allocation
of resources optimally
 Difficultto locate programming errors as
results are not deterministic and reproducible
Race Condition
 Occurs when multiple processes or
threads read and write data items
 Thefinal 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
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
Pro
ces
s
Inte
ract
ion
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, clock
Mutual Exclusion

Figure 5.1 Illustration of Mutual Exclusion


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
Mutual Exclusion:
Hardware Support
• Interrupt Disabling • Disadvantages:
– uniprocessor system – the efficiency of execution
could be noticeably
– disabling interrupts degraded
guarantees mutual
exclusion – this approach will not
work in a multiprocessor
architecture
Mutual Exclusion:
Hardware Support
 Special Machine Instructions
 Compare&Swap Instruction
 also called a “compare and exchange
instruction”
 a compare is made between a memory value
and a test value
 if the values are the same a swap occurs
 carried out atomically
Compare and Swap
Instruction

Figure 5.2 Hardware Support for Mutual Exclusion


Exchange Instruction

Figure 5.2 Hardware Support for Mutual Exclusion


Special Machine Instruction:
Advantages
Applicable to any number of processes on
either a single processor or multiple
processors sharing main memory
Simple and easy to verify
It can be used to support multiple critical
sections; each critical section can be defined
by its own variable
Special Machine Instruction:
Disadvantages
 Busy-waiting is employed, thus while a
process is waiting for access to a critical
section it continues to consume processor
time
 Starvation is possible when a process leaves a
critical section and more than one process is
waiting
 Deadlock is possible
Comm
on
Concur
rency
Mecha
nisms
Semaphore

1) May be initialized to a nonnegative integer value


2) The semWait operation decrements the value
3) The semSignal operation increments the value
Consequences
Semaphore Primitives
Binary Semaphore Primitives
Strong/Weak Semaphores
 A queue is used to hold processes waiting on the semaphore
Example of Semaphore
Mechanism
Mutual Exclusion
Shared Data Protected
by a Semaphore
Producer/Consumer Problem
Buffer Structure
Inco
rrec
t

Solu
tion

Figure 5.9 An Incorrect Solution to the Infinite-Buffer Producer/Consumer Problem Using Binary Semaphores
Pos
sibl
e
Sol
utio
n
Corr
ect

Solu
tion

Figure 5.10 A Correct Solution to the Infinite-Buffer Producer/Consumer Problem Using Binary Semaphores
Se
Sol
m
uti
ap
on
ho
Usi
res
ng
Finite
Circular
Buffer
Solutio
n
Using
Sema
phore
s
Figure 5.13 A Solution to the Bounded-Buffer Producer/Consumer Problem Using Semaphores
Implementation of
Semaphores
 Imperativethat the semWait and semSignal
operations be implemented as atomic primitives
 Can be implemented in hardware or firmware
 Softwareschemes such as Dekker’s or
Peterson’s algorithms can be used
 Useone of the hardware-supported schemes
for mutual exclusion
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
Monitor Characteristics
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
Structure of a Monitor

Figure 5.15 Structure of a Monitor


Problem Solution
Using a Monitor

Figure 5.16 A Solution to the Bounded-Buffer Producer/Consumer Problem Using a Monitor


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
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
Message Passing

Table 5.5 Design Characteristics of Message Systems for Interprocess Communication and Synchronization
Synchronization
Blocking Send,
Blocking Receive
 Both
sender and receiver are blocked until the
message is delivered
 Sometimes referred to as a rendezvous
 Allowsfor tight synchronization between
processes
Nonblocking Send
Addressing
 Schemes for specifying processes in send
and receive primitives fall into two
categories:
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
Indirect Addressing
Indirect Process
C
o
m
m
un
ica
tio
n
General Message Format
Mutual Exclusion
Message Passing Example

Figure 5.21 A Solution to the Bounded-Buffer Producer/Consumer Problem Using Messages


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
Readers Have Priority

So
lut
io
n

Figure 5.22 A Solution to the Readers/Writers Problem Using Semaphore: Readers Have Priority
Solution:
Writers Have Priority

Figure 5.23 A Solution to the Readers/Writers Problem Using Semaphore: Writers Have Priority
State of the Process Queues
Message Passing

Figure 5.24 A Solution to the Readers/Writers Problem Using Message Passing


Summary

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy