Professional Documents
Culture Documents
Operating System
Principles of Concurrency
A simple example
Race condition
Operating system concerns
Process interaction
Requirements for mutual exclusion
Hardware Support
Semaphores
Monitors
Message Passing
Interrupt disabling
activities
systems
hoc
techniques
used
by
Concurrency
race conditions
OS concerns
mutual exclusion requirements
ad
The
Learning Objectives
programming.
controlling concurrent
and Synchronization
Slides-4
Mutual Exclusion
Concurrency:
Readers/Writers Problem
Operating System
Chapter 05
Mutual exclusion
The Producer/Consumer problem
Implementation of semaphores
Synchronization
Addressing
Message format
Queueing discipline
Mutual exclusion
Multiprocessing
The management of multiple processes within a multiprocessor
system
Distributed processing
The management of multiple processes executing on multiple,
distributed computer systems.
clusters
Fundamental to all of these areas, and
Operating System
Slides-3
Operating System
Slides-6
Multiple Applications
Principles of Concurrency
Structured Applications
Operating System
Slides-7
Atomic operation
Critical section
Deadlock
Livelock
Race condition
Starvation
Slides-8
Operating System
Slides-10
Difficulties of Concurrency
Operating System
Slides-11
A simple example
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
How
to control
access to the
shared resource ?
anywhere in a process
Dr. GuoJun LIU
Operating System
Slides-9
Operating System
Slides-12
Race Condition
Resource Competition
A example
processor time
memory
I/O devices
Operating System
Slides-13
Operating System
Slides-16
Mutual Exclusion
The OS must
entercritical (Ra);
/* critical section */
exitcritical (Ra);
How to understand ?
Operating System
Slides-14
Process Interaction
Degree of
Awareness
Processes
unaware of each
other
Relationship
Competition
Processes
sharing
Mutual exclusion
No deadlock or starvation
Deadlock
Starvation
Starvation
Data coherence
Slides-17
Potential Control
Problems
Processes directly
Operating System
Slides-15
Operating System
Slides-18
OS or a
programming language
An integer value used for signaling among processes. Only three operations may be performed
on a semaphore, all of which are atomic: initialize, decrement, and increment. The decrement
operation may result in the blocking of a process, and the increment operation may result in the
unblocking of a process. Also known as a counting semaphore or a general semaphore
Similar to a binary semaphore. A key difference between the two is that the process that locks
the mutex (sets the value to zero) must be the one to unlock it (sets the value to 1).
Condition Variable A data type that is used to block a process or thread until a particular condition is true.
Monitor
Event Flags
Mailboxes/Messages A means for two processes to exchange information and that may be used for synchronization.
Spinlocks
Dr. GuoJun LIU
Operating System
Slides-19
Hardware Support
Interrupt Disabling
uniprocessor system
disabling interrupts
guarantees mutual
exclusion
Mutual exclusion mechanism in which a process executes in an infinite loop waiting for the
value of a lock variable to indicate availability.
Operating System
Slides-22
Semaphore
Disadvantages
The
fundamental principle
is this:
Operating System
Slides-20
Advantages
Operating System
Slides-23
Semaphore
Disadvantages
Busy-waiting is employed
Starvation is possible
when a process leaves a
critical section and more
than one process is waiting
Deadlock is possible
Operating System
Slides-21
There is no way to
inspect or manipulate
semaphores other than
these three operations
Operating System
Slides-24
Consequences
Strong/Weak Semaphores
Weak Semaphores
the order in which processes are removed from the
queue is not specified
Operating System
Slides-25
Operating System
Slides-28
Operating System
Slides-26
Slides-29
Operating System
Slides-27
Slides-30
Infinite Buffer
Slides-31
Operating System
General Situation:
Slides-34
what's meaning ?
Slides-35
Producer
Consumer
semWaitB(s)
n++
if (n==1) (semSignalB(delay))
semSignalB(s)
1
2
The Problem:
ensure that the producer
cant add data into full
buffer
and consumer cant
remove data from an
empty buffer
Operating System
Operating System
Slides-32
Producer/Consumer Problem
Slides-33
Delay
semWaitB(delay)
semWaitB(s)
n--
9
10
semSignalB(s)
semWaitB(s)
11
n++
12
if (n==1) (semSignalB(delay))
13
semSignalB(s)
14
if (n==0) (semWaitB(delay))
15
semWaitB(s)
16
n--
17
semSignalB(s)
18
if (n==0) (semWaitB(delay))
19
semWaitB(s)
20
n--
21
semSignalB(s)
Implementation of Semaphores
Operating System
Disadvantages of Semaphores
Which one
produces a
serious flaw ?
Semaphores provide a
primitive yet powerful and
flexible tool
Difficult to produce a
correct program using
semaphores
semWait and semSignal operations
may be scattered throughout a
program
not easy to see the overall effect of
these operations on the semaphores
they affect
Slides-40
Operating System
Slides-41
Monitors
what's meaning ?
The monitor is a
programming-language
construct
It is easier to control
It is a software module
one or more procedures
an initialization sequence
local data
Operating System
Slides-42
Monitor Characteristics
local data
condition variables
Process enters
monitor by
invoking one of
its procedures
initialization code
Operating System
Synchronization
Slides-46
Slides-43
csignal(c)
What is different
from those for the
semaphore ?
Operating System
Slides-47
Slides-44
Structure of a Monitor
Semaphore
Monitor
three advantages
all of the synchronization
functions are confined to
the monitor
What is programmers
responsibility for
mutual exclusion and synchronization ?
Slides-45
Slides-48
Synchronization
Send
If the process issuing the csignal has not finished with the
monitor, then two additional process switches are required
one to block this process
another to resume it when the monitor becomes available
Addressing
Direct
blocking
send
nonblocking
receive
explicit
Receive
implicit
blocking
Indirect
nonblocking
static
dynamic
Format
ownership
Content
Length
Queueing Discipline
FIFO
Priority
fixed
variable
Butler W Lampson
United States 1992
Dr. GuoJun LIU
Operating System
Slides-49
Message Passing
Operating System
Slides-52
Synchronization
communication
to exchange information
Operating System
Slides-50
Message Passing
Slides-53
Operating System
Slides-51
Operating System
Slides-54
Nonblocking Send
Operating System
Slides-55
Direct Addressing
Operating System
Slides-58
implicit addressing
source parameter of the receive primitive possesses a value returned
when the receive operation has been performed
Operating System
Slides-56
Indirect Addressing
Operating System
Slides-59
mailboxes
Slides-57
Operating System
Slides-60
what's meaning ?
Slides-61
Slides-64
Readers/Writers Problem
Operating System
Slides-62
Readers/Writers Problem
Operating System
Slides-65
Slides-63
wsem set
no queues
Operating System
Slides-66
Slides-67
Summary
Hardware support
OS or a programming
language
Semaphores
Used for signaling among
processes and can be readily used
to enforce a mutual exclusion
discipline
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
Three approaches to supporting
Monitors
Messages
Operating System
Slides-68