Operating System Fundamentals



Download 2.34 Mb.
View original pdf
Page27/66
Date06.03.2023
Size2.34 Mb.
#60834
1   ...   23   24   25   26   27   28   29   30   ...   66
OperatingSystemFundamentals
best answers from c, Lesson 2 C# Windows Forms
Synchronization
We will start with a very simple task. Suppose that we have two threads (again, if they are in the same or different processes the same problems exist) as follows threadA()
{ system.out.print(“Thread A sysmtem.out.println();
} threadB()
{ system.out.print(“Thread B system.out.println();
} Suppose we were to start the threads at exactly the same time (this is not actually possibly by the way…why?). The result should be that both messages will appear in the output window. The question that has to arise however is which message appears first Does it look like this Thread A Thread B Does the result look like this Thread B Thread A Does the result look like this Thread A Thread B Unfortunately, with the information provided the output is actually unknown This type of solution is called non-deterministic because we cannot predictor determine) what will happen every time. In fact every time that we run the program we might see a different result. Why do we end up with so many possible results It looks like in the first example the first thread got to run completed, then the scheduler switched to the second thread. In the last

Operating System Fundamentals
38 example, the first thread got to run but was not completed (it did not make it to the println() function, then threadB ran and then threadA finished. Non-deterministic solutions are extremely dangerous in critical systems where peoples lives are at stake. Unfortunately we did not actually say what the desired output should actually look like. Let us suppose that we want the output to be in the order of Thread A followed by Thread B on the next line, and we want to be guaranteed of this order, regardless of what else possibly happens. This suggests that we need some mechanism of making sure threadB waits for threadA to finish. As always, we would like to try to minimize the amount of CPU time actually used. This first thing we will talk about is called synchronization.

Download 2.34 Mb.

Share with your friends:
1   ...   23   24   25   26   27   28   29   30   ...   66




The database is protected by copyright ©ininet.org 2024
send message

    Main page