Lec 1 - Concurrency Mutual Exclusion and Synchronization
Lec 1 - Concurrency Mutual Exclusion and Synchronization
Systems: Chapter 5
Internals Concurrency:
and Design
Principles Mutual Exclusion
and Synchronization
Eighth Edition
By William Stallings
OperatingSystem design is concerned
with the management of processes and
threads:
Multiprogramming
Multiprocessing
Distributed Processing
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
Table 5.1
Some Key
Terms
Related
to
Concurrency
Interleaving and overlapping
can be viewed as examples of concurrent processing
both present the same problems
There is no way to
There is no way to know which process You don’t know
know before a will continue whether another
process decrements immediately on a process is waiting so
a semaphore uniprocessor system the number of
whether it will when two processes unblocked processes
block or not are running may be zero or one
concurrently
Figure 5.3
A
Definition
of
Semaphore
Primitives
st r uct binary_semaphore {
enum {zero, one} value;
queueType queue;
};
voi d semWaitB(binary_semaphore s)
{
i f ( s.value == one)
s.value = zero;
el se {
/* place this process in s.queue */;
/* block this process */;
}
}
voi d semSignalB(semaphore s)
{
i f (s.queue is empty())
s.value = one;
el se {
/* remove a process P from s.queue */;
/* place process P on ready list */;
}
}
Figure 5.4
A Definition of Binary Semaphore Primitives
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
A issues semWait, later times out
1 Processor
C D B A D BA C
Ready queue Processor Ready queue
s= 1 s= 0 5
C issues semWait
Blocked queue Blocked queue
Processor
A CD 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.
out in
A Correct
Solution to the
Infinite-Buffer
Producer/Cons
umer Problem
Using Binary
Semaphores
/* pr ogr am producerconsumer */
semaphore n = 0, s = 1;
voi d producer()
Figure 5.11 {
whi l e (true) {
produce();
A Solution semWait(s);
append();
to the semSignal(s);
Infinite- semSignal(n);
Buffer }
}
Producer/C voi d consumer()
onsumer {
whi l e (true) {
Problem semWait(n);
Using semWait(s);
Semaphores take();
semSignal(s);
consume();
}
}
voi d main()
{
par begi n (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/Cons
umer Problem
Using
Semaphores
Implementation of
Semaphores
Imperativethat 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
Useone of the hardware-supported
schemes for mutual exclusion
Figure 5.14
Two Possible Implementations of Semaphores
semWait(s) semWait(s)
{ {
whi l e ( compare_and_swap(s.flag, 0 , 1) == 1) inhibit interrupts;
/* do nothing */; s.count--;
s.count--; i f (s.count < 0) {
i f (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) }
*/; el se
} allow interrupts;
s.flag = 0; }
}
semSignal(s)
semSignal(s) {
{ inhibit interrupts;
whi l e ( compare_and_swap(s.flag, 0 , 1) == 1) s.count++;
/* do nothing */; i f (s.count <= 0) {
s.count++; /* remove a process P from s.queue */;
i f (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;
}
M ONI TOR
cwait(c1)
condition variables
Procedure 1
condition cn
cwait(cn)
Procedure k
urgent queue
csignal
initialization code
Exit
A Solution to the
Bounded-Buffer
Producer/Consu
mer Problem
Using a Monitor
When processes interact with one another two
fundamental requirements must be satisfied:
synchronization communication
Table 5.5
Design Characteristics of Message Systems for
Interprocess Communication and Synchronization
Bothsender 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
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 M ailbox R1 Port R1
Sn
R1 S1 R1
S1 M ailbox M ailbox
Rm Sn Rm
A Solution to
the
Readers/Write
rs Problem
Using
Semaphores:
Readers Have
Priority
Readers only in the system •wsem set
•no queues
Both readers and writers with read first •wsem set by reader
•rsem set by writer
•all writers queue on wsem
•one reader queues on rsem
•other readers queue on z
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
Table 5.6
State of the Process Queues for Program of Figure 5.23
Figure 5.23
A Solution to the
Readers/Writers
Problem Using
Semaphores:
Writers Have
Priority
voi d reader(int i) voi d controller()
{ {
message rmsg; whi l e (true)
whi l e (true) { {
rmsg = i; i f (count > 0) {
send (readrequest, rmsg); i f ( ! empty (finished)) {
receive (mbox[i], rmsg); receive (finished, msg);
READUNIT (); count++;
rmsg = i; }
send (finished, rmsg); el se i f ( !empty (writerequest)) {
} receive (writerequest, msg);
} writer_id = msg.id;
voi d writer(int j) count = count – 100;
{ }
message rmsg; el se i f ( !empty (readrequest)) {
whi l e(true) { receive (readrequest, msg);
rmsg = j; count--;
send (writerequest, rmsg); send (msg.id, "OK");
receive (mbox[j], rmsg); }
WRITEUNIT (); }
rmsg = j; i f (count == 0) {
send (finished, rmsg); send (writer_id, "OK");
} receive (finished, msg);
} count = 100;
}
whi l e (count < 0) {
receive (finished, msg);
count++;
}
}
}
Figure 5.24