Sarah Diesburg Operating Systems COP 4610 Homework 3-4 Sarah Diesburg Operating Systems COP 4610
1. All Possible Execution Orders Thread A Thread B x = 3; y = 2; x = y - 1; y = x + 1; (x = ?, y = ?) x = 3 y = 2 (x = ?, y = 2) (x = 3, y = ?) x = y - 1 y = 2 x = 3 y = x + 1 (x = 3, y = 2) (x = ?, y = ?) (x = ?, y = ?) y = 2 y = x + 1 x = y - 1 y = x + 1 x = 3 x = y - 1 (x = ?, y = 2) (x = 1, y = 2) (x = 3, y = 4) (x = 3, y = ?) (x = ?, y = ?) (x = 1, y = 2) (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() {
3. (cont.) //Dumb Lock::Acquire(value) if(no milk) { //go get milk Lock::Release(value) //Dumber 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