A critical section is the part of a process where shared data or a shared resource is accessed or modified. If two processes enter that sensitive part at the same time, the final result can become wrong because their operations may interfere with each other. Mutual exclusion is the rule that allows only one process at a time to execute the critical section for a particular shared resource. This chapter explains race condition, critical-section structure, entry and exit sections, the three requirements of a correct solution, locks, busy waiting, atomic operations, mutexes, semaphores and simple solved examples so you can understand why synchronization is needed before studying Peterson's solution, semaphores and classical concurrency problems.
What Is a Critical Section in Operating System?
A critical section is a part of a program where a process or thread accesses shared data or a shared resource that must not be modified by multiple executions at the same time.
Suppose two processes are updating the same bank balance. Both processes read and write the same shared value. If their operations overlap in the wrong order, the final balance can become incorrect.
This is why the operating system needs synchronization.
If you are not clear about processes and concurrency yet, first revise Process in Operating System: Process Concept, Concurrency & Process Generation.
Where Is the Critical Section?
SECTION
SECTION
The entry section decides whether the process is allowed to enter. The exit section releases the permission or lock. The remainder section contains normal work that does not require exclusive access to the shared resource.
What Is a Race Condition?
A race condition happens when the final result depends on the timing or order in which concurrent processes or threads execute shared-data operations.
Consider a shared counter:
Process P1 and Process P2 both want to increase it by 1. A simple increment is not always one indivisible step. Conceptually, it may happen as read, add and write.
The expected result was 7, but the result may become 6 because both processes used the same old value.
What Is Mutual Exclusion?
Mutual exclusion means that only one process or thread at a time is allowed to execute the critical section for the same shared resource.
If P1 is inside the critical section, P2 must wait until P1 leaves. After P1 exits, another waiting process can enter.
Process P1
Requests access to shared counter.
AT A
TIME
Process P2
Waits while P1 is inside the protected section.
Entry Section, Critical Section, Exit Section & Remainder Section
A common textbook form is:
| Part | Purpose |
|---|---|
| Entry Section | Checks or waits until the process can safely enter |
| Critical Section | Reads or changes shared data/resource |
| Exit Section | Releases lock/permission after shared work finishes |
| Remainder Section | Executes code unrelated to the protected shared resource |
Requirements of a Correct Critical-Section Solution
A good solution to the critical-section problem should satisfy three important conditions.
Mutual Exclusion Explained With an Example
Suppose P1 and P2 both want to update a shared file.
Correct behavior
P1 enters first. P2 waits. P1 updates the file and exits. Then P2 enters.
Incorrect behavior
P1 and P2 update the shared file at the same time and the final file state becomes unpredictable.
Mutual exclusion prevents the second situation.
What Does Progress Mean?
Progress means that if the critical section is free and one or more processes want to enter, the system should eventually decide which waiting process enters next.
A process that is not interested in entering the critical section should not unnecessarily block others forever.
Progress prevents the system from getting stuck even though the shared resource is available.
What Is Bounded Waiting?
Bounded waiting means a process should not wait forever after requesting access to the critical section.
There must be some limit on how many times other processes are allowed to enter before the waiting process gets its chance.
This requirement helps prevent starvation.
What Is a Lock?
A lock is a synchronization mechanism used to control access to a shared resource.
A simple lock has two basic ideas:
Acquire / Lock
Process requests permission before entering the critical section.
Release / Unlock
Process releases permission after finishing the shared operation.
Conceptually:
Why a Normal Shared Boolean Lock Can Fail
Students sometimes think this is enough:
But if checking the lock and setting the lock are separate operations, two processes can both see “free” before either one changes it.
The solution is to use an atomic operation or a correct synchronization algorithm so the “check and change” happens safely.
What Does Atomic Operation Mean?
An atomic operation is treated as one indivisible action: other processes cannot observe it half-completed.
Hardware-supported atomic operations are often used to build locks. Examples include test-and-set and compare-and-swap style operations.
At this stage, remember the main idea: if acquiring the lock is atomic, two processes cannot both successfully acquire the same simple lock at the exact same moment.
What Is Busy Waiting?
Busy waiting happens when a process repeatedly checks a condition while waiting instead of giving up the CPU.
For example, a process may keep checking whether a lock becomes free:
This wastes CPU time if the waiting period is long. A lock that waits by repeatedly checking is often called a spinlock.
When can spinning still be useful?
If the expected wait is extremely short, especially on a multiprocessor system, sleeping and waking a thread may cost more than briefly spinning. But for longer waits, blocking is usually more efficient.
What Is a Mutex?
A mutex is a mutual-exclusion synchronization object used to ensure that only one thread or process at a time owns access to a protected critical section.
Conceptually, a thread locks the mutex before entering the critical section and unlocks it after leaving.
Mutex free
A process can acquire it and enter the critical section.
Mutex owned
Other processes or threads must wait according to the mutex implementation.
Mutexes are one practical way to provide mutual exclusion. Semaphores, monitors and hardware atomic instructions are other synchronization tools.
How Are Semaphores Related to Mutual Exclusion?
A binary semaphore can be used to protect a critical section in a way similar to a mutex.
Semaphores are more general than simple mutual exclusion and will be studied separately because they can also coordinate ordering between processes.
The next article should cover Semaphores in OS: Wait, Signal, Test-and-Set & Synchronization in detail.
Can Mutual Exclusion Be Solved Without Special Hardware?
Yes. Classical software algorithms were designed to solve the critical-section problem using shared variables.
You do not need the full Peterson or Dekker algorithm inside this chapter. They deserve separate step-by-step articles because students usually need solved execution examples.
How Does Hardware Help Mutual Exclusion?
Processors provide atomic instructions that operating systems can use to build safe locks.
Test-and-Set
Atomically reads a lock value and changes it in one indivisible operation.
Compare-and-Swap
Atomically changes a value only if it currently matches an expected value.
On a single-processor kernel, temporarily controlling interrupts can also protect very short kernel critical sections, but this is not a general user-process solution and does not directly solve synchronization across multiple processors.
See the Race Condition With and Without Mutual Exclusion
Two Processes Increment the Same Counter
Result
Does Mutual Exclusion Solve Every Concurrency Problem?
No. Mutual exclusion solves unsafe simultaneous entry into a critical section, but poor synchronization design can still create deadlock, starvation or unnecessary waiting.
This is why a complete synchronization solution needs more than just a lock; it also needs fairness, correct ordering and careful resource design.
Where Do We See Critical Sections in Real Systems?
Critical Section, Race Condition and Mutual Exclusion: Difference
| Term | Meaning | Easy Memory Line |
|---|---|---|
| Critical Section | Code that accesses shared data/resource | Sensitive code area |
| Race Condition | Wrong/unpredictable result caused by unsafe execution order | Timing changes result |
| Mutual Exclusion | Only one execution enters the critical section at a time | One at a time |
| Synchronization | General coordination of concurrent processes/threads | Control the order safely |
Common Student Confusions to Avoid
How to Write Critical Section & Mutual Exclusion in an Exam
Definition answer
Define critical section and mutual exclusion in one or two clear lines.
Critical-section problem
Explain race condition, entry/critical/exit/remainder sections and the three requirements.
Complete answer
Add solved counter example, mutual exclusion, atomic operations, busy waiting, mutex/semaphore connection and a diagram.
Fast exam memory trick
M P B = Mutual Exclusion, Progress, Bounded WaitingPractice Questions With Short Answers
1. What is a critical section?
A critical section is the part of a program that accesses or modifies shared data or a shared resource.
2. What is mutual exclusion?
Mutual exclusion means only one process or thread at a time can execute the critical section for the same shared resource.
3. What is a race condition?
A race condition occurs when the final result depends on the timing or order of concurrent operations on shared data.
4. What are the four parts around a critical section?
Entry section, critical section, exit section and remainder section.
5. What are the three requirements of a correct critical-section solution?
Mutual exclusion, progress and bounded waiting.
6. What is bounded waiting?
Bounded waiting means a process should not be forced to wait forever after requesting entry to the critical section.
7. What is an atomic operation?
An atomic operation is an indivisible action that other processes cannot observe in a half-completed state.
8. What is busy waiting?
Busy waiting happens when a process repeatedly checks a condition while waiting instead of blocking or giving up the CPU.
9. What is a mutex?
A mutex is a synchronization object used to provide mutual exclusion for a protected critical section.
10. Is mutual exclusion enough to solve every synchronization problem?
No. Correct concurrent systems must also consider progress, bounded waiting, deadlock, starvation and ordering requirements.
One-Minute Revision Sheet
Final memory line
Critical section is the risky area; mutual exclusion is the one-at-a-time rule that protects it.Frequently Asked Questions
What is a critical section in OS in simple words?
A critical section is the part of a program where shared data or a shared resource is accessed and therefore needs protection from unsafe simultaneous access.
What is mutual exclusion in Operating System?
Mutual exclusion is the rule that only one process or thread at a time can execute the critical section for the same shared resource.
What are the three conditions for solving the critical-section problem?
The three conditions are mutual exclusion, progress and bounded waiting.
What is the difference between a race condition and a critical section?
A critical section is the sensitive code area that accesses shared data, while a race condition is an incorrect or unpredictable result caused by unsafe concurrent execution.
What is busy waiting?
Busy waiting is when a process repeatedly checks whether a condition has changed instead of blocking and giving up the CPU.
Why is an atomic operation important for locks?
An atomic operation prevents two processes from both observing and changing the same lock state at the same time, which helps build safe mutual exclusion.
What Should You Learn Next?
Next: Semaphores in Operating System
Now that you understand critical sections and mutual exclusion, the next topic should be Semaphores in OS: Wait, Signal, Test-and-Set & Synchronization Explained.
That chapter will show how operating systems actually coordinate processes using synchronization primitives. You can also revise Processes, Threads & Multiprocessor Systems if needed.
