Operating System Fundamentals 42 single line with a space between each. The behavior of the program is really quite simple. However, if we now launch both threads at the same time we might end up with the scheduler switching back and forth several times. The result could look something like this Thread A is finished 0
Thread Bis finished 0 1 2 1 2 3 4 5 6 7
3 4 5 6 7 8 8 9 9 We have marked the output from threadB using italics so that you can see what is happening. The problem is that the scheduler keeps switching back and forth between the two threads while they are printing to the screen. This is quite common. Most operating systems will take an IO request as a chance to reschedule the threads or processes. Unfortunately, there are no actual guarantees of when the rescheduling will occur. Our goal for this small program is to make sure that the output does not get mixed up. The problem is that all of the output statements in our threads
are part of a critical section, which means that we do not want the other thread interfering while we are trying to write to the screen.
Share with your friends: