Homework 3-4 Sarah Diesburg Operating Systems COP 4610

advertisement
Homework 3-4
Sarah Diesburg
Operating Systems
COP 4610
1. All Possible Execution Orders
Thread A
x = 3;
x = y - 1;
Thread B
y = 2;
y = x + 1;
(x = ?, y = ?)
(x = ?, y = 2)
y=2
(x = 3, y = ?)
x=3
(x = 3, y = 2)
(x = ?, y = ?)
x=y-1
(x = ?, y = 2)
y=2
y=2
(x = 1, y = 2)
x=y-1
y=x+1
(x = ?, y = ?)
y=x+1
(x = 1, y = 2)
x=3
(x = ?, y = ?)
y=x+1
(x = 3, y = 4)
(x = 3, y = ?)
x=3
y=x+1
x=y-1
x=y-1
(x = 3, y = 4)
(x = ?, y = ?)
2. Why does disabling interrupts not
work on multi-processors?


Interrupts are disabled on a per-CPU basis
A thread running on another CPU could enter
the critical area
3. Too Much Milk w/ Busy Waits
//define the busy-wait locks
value = 0;
Lock::Acquire() {
// while the previous value is BUSY, loop
while (test_and_set(value) == 1);
}
Lock::Release() {
value = 0;
}
3. (cont.)
//Dumb
//Dumber
Lock::Acquire(value)
if(no milk) {
//go get milk
Lock::Release(value)
Lock::Acquire(value)
if(no milk) {
//go get milk
Lock::Release(value)
4. Does this work?
// consumer waits on 0
semaphore nLoadedBuffers = 0;
// producer waits on 0
semaphore nFreeBuffers = N; // N >= 2
// one thread waits when another thread is
// modifying the buffer
semaphore mutex = 1;
Producer() {
1. P(nFreeBuffers);
2. P(mutex);
3. // put 1 item in the buffer
4. V(nLoadedBuffers);
5. V(mutex);
}
Consumer() {
6. P(nLoadedBuffers);
7. P(mutex);
8. // take 1 item from the buffer
9. V(mutex);
10. V(nFreeBuffers);
}
4. Does this work?
// consumer waits on 0
semaphore nLoadedBuffers = 0;
// producer waits on 0
semaphore nFreeBuffers = N; // N >= 2
// one thread waits when another thread is
// modifying the buffer
semaphore mutex = 1;
Producer() {
1. P(nFreeBuffers);
2. P(mutex);
3. // put 1 item in the buffer
4. V(nLoadedBuffers);
5. V(mutex);
}
Consumer() {
6. P(nLoadedBuffers);
7. P(mutex);
8. // take 1 item from the buffer
9. V(mutex);
10. V(nFreeBuffers);
}
4. Does this work?


Actually, yes.
Even if producer thread hits #4 and wakes up
a consumer sleeping on #6, the consumer
will not be able to enter the critical section
until the producer comes back and hits #5
Additional Things
Race Conditions – An anology

Suppose there is a bag of chips in the kitchen
at home and I want to make “walking tacos”
for dinner
Race Conditions – An anology


Suppose there is a bag of chips in the kitchen
at home and I want to make “walking tacos”
for dinner
If my husband gets home first, the bag of
chips will be consumed before dinner


My dinner plans are foiled!
If I get home first, I can use the bag of chips
for dinner
Download