Unit 3 of 4 · BCA Sem 2

Unit 3: Synchronization and deadlocks

Operating Systems notes · PTU syllabus (UGCC2508)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Inter-process communication and race conditions
  3. The critical section problem
  4. Semaphores and monitors
  5. Deadlock: model and characterisation
  6. Handling deadlocks and the Banker's algorithm
  7. Key terms
  8. Quick revision
  9. Important questions

Unit summary

When processes run at the same time and share data, they must be coordinated, or they can corrupt data or block each other forever. This unit covers inter-process communication, race conditions, the critical section problem and its solutions (semaphores and monitors), and deadlocks with the Banker's algorithm.

After this unit you can

  • Explain IPC, race conditions and the critical section problem
  • Use semaphores and monitors for mutual exclusion
  • State the four necessary conditions for deadlock
  • Apply deadlock prevention, avoidance (Banker's algorithm), detection and recovery

PTU syllabus topics

  • Inter-process communication
  • race conditions
  • critical section problem
  • mutual exclusion
  • semaphores
  • monitors
  • deadlock system model and characterization
  • prevention
  • avoidance
  • Banker's algorithm
  • deadlock detection and recovery
Key termsFour conditions for deadlock (all must hold)
Mutual exclusion
A resource can be used by only one process at a time
Hold and wait
A process holds one resource while waiting for another
No preemption
Resources cannot be taken away forcibly
Circular wait
A closed chain of processes each waiting for the next
1

Topic 1

Inter-process communication and race conditions

Inter-process communication (IPC) lets processes exchange data. The two models are shared memory (fast, needs synchronisation) and message passing (send/receive; easier across computers). A race condition occurs when two processes access shared data at the same time and the result depends on the order in which they run.

Example

Two processes both read a counter value 5, each adds 1 and writes back 6. The correct result is 7 — one update was lost.

2

Topic 2

The critical section problem

A critical section is the part of a program that accesses shared data. A solution must satisfy three requirements:

  • Mutual exclusion: only one process in its critical section at a time.
  • Progress: if no process is in the critical section, one that wants to enter must be allowed to without indefinite delay.
  • Bounded waiting: a limit on how many times others can enter before a waiting process gets its turn.
3

Topic 3

Semaphores and monitors

A semaphore S is an integer variable accessed only through two atomic operations:

  • wait(S) (P): while S ≤ 0 wait; then S = S − 1.
  • signal(S) (V): S = S + 1.

A binary semaphore (0 or 1, also called a mutex) gives mutual exclusion; a counting semaphore controls access to a resource with several instances.

wait(mutex);
    /* critical section */
signal(mutex);
    /* remainder section */

A monitor is a high-level construct in which shared data and the procedures that use it are grouped together, and only one process can be active inside the monitor at a time. Condition variables (wait and signal) handle waiting inside it.

Exam tip

Classic synchronisation problems: producer-consumer (bounded buffer), readers-writers and dining philosophers. Know one solution with semaphores.

4

Topic 4

Deadlock: model and characterisation

A deadlock is a situation in which a set of processes are blocked forever, each waiting for a resource held by another.

ClassificationFour necessary conditions (Coffman)
Deadlock occurs only if all hold
  • Mutual exclusion

    A resource is used by one process at a time

  • Hold and wait

    A process holds resources while waiting for more

  • No preemption

    Resources cannot be taken away forcibly

  • Circular wait

    A closed chain of processes, each waiting for the next

A resource allocation graph shows processes and resources; a cycle indicates a possible deadlock (a certain deadlock if each resource has one instance).

5

Topic 5

Handling deadlocks and the Banker's algorithm

  • Prevention: make sure at least one of the four conditions can never hold (for example, request all resources at once, or number resources and request them in order).
  • Avoidance: check each request and grant it only if the system stays in a safe state. The Banker's algorithm does this.
  • Detection and recovery: allow deadlock, detect it periodically, then recover by terminating processes or preempting resources.
Key formulasBanker's algorithm
  • Need

    Need = Max − Allocation

  • Safe check

    Find a process with Need ≤ Available

  • Release

    Available = Available + Allocation of that process

  • Safe state

    All processes can finish in some order (safe sequence)

Example

If Available = (3, 3, 2) and P1's Need = (1, 2, 2), P1 can finish; its allocation is released and added to Available, and the check continues with the others to find a safe sequence.

Key terms

Race condition
An error caused by unsynchronised access to shared data
Critical section
Code that accesses shared resources
Semaphore
An integer used with wait and signal for synchronisation
Deadlock
Processes blocked forever, each waiting for another
Safe state
A state in which all processes can finish in some order

Quick revision

  • Critical section requirements: mutual exclusion, progress, bounded waiting.
  • wait decrements, signal increments; binary semaphore = mutex.
  • Deadlock needs all four Coffman conditions.
  • Banker's: Need = Max − Allocation; find a safe sequence.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.What is a race condition?
  2. Q2.State the requirements of a critical section solution.
  3. Q3.Differentiate between binary and counting semaphores.
  4. Q4.What is a monitor?
  5. Q5.List the four necessary conditions for deadlock.
  6. Q6.What is a safe state?

Long-answer questions

  1. Q1.Explain the critical section problem and its solution using semaphores.
  2. Q2.Explain the producer-consumer problem and its solution.
  3. Q3.Explain deadlock characterisation and methods of deadlock prevention.
  4. Q4.Explain the Banker's algorithm with an example and find the safe sequence.

Stuck on this unit?

Message SBS on WhatsApp for help with Operating Systems, or to ask about studying BCA at Synetic.

WhatsApp us