xxyzz / ostep-hw

Operating Systems: Three Easy Pieces(OSTEP) homework and project solutions
GNU General Public License v3.0
763 stars 179 forks source link

Question 4/6 #1

Closed goldshakil closed 5 years ago

goldshakil commented 5 years ago

Hello Sir,

Sir I am currently learning OS in college and while I was doing this homework I stumbled upon the difference between question 4 and 6 (Chapter 30 / conditional variables)

I don't understand why having a sleep at c3 is different than having it at c6. In both cases the lock is released.

I would really appreciate if you can provide me with some explanation.

Regards

xxyzz commented 5 years ago

My previous answer is confusing... Sleep at c3 is slower because it runs after the thread getting the full signal and reacquiring the lock, but sleep at c6 doesn't slow down the program because it "runs" after the thread sleeps. Thank you for pointing out my mistake!

goldshakil commented 5 years ago

I am really sorry to bother you,I just want to double check if I got your point. a) in case of sleeping at c3;the running consumer thread will release the lock and wait for the "fill" signal to wake it up. After it gets signaled by the producer, c3 will force the thread to go to sleep state while the consumer still has the lock. That's why the scheduler can't make another consumer thread run because the lock is still held by the sleeping consumer. So, the program will stop for 1 second.

b) in case of sleeping at c6; the running consumer thread will release the lock by calling unlock. Then at c6the thread will be put to sleep. After that the scheduler will try to schedule another consumer which is legal in this case since the new consumer can acquire the lock and continue without waiting for the sleeping consumer.

Am I right?

Thank you so so much

xxyzz commented 5 years ago

Exactly! You can also add a printf() after the sleep() inside do_pause() function(main-header.h) to see when the sleep actually occurs.

goldshakil commented 5 years ago

Extremely thankful Sir. Really helpful