Suggested Exercise 3 Sarah Diesburg Operating Systems CS
Suggested Exercise #3 Sarah Diesburg Operating Systems CS 3430
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 (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 = 3, y = 4) (x = ? , y = ? )
2. Why does disabling interrupts not work on multi-processors? n n 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. ) //Dumber Lock: : Acquire(value) if(no milk) { //go get milk Lock: : Release(value)
4. Does this work? // consumer waits on 0 semaphore n. Loaded. Buffers = 0; // producer waits on 0 semaphore n. Free. Buffers = N; // N >= 2 // one thread waits when another thread is // modifying the buffer semaphore mutex = 1; Producer() { 1. P(n. Free. Buffers); 2. P(mutex); 3. // put 1 item in the buffer 4. V(n. Loaded. Buffers); 5. V(mutex); } Consumer() { 6. P(n. Loaded. Buffers); 7. P(mutex); 8. // take 1 item from the buffer 9. V(mutex); 10. V(n. Free. Buffers); }
4. Does this work? // consumer waits on 0 semaphore n. Loaded. Buffers = 0; // producer waits on 0 semaphore n. Free. Buffers = N; // N >= 2 // one thread waits when another thread is // modifying the buffer semaphore mutex = 1; Producer() { 1. P(n. Free. Buffers); 2. P(mutex); 3. // put 1 item in the buffer 4. V(n. Loaded. Buffers); 5. V(mutex); } Consumer() { 6. P(n. Loaded. Buffers); 7. P(mutex); 8. // take 1 item from the buffer 9. V(mutex); 10. V(n. Free. Buffers); }
4. Does this work? n n 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
- Slides: 8