The critical-section problem asks for a protocol that processes can follow so that they cooperate safely on shared data.
Structure of a Process
Each process has the form: an entry section, which asks permission to enter, the critical section, an exit section, which announces leaving, and the remainder section. The design problem is to write the entry and exit sections.
Definition: Mutual exclusion. The requirement that if one process is executing in its critical section, no other process may execute in its critical section at the same time.
Requirements of a Solution
A correct solution must satisfy three conditions. Mutual exclusion: at most one process is in its critical section at a time. Progress: if no process is in its critical section, and some want to enter, the choice of the next one cannot be postponed indefinitely, and only processes that wish to enter take part in the decision. Bounded waiting: there is a limit on the number of times other processes can enter their critical sections after a process has asked to enter and before it is allowed. No assumption may be made about the speed of the processes.
Software Solution: Peterson's Algorithm
For two processes, Peterson's solution uses two shared variables: a boolean array flag[2], where flag[i] = true means Pi wants to enter, and an integer turn. Process Pi executes: flag[i] = true; turn = j; while (flag[j] && turn == j) wait; then the critical section, then flag[i] = false. If both want to enter, the one that set turn last gives way. It satisfies all three requirements, though on modern processors it needs memory barriers to work.
Hardware Solutions
Disabling interrupts works on a single CPU but is unsafe and unsuitable for multiprocessors. Modern processors offer atomic instructions such as test-and-set and compare-and-swap, which read and change a memory word in one indivisible step. A lock built on them, entered by spinning until it is free, is a spinlock. Operating systems give programmers higher-level tools built on these: mutex locks, with acquire() and release(), semaphores and monitors.
Table 3.2: Requirements of a critical-section solution
| Requirement | Meaning | Violation example |
|---|---|---|
| Mutual exclusion | One process inside at a time | Both enter together |
| Progress | Decision not postponed for ever | Strict alternation blocks a willing process |
| Bounded waiting | Limited waiting before entry | One process starves |
Example 3.2: Why strict alternation fails
Problem: Two processes use a shared variable turn, and Pi enters only when turn == i, setting turn to the other on exit. Does this satisfy the requirements?
Solution:
- Mutual exclusion holds, since only the process whose turn it is can enter.
- Suppose P0 leaves and sets turn = 1, but P1 does not want to enter. P0 now wants to enter again but must wait for P1.
- A process outside its critical section is blocking another, so progress fails.
Result: Strict alternation gives mutual exclusion but violates progress.
#include <stdio.h>
#include <pthread.h>
long count = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *inc(void *a) {
for (int i = 0; i < 100000; i++) {
pthread_mutex_lock(&lock);
count++;
pthread_mutex_unlock(&lock);
}
return a;
}
int main(void) {
pthread_t t1, t2;
pthread_create(&t1, NULL, inc, NULL);
pthread_create(&t2, NULL, inc, NULL);
pthread_join(t1, NULL); pthread_join(t2, NULL);
printf("with a mutex: count = %ld (expected 200000)\n", count);
return 0;
}
Output
with a mutex: count = 200000 (expected 200000)
Common mistakes
- Checking only mutual exclusion and ignoring progress and bounded waiting.
- Disabling interrupts on a multiprocessor to protect a critical section.
- Forgetting to release a lock, which blocks every other process.
Key takeaways
- A process has entry, critical, exit and remainder sections.
- A solution needs mutual exclusion, progress and bounded waiting.
- Peterson's algorithm solves the problem for two processes.
- Atomic test-and-set and compare-and-swap build locks.
Practice questions
- State the three requirements of a critical-section solution.
- What variables does Peterson's solution use?
- What is a spinlock?
Answers to the practice questions
- Mutual exclusion, progress and bounded waiting.
- A boolean array flag[2] and an integer turn.
- A lock in which a waiting process loops repeatedly until the lock becomes free.
Sources
- Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit III
