The critical-section problem
Entry/exit structure and the three requirements.
Operating Systems: The Critical-Section Problem
1. Problem Formulation
Consider a system consisting of n processes {P0, P1, …, Pn−1}. Each process has a segment of code called a Critical Section (CS), in which the process may be updating shared variables, updating a table, writing a file, or modifying shared memory.
The Problem: When one process is executing in its critical section, no other process is allowed to execute in its critical section simultaneously. The critical-section problem is to design a protocol that processes can use to cooperate safely without data corruption or execution deadlocks.
2. Structure of a Typical Process (Pi)
To control access, each process must request permission to enter its critical section. The general execution loop of a process Pi is partitioned into four explicit code sections:
// Structure of a typical process Pi do { entry section critical section exit section remainder section} while (true);
3. Requirements for a Valid CS Solution
A valid solution to the critical-section problem must satisfy the following three fundamental criteria under all execution orderings:
If process Pi is executing in its critical section, then no other processes can be executing in their critical sections.
If no process is executing in its critical section and some processes wish to enter, only those processes not executing in their remainder section can participate in deciding which process will enter next. This selection cannot be postponed indefinitely.
There must be a bound or limit on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter and before that request is granted.
4. Interactive Critical Section Protocol Simulator
Experiment below by requesting entry for Process 0 (P0) and Process 1 (P1). Switch between **Peterson's Algorithm** (enforces all 3 rules) and **Unsynchronized Mode** (causes race conditions).