Critical Section

 The key to preventing trouble involving shared storage is find some way to prohibit more than one process from reading and writing the shared data simultaneously. That part of the program where the shared memory is accessed is called the Critical Section. To avoid race conditions and flawed results, one must identify codes in Critical Sections  in each thread. The characteristic properties of the code that form a Critical Section are

  • Codes that reference one or more variables in a “read-update-write” fashion while any of those variables is possibly being altered by another thread.
  • Codes that alter one or more variables that are possibly being referenced in “read-updata-write” fashion by another thread.
  • Codes use a data structure while any part of it is possibly being altered by another thread.
  • Codes alter any part of a data structure while it is possibly in use by another thread.

Here, the important point is that when one process is executing shared modifiable data in its critical section, no other process is to be allowed to execute in its critical section. Thus, the execution of critical sections by the processes is mutually exclusive in time.

Critical Section

Mutual Exclusion

Achieving Mutual