Lecture 05 - Concurrency & Mutual Exclusion
Lecture 05 - Concurrency & Mutual Exclusion
Process P1 Process P2
. .
chin = getchar(); .
. chin = getchar();
chout = chin; chout = chin;
putchar(chout); .
. putchar(chout);
. .
• on Multiptocessor system
• same problem arise
• character input to P1 is lost before being displayed
and the character input to P2 is displayed by both P1 and P2
• same solution works
Dr. Noha Adly Operating Systems – CS x61 6
Enforce Single Access
16
Dr. Noha Adly Operating Systems – CS x61
Requirements for Mutual Exclusion
Any facility or capability that is to provide support for mutual
exclusion should meet the following requirements:
1. Mutual exclusion must be enforced: Only one process at a time
is allowed into its critical section, among all processes that
have critical sections for same resource or shared object
2. A process that halts in its noncritical section must do so
without interfering with other processes.
3. Must not be possible for a process requiring access to a critical
section to be delayed indefinitely: no deadlock or starvation
4. When no process is in a critical section, any process requests
entry to its critical section must be permitted without delay
5. No assumptions are made about relative process speeds or
number of processors
6. A process remains in its critical section for a finite time only
Dr. Noha Adly Operating Systems – CS x61
Hardware Solutions: Disabling Interrupts
Preventing a process from being interrupted guarantees
mutual exclusion
shared double balance;
Disadvantages
Interrupts could be disabled arbitrarily long
A user process can easily abuse this privilege and hence should not be
available to a user process.
We only want to prevent p1 and p2 from interfering with one another;
this prevents any other process pk to execute
Does not work in a Multiprocessor system: disabling interrupts in one
processor will not disable it in another process and hence mutual
exclusion is not guaranteed
18
Dr. Noha Adly Operating Systems – CS x61
Mutual Exclusion: Hardware Support
Many systems provide hardware support for implementing the
critical section code.
All solutions below based on idea of locking: Protecting critical
regions via locks
do{
acquire lock
critical section
release lock
remainder section
} while (TRUE);
acquire() {
while (!available) do {
; /* busy wait */ acquire lock
available = false;; critical section
} release lock
release() { remainder section
available = true; } while (true);
}
Dr. Noha Adly Operating Systems – CS x61
acquire() and release()
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
Dr. Noha Adly Operating Systems – CS x61
Definition of Semaphore Primitives
Processes
A, B, and
C depend
on a result
from
process D
Producer: Consumer:
while (true) { while (true) {
/* produce item v */ while (in <= out) /* wait */;
b[in] = v; w = b[out];
in++; out++;
} /* consume item w */
}
Infinite Buffer
n # items in buffer
= in - out
s : enforce mutual
exclusion
delay: force consumer
to semWait if the
buffer is empty
There is a flaw in
this program!
buffer is treated as a
circular storage
Producer: Consumer:
while (true) { while (true) {
/* produce item v */ while (in == out)
/* do nothing */;
while((in + 1)%n==out)
/* do nothing */; w = b[out];
out = (out + 1) % n;
b[in] = v;
in = (in + 1) % n /* consume item w */
} }
condition x, y;
Two operations are allowed on a condition variable:
x.wait() – a process that invokes the operation is suspended
until x.signal()-
x.signal() – resumes one of processes (if any) that invoked
x.wait()
Ifno x.wait() on the variable, then it has no effect on the
variable
Note that monitor wait and signal operations are different from those
for the semaphore. If a process in a monitor signals and no task
is waiting on the condition variable, the signal is lost.
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
- A signaling process must wait until the resumed process either leaves
or waits – the process can use next to suspend itself
» Solaris
» Windows
» Linux
» Pthreads
Shared
Pipes Messages
memory
Semaphores Signals