The two atomic instructions while (turn!=0), and while (turn !=1); in thread 0 a
ID: 3841229 • Letter: T
Question
The two atomic instructions while (turn!=0), and while (turn !=1); in thread 0 and thread 1 each takes 100 mu s (100 microseconds). The two atomic instructions turn and turn = 1; and turn = 0; also each takes 100 mu s (100 microseconds) also. Critical region(); in both threads takes 1 ms (1 millisecond) each consisting of 4 atomic instructions cr1 cr2, cr3, and cr4 (none of these 4 can be stopped in the middle and switched to another thread). We also assume that each of crl, cr, 2 cr, 3 and cr4 takes the same amount of time or 1ms 4 250 mu s. Non_ciritcial_region(); in both threads take 2ms (two milliseconds) each consisting of 4 atomic instructions nc1 till nc4. We also assume each of nc1, nc2, nc3, and nc4 takes the same amount of time or 500 mu s. To clarify, one iteration in the while (TRUE) {...} of thread 0 running every instruction and function exactly once takes 100 mu s + 1ms 100 mu s + 2ms, or 3.3 ms. Suppose OS schedules by round robin of thread 0 for 2ms, thread 1 for 2ms, thread 0 for 2ms, and thread 1 for 2ms. Compute the real amount of time when OS moves between thread 0 and thread l each twice as above. Note the amount of time could be more than 8 ms since at the end of 2 ms, thread 0 may be in the middle of an atomic instruction. Show which instruction is being executed in thread 0 when thread 0 and thread l each has been executed twice with 2ms as the time of scheduling. Tabulate and show how much time is spent in each instruction of thread 0 in the first run and in the second run.Explanation / Answer
a) In the first part, thread 0 takes 2 ms and terminates after executing 300 ms of nc2. Then thread 1 takes 2 ms and terminates after 300 ms of nc2. Then thread 0 takes 2.05 ms as after 2 ms, cr3 will be executed for 200 us and since it is in the critical region, it cannot be swithced. Hence it will take 0.05 ms more. Then thread 1 will be executed and it will again take the same time of 2.05 ms. Hence total time would be 8.1 ms.
b) When both threads have been executed twice for 2 ms, cr3 is being executed in them.
c) Thread 0 First Run
while(turn!=0) 100us
cr1+cr2+cr3+cr4 1ms
turn=1 100us
nc1 500us
nc2 300us
Thread 0 Second Run
nc2 200us
nc3 500us
nc4 500us
while(turn!=0) 100us
cr1 250us
cr2 250us
cr3 250us
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.