Homework-4 Version 0 |
|
👤 Author: by ikarososohucom 2019-09-28 13:45:31 |
Think about your possible case about process synchronization, and give out a solution for the problem.
Suppose a company have a public bank account and lots of people may take out or save money to this account at same time.
The rule is that if there is no enough money in the account, money cant be take out(No negative value in the account).
If there is no synchronization, this kind of situation may happen——Two people want to take out money at the same time. A wants to take out 100 yuan and B wants to take out 200 yuan. However, the current money in the account is 250 yuan. Since there is no synchronization, these two threads will check the account and find out that the money is enough for taking out. Then both of the threads take out the money and make the money in account becomes a negative number, which is -50 yuan.
We can implement a synchronization to this program so that this kind of situation will not happen. This is the solution——when a thread is trying to access some variable or check some variable, any other thread related to those varivables is locked and cant be executed. This will make sure that the current money vaule in the account wont be a negative number. Since all the threads want to be executed are lining up and waiting to be unlocked so that they can be executed.
It also means that the variable related to the thread is protected from any other thread, expect for the current executing thread.