Resource Sharing
Processes that share resources on a system can sometimes create problems which are sporadic, yet serious. Such problems are often hard to detect and remedy. Let us look at an example in which two processes share a variable.
v[10] = {1,2,3,4,5,6,7,8,9,10} count = 0 // process One for i <- 1 to 5 do if v[i] <> 0 then count <- count + 1; // process Two for j <- 6 to 10 do if v[j] <> 0 then count <- count + 1;
Looking at this code, you would probably assume that the value of count, after these processes are finished running, is 10. However, there is a strong possibility you would be wrong. Lets look at a likely format for the individual machine instructions that would be used to increment count.
load reg, count add reg, 1 store count, reg
In a multiprogramming system, these two processes might try to run at the same time, leading to a situation like this:
process One process Two 1. load reg1, count 2. load reg2, count 3. add reg2, 1 4. store count, reg2 5. add reg1, 1 6. store count, reg1
You can see that in this example, one increment is lost. You can also see that a problem like this is difficult to detect just by looking at the code. Some form of mutual exclusion is needed in order to prevent two processes from entering their Critical Sections at the same time.
IPC Home Subway Process Synch Page Map Interaction