Transcript Lecture 22
COP 4600 Operating Systems Fall 2010
Dan C. Marinescu
Office: HEC 439 B
Office hours: Tu-Th 3:30-4:30 PM
Lecture 22 – Thursday November 4, 2010
Last time:
Presentation of the paper “Programming with Threads” by Andrew Birell
Thread coordination with a bounded buffer.
Today:
Thread coordination with a bounded buffer.
WAIT
NOTIFY
AWAIT
ADVANCE
SEQUENCE
TICKET
Semaphores
Deadlock and Wait for Graphs
Enforced modularity on the Intel x86 architecture
Virtual Machines
Next Time
Scheduling Algorithms
Lecture 22
2
Coordination with bounded buffer
WAIT and NOTIFY can be used to coordinate the SEND and
RECIVE systems calls.
This solution creates problems when a NOTIFY is sent before the
WAIT. The thread in the WAITING state will never wake up.
Lecture 22
3
Read from
the buffer
location
pointed by
out
0
1
in
2
N-2 N-1
out
Write to
the buffer
location
pointed by
out
shared structure buffer
message instance message[N]
integer in initially 0
integer out initially 0
procedure SEND (buffer reference p, message instance msg)
while p.in – p.out = N do nothing
/* if buffer full wait
p.message [p.in modulo N] ßmsg
/* insert message into buffer cell
p.in ß p.in + 1
/* increment pointer to next free cell
procedure RECEIVE (buffer reference p)
while p.in = p.out do nothing
/* if buffer empty wait for message
msgß p.message [p.in modulo N] /* copy message from buffer cell
p.out ß p.out + 1
/* increment pointer to next message
return msg
Lecture 22
4
Lecture 22
5
AWAIT - ADVANCE solution
A new state, WAITING and two before-or-after actions that take a
RUNNING thread into the WAITING state and back to RUNNABLE state.
eventcount variables with an integer value shared between threads and
the thread manager; they are like events but have a value.
A thread in the WAITING state waits for a particular value of the
eventcount
AWAIT(eventcount,value)
If eventcount >value the control is returned to the thread calling AWAIT and this
thread will continue execution
If eventcount ≤value the state of the thread calling AWAIT is changed to WAITING
and the thread is suspended.
ADVANCE(eventcount)
increments the eventcount by one then
searches the thread_table for threads waiting for this eventcount
if it finds a thread and the eventcount exceeds the value the thread is waiting for then
the state of the thread is changed to RUNNABLE
Lecture 22
6
Implementation of AWAIT and ADVANCE
Lecture 22
7
Lecture 22
8
Solution for a single sender and multiple receivers
Lecture 22
9
Supporting multiple senders: the sequencer
Sequencer shared variable supporting thread sequence
coordination -it allows threads to be ordered and is manipulated
using two before-or-after actions.
TICKET(sequencer) returns a negative value which increases by
one at each call. Two concurrent threads calling TICKET on the
same sequencer will receive different values based upon the timing
of the call, the one calling first will receive a smaller value.
READ(sequencer) returns the current value of the sequencer
Lecture 22
10
Multiple sender solution; only the SEND must be
modified
Lecture 22
11
Semaphores
Introduced by Dijkstra in 1965
Does not require busy waiting
Semaphore S – integer variable
Two standard operations modify S: wait() and signal()
Originally called P() and V()
Less complicated
Can only be accessed via two indivisible (atomic) operations
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
Lecture 22
12
Lecture 22
13
Lecture 22
14
Simultaneous conditions for deadlock
Mutual exclusion: only one process at a time can use a resource.
Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes.
No preemption: a resource can be released only voluntarily by the process
holding it (presumably after that process has finished).
Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that
P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is
held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting
for a resource that is held by P0.
Lecture 22
15
Wait for graphs
Lecture 22
16
Evolution of modularity for the Intel architecture x86
Real and Virtual Address Space
The address space size determined by the number of address bits:
24 for 80286 a 16 bit processor modularity enforced through segmentation
32 for 80386 a 32 bit processor
each segment could have up to 232 bytes
within each segment support for virtual memory
Backward compatibility
Lecture 22
17
Lecture 22
18
Lecture 22
19
Lecture 22
Re
dH
Re
dH
s
NT
7.
1
s
8)
98
XP
Vi
st
a
s
00
1)
(2
00
0)
(2
8)
)
95
(1
99
W
in
do
w
at
Li
nu
x
6.
2
5.
0
in
do
w
W
s
(1
99
(1
99
2
99
2)
(1
in
do
w
W
at
Li
nu
x
do
w
W
in
NT
3.
1
So
la
ris
s
s
in
do
w
W
in
do
w
W
The increase in the number of lines of operating
systems source code (millions)
60
50
40
30
20
10
0
20
Virtual machines
First commercial product IBM VM 370 originally developed as CP-67
Advantages:
One could run multiple guest operating systems on the same machine
An error in one guest operating system does not bring the machine down
An ideal environment for developing operating systems
Word
Internet
Explorer
Firefox
X Windows
User Mode
Windows
GNU/Linux
VM Monitor
Lecture 22
Kernel Mode
21