Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
and Synchronization
Chapter 5
Currency
Concurrency
Multiple applications
Multiprogramming
Structured application
Application can be a set of concurrent
processes
Operating-system structure
Operating system is a set of processes or
threads
A Simple Example
void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}
A Simple Example
Process P1
.
in = getchar();
.
chout = chin;
putchar(chout);
.
.
Process P2
.
.
in = getchar();
chout = chin;
.
putchar(chout);
.
Processor time
Memory
Files
I/O devices
Process Interaction
Processes unaware of each other
Processes indirectly aware of each other
Process directly aware of each other
Competition Among
Processes for Resources
Mutual Exclusion
Critical sections
Only one program at a time is allowed in its
critical section
Example only one process at a time is allowed
to send command to the printer
Deadlock
Starvation
First Attempt
Busy Waiting
Process is always checking to see if it can
enter the critical section
Process can do nothing productive until it
gets permission to enter its critical section
Coroutine
Designed to be able to pass execution
control back and forth between
themselves
Inadequate to support concurrent
processing
Second Attempt
Each process can examine the others status but
cannot alter it
When a process wants to enter the critical
section is checks the other processes first
If no other process is in the critical section, it
sets its status for the critical section
This method does not guarantee mutual
exclusion
Each process can check the flags and then
proceed to enter the critical section at the same
time
Third Attempt
Set flag to enter critical section before check
other processes
If another process is in the critical section
when the flag is set, the process is blocked
until the other process releases the critical
section
Deadlock is possible when two process set
their flags to enter the critical section. Now
each process must wait for the other process to
release the critical section
Fourth Attempt
A process sets its flag to indicate its
desire to enter its critical section but is
prepared to reset the flag
Other processes are checked. If they are
in the critical region, the flag is reset and
later set to indicate desire to enter the
critical region. This is repeated until the
process can enter the critical region.
Fourth Attempt
It is possible for each process to set their
flag, check other processes, and reset
their flags. This scenario will not last
very long so it is not deadlock. It is
undesirable
Correct Solution
Each process gets a turn at the critical
section
If a process wants the critical section, it
sets its flag and may have to wait for its
turn
Mutual Exclusion:
Hardware Support
Interrupt Disabling
A process runs until it invokes an operatingsystem service or until it is interrupted
Disabling interrupts guarantees mutual
exclusion
Processor is limited in its ability to
interleave programs
Multiprocessing
disabling interrupts on one processor will
not guarantee mutual exclusion
Mutual Exclusion:
Hardware Support
Special Machine Instructions
Performed in a single instruction cycle
Not subject to interference from other
instructions
Reading and writing
Reading and testing
Mutual Exclusion:
Hardware Support
Test and Set Instruction
boolean testset (int i) {
if (i == 0) {
i = 1;
return true;
}
else {
return false;
}
}
Mutual Exclusion:
Hardware Support
Exchange Instruction
void exchange(int register,
int memory) {
int temp;
temp = memory;
memory = register;
register = temp;
}
Semaphores
Special variable called a semaphore is
used for signaling
If a process is waiting for a signal, it is
suspended until that signal is sent
Wait and signal operations cannot be
interrupted
Queue is used to hold processes waiting
on the semaphore
Semaphores
Semaphore is a variable that has an
integer value
May be initialized to a nonnegative number
Wait operation decrements the semaphore
value
Signal operation increments semaphore
value
Producer/Consumer Problem
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
Producer
producer:
while (true) {
/* produce item v */
b[in] = v;
in++;
}
Consumer
consumer:
while (true) {
while (in <= out)
/*do nothing */;
w = b[out];
out++;
/* consume item w */
}
Infinite Buffer
Barbershop Problem
Monitors
Monitor is a software module
Chief characteristics
Local data variables are accessible only by
the monitor
Process enters monitor by invoking one of
its procedures
Only one process may be executing in the
monitor at a time
Message Passing
Enforce mutual exclusion
Exchange information
send (destination, message)
receive (source, message)
Synchronization
Sender and receiver may or may not be
blocking (waiting for message)
Blocking send, blocking receive
Both sender and receiver are blocked until
message is delivered
Called a rendezvous
Synchronization
Nonblocking send, blocking receive
Sender continues processing such as
sending messages as quickly as possible
Receiver is blocked until the requested
message arrives
Addressing
Direct addressing
send primitive includes a specific identifier
of the destination process
receive primitive could know ahead of time
which process a message is expected
receive primitive could use source
parameter to return a value when the
receive operation has been performed
Addressing
Indirect addressing
messages are sent to a shared data structure
consisting of queues
queues are called mailboxes
one process sends a message to the mailbox
and the other process picks up the message
from the mailbox
Message Format
Readers/Writers Problem
Any number of readers may
simultaneously read the file
Only one writer at a time may write to
the file
If a writer is writing to the file, no reader
may read it