Operating
Systems: Week-5
Internals Concurrency: Mutual
and Design
Principles Exclusion and
Synchronization
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:
Multiple
Applications
Structured
Applications Operating
invented to allow System
processing time to Structure
be shared among extension of
active applications modular design
and structured
programming OS themselves
implemented as a
set of processes or
threads
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
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
In the case of competing processes three
control problems must be faced:
• the need for mutual exclusion
• deadlock
• starvation
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
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
Semaphore
A variable that has an There is no way to
integer value upon inspect or manipulate
which only three semaphores other than
operations are defined: these three operations
1) May be initialized to a nonnegative integer value
2) The semWait operation decrements the value
3) The semSignal operation increments the value
Consequences
There is no way to
know which process You don’t know
There is no way to
will continue whether another
know before a
immediately on a process is waiting so
process decrements a
uniprocessor system the number of
semaphore whether it
when two processes unblocked processes
will block or not
are running may be zero or one
concurrently
Strong/Weak Semaphores
A queue is used to hold processes waiting on the semaphore
Strong Semaphores
• the process that has been blocked the longest is
released from the queue first (FIFO)
Weak Semaphores
• the order in which processes are removed from the
queue is not specified
Producer/Consumer Problem
General
Situation: The Problem:
• ensure that the
• one or more producers are
producer can’t add
generating data and placing data into full buffer
these in a buffer
and consumer can’t
• a single consumer is taking
remove data from
items out of the buffer one an empty buffer
at time
• only one producer or
consumer may access the
buffer at any one time
Buffer Structure
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
Local data variables
are accessible only
by the monitor’s
procedures and not Only one process
by any external may be executing in
procedure the monitor at a time
Process enters
monitor by invoking
one of its procedures
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
Message Passing
When processes interact with one another two
fundamental requirements must be satisfied:
synchronization communication
• to enforce mutual • to exchange
exclusion information
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
Synchronization
When a r
executed eceive primitive
in a proc is
es s the
a message two possi re are
Communication of sses bilities:
between two proce n if there
atio
implies synchroniz i
process s no waiting me
between the two i s
arrives s blocked until sage the
o a
execute, r the process co message
abandon n
ing the a tinues to
receive ttempt t
no t o
the receiver can til
g e un
receive a messa by if a mess
ag
it has been sent been sen e has previously
t
another process received the message is
and exe
continue cution
s
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
Nonblocking send, blocking receive
• 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, nonblocking receive
• neither party is required to wait
Addressing
Schemes for specifying processes in send
and receive primitives fall into two
categories:
Direct Indirect
addressing addressing
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
Messages are sent to a Queues are
shared data structure
consisting of queues that can referred to as
temporarily hold messages mailboxes
One process sends a
Allows for greater message to the mailbox
flexibility in the and the other process picks
use of messages up the message from the
mailbox
Indirect Process
C
o
m
m
un
ica
tio
n
General Message Format
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
Messages Summary
• 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
End