CSC 553 Operating Systems: Lecture 4 - Concurrency: Mutual Exclusion and Synchronization
CSC 553 Operating Systems: Lecture 4 - Concurrency: Mutual Exclusion and Synchronization
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
– 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
Key Terms
Related
to
Concurrency
Principles of Concurrency
• Interleaving and overlapping
• can be viewed as examples of concurrent processing
• both present the same problems
Difficulties of Concurrency
• Sharing of global resources
• Difficult for the OS to manage the
allocation of resources optimally
• Difficult to locate programming errors as
results are not deterministic and
reproducible
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
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
Illustration of Mutual Exclusion
– Interrupt Disabling
– uniprocessor system
– disabling interrupts guarantees mutual exclusion
– Disadvantages:
– the efficiency of execution could be noticeably
degraded
– this approach will not work in a multiprocessor
architecture
Semaphore 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
Binary Semaphore A semaphore that takes on only the values 0 and 1.
Mutex 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).
Common
Condition Variable A data type that is used to block a process or thread until a particular Concurrency
condition is true.
Monitor A programming language construct that encapsulates variables,
Mechanisms
access procedures and initialization code within an abstract data type.
The monitor's variable may only be accessed via its access
procedures and only one process may be actively accessing the
monitor at any one time. The access procedures are critical sections.
A monitor may have a queue of processes that are waiting to access
it.
Event Flags A memory word used as a synchronization mechanism. Application
code may associate a different event with each bit in a flag. A thread
can wait for either a single event or a combination of events by
checking one or multiple bits in the corresponding flag. The thread is
blocked until all of the required bits are set (AND) or until at least
one of the bits is set (OR).
Mailboxes/Messages A means for two processes to exchange information and that may be
used for synchronization.
Spinlocks Mutual exclusion mechanism in which a process executes in an
infinite loop waiting for the value of a lock variable to indicate
availability.
Semaphore
• A variable that has an integer value upon
which only three operations are defined:
– There is no way to inspect or manipulate
semaphores other than 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
A
Definition
of
Semaphore
Primitives
struct binary_semaphore {
enum {zero, one} value; A
queueType queue;
};
void semWaitB(binary_semaphore s)
Definition
{
if (s.value == one) of Binary
s.value = zero;
else {
/* place this process in s.queue */;
Semaphor
}
/* block this process */;
e
}
void semSignalB(semaphore s)
{
if (s.queue is empty())
s.value = one;
else {
/* remove a process P from s.queue */;
/* place process P on ready list */;
}
}
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
C issues semWait
Blocked queue Blocked queue
Processor
A C D B
Ready queue
s=0 2
B issues semWait
Blocked queue
Processor Processor
AC D D
Ready queue D issues semSignal Ready queue D issues semSignal
s = –1 3
s = –3 6
B C A B
Blocked queue Blocked queue
D issues semSignal, later times out
4 Processor
B AC D C D
Ready queue Processor Ready queue D issues semSignal
s=0 s = –2
7
A B
Blocked queue Blocked queue
Normal
semWait(lock)
execution
0
Blocked on
semWait(lock)
semaphore
B –1 lock
semWait(lock)
C B –2
semSignal(lock)
C –1
semSignal(lock)
semSignal(lock)
1
Note that normal
execution can
proceed in parallel
but that critical
regions are serialized.
0 1 2 3 4
out in
/* program producerconsumer */
semaphore n = 0, s = 1;
void producer() A Solution
{
while (true) { to the
produce();
semWait(s); Infinite-
append();
semSignal(s);
Buffer
semSignal(n); Producer/C
}
} onsumer
void consumer()
{ Problem
while (true) { Using
semWait(n);
semWait(s); Semaphores
take();
semSignal(s);
consume();
}
}
void main()
{
parbegin (producer, consumer);
}
b[1] b[2] b[3] b[4] b[5] b[n]
out in
(a)
in out
(b)
A Solution
to the
Bounded-
Buffer
Producer/
Consumer
Problem
Using
Semaphores
Implementation of Semaphores
• Imperative that the semWait and
semSignal operations be implemented as
atomic primitives
• Can be implemented in hardware or
firmware
• Software schemes such as Dekker’s or
Peterson’s algorithms can be used
• Use one of the hardware-supported
schemes for mutual exclusion
semWait(s) semWait(s)
{ {
while (compare_and_swap(s.flag, 0 , 1) == 1) inhibit interrupts;
/* do nothing */; s.count--;
s.count--; if (s.count < 0) {
if (s.count < 0) { /* place this process in s.queue */;
/* place this process in s.queue*/; /* block this process and allow interrupts */;
/* block this process (must also set s.flag to 0) }
*/; else
} allow interrupts;
s.flag = 0; }
}
semSignal(s)
semSignal(s) {
{ inhibit interrupts;
while (compare_and_swap(s.flag, 0 , 1) == 1) s.count++;
/* do nothing */; if (s.count <= 0) {
s.count++; /* remove a process P from s.queue */;
if (s.count <= 0) { /* place process P on ready list */;
/* remove a process P from s.queue */; }
/* place process P on ready list */; allow interrupts;
} }
s.flag = 0;
}
Monitor Characteristics
queue of
entering
processes
MONITOR
cwait(c1)
condition variables
Procedure 1
condition cn
cwait(cn)
Procedure k
urgent queue
csignal
initialization code
Exit
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
Design Characteristics of Message Systems for
Interprocess Communication and
Synchronization
Synchronization Format
Send Content
blocking Length
nonblocking fixed
Receive variable
blocking
nonblocking Queueing Discipline
test for arrival FIFO
Priority
Addressing
Direct
send
receive
explicit
implicit
Indirect
static
dynamic
ownership
Synchronization
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
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
S1
S1 Mailbox R1 Port R1
Sn
R1 S1 R1
S1 Mailbox Mailbox
Rm Sn Rm
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
A Solution to the
Readers/Writers
Problem Using
Semaphores:
Readers
Have
Priority
Both readers and writers with write first •wsem set by writer
•rsem set by writer
•writers queue on wsem
•one reader queues on rsem
•other readers queue on z
A Solution to the
Readers/Writers
Problem Using
Semaphores: Writers
Have Priority
Summary
• Semaphores
– Mutual exclusion
– Producer/consumer problem
– Implementation of semaphores
• Monitors
– Monitor with signal
– Alternate model of monitors with notify and broadcast
Summary
• Message passing
– Synchronization
– Addressing
– Message format
– Queueing discipline
– Mutual exclusion
• Readers/writers problem
– Readers have priority
– Writers have priority