- Request the resource: The process requests exclusive access to the resource (if it is occupied, the process must wait).
- Use the resource: The process enters the critical section and performs the required operations on the resource.
- Release the resource: The process frees the resource, allowing other waiting processes to acquire it.
Competition: Mutual Exclusion
An introduction to the fundamentals of concurrent programming: competition vs. cooperation, critical sections, mutual exclusion, deadlock, and starvation.
Introduction
Section titled “Introduction”Competition and Cooperation
Section titled “Competition and Cooperation”When multiple processes run in a computer system, they can interact in two main ways:
- Competition: Two or more processes need exclusive use of limited resources that they both need, and therefore compete to acquire them. For example, two processes may try to write to the same file at the same time.
- Cooperation: Processes do not compete, but must coordinate and exchange information to perform a common task (e.g., a producer process that puts data into a buffer and a consumer process that retrieves it).
In this lesson, we will focus on competition. Cooperation will be covered in a later lesson on the Producer–Consumer problem.
Competition: Shared resource, critical section, and mutual exclusion
Section titled “Competition: Shared resource, critical section, and mutual exclusion”When processes compete for access to common elements (such as global variables, files, or data structures), any reusable hardware or software element contested by processes is called a shared resource.
Shared Resource Access Diagram
Section titled “Shared Resource Access Diagram”The diagram below shows how two separate processes (Process P1 and
Process P2) execute their code and must pass through their respective critical sections to interact with the shared resource R:
Diagram created with Mermaid
Why Mutual Exclusion Is Needed
Section titled “Why Mutual Exclusion Is Needed”To preserve data consistency and prevent race conditions (situations in which the execution order unpredictably changes the result), the system must guarantee mutual exclusion:
The Access Protocol (Request – Use – Release)
Section titled “The Access Protocol (Request – Use – Release)”To guarantee mutual exclusion and ensure that every process gains access in finite time, access to a resource must strictly follow a standard protocol consisting of three stages:
Practical Example: The Stack
Section titled “Practical Example: The Stack”One classic shared data structure subject to competition is a stack, managed using an array and a top pointer (top).
Operations such as push (Push) and pop (Pop) modify the same shared variables (stack[] and top). If these operations run concurrently without proper synchronization, data may be overwritten or undefined values may be read (race condition).
Summary
Section titled “Summary”Knowledge Check
Section titled “Knowledge Check”Quiz answers on this page are saved in your browser's local storage and remain saved between visits
What is a critical section?
- A resource shared by multiple processes.
- The portion of code that accesses a shared resource.
Correct: it is the code in which a process reads or modifies the shared resource.
- A process that waits indefinitely for a resource.
Which property does mutual exclusion guarantee?
- At most one process at a time can access the shared resource in its critical section.
Correct: this prevents concurrent accesses from compromising data consistency.
- All processes access the resource simultaneously.
- A waiting process always gets the resource within a finite time.
This is a progress guarantee, not the definition of mutual exclusion.
What is the correct order of the stages in the shared-resource access protocol?
- Use, request, release.
- Request, use, release.
Correct: the process requests the resource, uses it in the critical section, and then releases it.
- Release, use, request.
The portion of code that accesses a shared resource is the [[critical section]].
Which of the following can be examples of a shared resource? Select all correct answers.
- A global variable
Correct: multiple processes can access the same variable.
- A file used by multiple processes
Correct: the file is a common resource that processes may contend for.
- A shared data structure
Correct: different processes can read or modify the same structure.
- A process’s private code that does not interact with other processes
This is not a shared resource.
Quiz progress
0 / 0 questions answered (0%)
0 correct answers
Your score
0%
0 / 0 correct answers
Answered: 0 / 0 (0%)
STICS © 2026 by Carlo Scocco is licensed under CC BY-NC 4.0