Q: Describe your own case which encounters data inconsistency when processes share data, and write the method to solve the problem.
A: When shared resources are modified at the same time by multiple resources, data errors or inconsistencies may occur. Sections of a program that might cause these problems are called critical sections. Failure to coordinate access to a critical section is called a race condition because success or failure depends on the ability of one process to exit the critical section before another process enters the critical section. It is often the case that two processes are seldom in the critical section at the same time; but when there is overlap in accessing the critical section, the result is a disaster.
For example, the above picture shows a car and a bus are met in a cross road. We suppose the gray square in the middle indicates a vehicle counter and this counter is driven by a balance. It can automatically increase by one when the weight of balance changed. Normally, when a vehicle passed, the counter will increase by one. However, in this case, due to the existence of balance. When two vehicles are in the grey square concurrently, the counter will only add one, which cause the data inconsistency.
In this example, the grey square can indicate shared memory, the vehicle can indicate different process.
The solution is quite simple, we only need to add some traffic light to control each car go and stop. Switch to the OS concept, which means we can add
semaphore, which will take the same role as traffic light,.