Download Communicating Sequential Processes
Transcript
7.2 Shared storage 211 The other processes must not read the variable until the updating has taken place. Similarly, the first process must not update the variable again until all the other processes have read the earlier updating. To solve this problem, a convenient facility is offered by the conditional critical region. This takes the form with sharedvar when condition do critical region On entry to the critical region, the value of the condition is tested. If it is true, the critical region is executed normally. But if the condition is false, this entry into the critical region is postponed, so that other processes are permitted to enter their critical regions and update the shared variable. On completion of each such update, the condition is retested. If it has become true, the delayed process is permitted to proceed with its critical region; otherwise that process is suspended again. If more than one delayed process can proceed, the choice between them is arbitrary. To solve the problem of updating and reading a message by many processes, declare as part of the resource an integer variable to count the number of processes that must read the message before it is updated again shared message : record count : integer ; content : . . . end message.count := 0; The updating process contains a critical region with message when count = 0 do begin content := . . . ; ...; count := number of readers end Each reading process contains a critical region with message when count > 0 do begin my copy := content ; count = count − 1 end Conditional critical regions may be implemented by means of semaphores. Compared with direct use of synchronisation semaphores by the programmer, the overhead of conditional critical regions may be quite high, since the conditions of all processes waiting to enter the region must be retested on every exit from the region. Fortunately, the conditions do not have to be retested more frequently than that, because restrictions on access to shared variables ensure that the condition tested by a waiting process can change value only when the shared variable itself changes value. All other variables in the condition must