Skip to content

Competition: Mutual Exclusion

An introduction to the fundamentals of concurrent programming: competition vs. cooperation, critical sections, mutual exclusion, deadlock, and starvation.

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.

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.

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:

flowchart TD subgraph P1 ["Process P1"] CP1_start["P1 Code"] --> SCP1["P1 Critical Section"] SCP1 --> CP1_end["P1 Code"] end subgraph P2 ["Process P2"] CP2_start["P2 Code"] --> SCP2["P2 Critical Section"] SCP2 --> CP2_end["P2 Code"] end SCP1 ==>|Exclusive access| R["Resource R"] SCP2 ==>|Exclusive access| R["Resource R"]

Diagram created with Mermaid

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:

  1. Request the resource: The process requests exclusive access to the resource (if it is occupied, the process must wait).
  2. Use the resource: The process enters the critical section and performs the required operations on the resource.
  3. Release the resource: The process frees the resource, allowing other waiting processes to acquire it.

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).

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