Unit 2 of 4 · M.Sc IT Sem 1

Unit 2: Synchronization, scheduling and deadlocks

Operating System notes · PTU syllabus (PGCA1903)

5 min read12 topics10 exam questions
On this page
  1. Unit summary
  2. Race conditions
  3. The critical section problem
  4. Mutex locks
  5. Semaphores and monitors
  6. The bounded buffer problem
  7. The readers–writers problem
  8. CPU scheduling concepts and criteria
  9. Single-processor scheduling algorithms
  10. Multiprocessor scheduling
  11. Real-time scheduling
  12. Deadlocks: necessary conditions and resource allocation graph
  13. Deadlock prevention, avoidance, detection and recovery
  14. Key terms
  15. Quick revision
  16. Important questions

Unit summary

Processes that share data must be synchronised, scheduled fairly and kept out of deadlock. This unit covers the critical section problem, mutex locks, semaphores, the bounded buffer and readers–writers problems, CPU scheduling concepts and criteria, single-processor, multiprocessor and real-time scheduling, and deadlocks — conditions, resource allocation graphs, prevention, avoidance, detection and recovery.

After this unit you can

  • Solve the critical section problem with mutex locks and semaphores
  • Solve the bounded buffer and readers–writers problems
  • Apply CPU scheduling algorithms and explain multiprocessor and real-time scheduling
  • Handle deadlocks

PTU syllabus topics

  • Critical section problem
  • mutex locks
  • semaphores
  • bounded buffer and reader-writer problems
  • CPU scheduling concepts and criteria
  • single/multiprocessor and real-time scheduling
  • deadlock necessary conditions
  • resource allocation graph
  • prevention
  • avoidance
  • detection and recovery
ClassificationFour conditions for deadlock (Coffman)
Deadlock occurs only if all hold
  • Mutual exclusion

    Resource used by one at a time

  • Hold and wait

    Holding one, waiting for another

  • No preemption

    Resources can't be taken away

  • Circular wait

    Closed chain of waiting processes

1

Topic 1

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

Mutex locks

  • A mutex lock protects a critical section: a process must acquire() the lock before entering and release() it on leaving. A lock that busy-waits is a spinlock — useful on multiprocessors when waits are short.
cacquire() { while (!available) ; /* busy wait */ available = false; }
release() { available = true; }
/* usage */
acquire();  /* critical section */  release();
4

Topic 4

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.

5

Topic 5

The bounded buffer problem

c/* n buffers; semaphores: mutex = 1, empty = n, full = 0 */
/* Producer */                       /* Consumer */
do {                                 do {
  produce an item;                     wait(full);
  wait(empty);                         wait(mutex);
  wait(mutex);                         remove item from buffer;
  add item to buffer;                  signal(mutex);
  signal(mutex);                       signal(empty);
  signal(full);                        consume the item;
} while (true);                      } while (true);
  • empty counts free slots, full counts filled slots, mutex guards the buffer itself.
6

Topic 6

The readers–writers problem

c/* rw_mutex = 1, mutex = 1, read_count = 0 */
/* Writer */                         /* Reader */
wait(rw_mutex);                      wait(mutex);
  /* writing */                        read_count++;
signal(rw_mutex);                      if (read_count == 1) wait(rw_mutex);  /* first reader locks out writers */
                                     signal(mutex);
                                       /* reading */
                                     wait(mutex);
                                       read_count--;
                                       if (read_count == 0) signal(rw_mutex); /* last reader lets writers in */
                                     signal(mutex);
  • Many readers may read together; a writer needs exclusive access. This "first readers–writers" solution can starve writers.
7

Topic 7

CPU scheduling concepts and criteria

Key termsScheduling criteria
CPU utilisation
Keep the CPU as busy as possible (maximise)
Throughput
Processes completed per unit time (maximise)
Turnaround time
Completion time − arrival time (minimise)
Waiting time
Turnaround time − burst time (minimise)
Response time
Time from submission to first response (minimise)

Preemptive scheduling can take the CPU away from a running process; non-preemptive lets it run until it finishes or blocks.

8

Topic 8

Single-processor scheduling algorithms

ComparisonCPU scheduling algorithms
How it works
Key point

FCFS

First come, first served; non-preemptive

Simple; convoy effect behind long jobs

SJF

Shortest burst first; preemptive version is SRTF

Minimum average waiting time; may starve long jobs

Round Robin

Each process gets a time quantum in turn; preemptive

Fair and good for time-sharing; quantum size matters

Example

Processes P1 = 24, P2 = 3, P3 = 3 ms, all arriving at 0. FCFS (P1, P2, P3): waiting times 0, 24, 27 → average 17 ms. SJF (P2, P3, P1): waiting times 6, 0, 3 → average 3 ms.

Example

Round Robin with quantum 4 for the same jobs: P1 runs 0–4, P2 4–7, P3 7–10, then P1 10–30. Waiting: P1 = 6, P2 = 4, P3 = 7 → average 5.67 ms.

Exam tip

Always draw a Gantt chart first, then compute completion, turnaround and waiting times in a table. Examiners give marks for each step.

9

Topic 9

Multiprocessor scheduling

  • Multiprocessor scheduling: asymmetric (one master processor schedules) vs symmetric (each processor self-schedules); processor affinity keeps a process on the same CPU for cache benefits; load balancing (push and pull migration).
  • Thread scheduling: kernel schedules kernel-level threads; user-level threads are mapped by the thread library (many-to-one, one-to-one, many-to-many).
  • Real-time scheduling: rate-monotonic and earliest-deadline-first.
10

Topic 10

Real-time scheduling

  • Hard real-time systems must meet every deadline (airbag controller); soft real-time systems give priority but tolerate occasional misses (video streaming).
ComparisonReal-time scheduling algorithms
Rule
Notes

Rate-monotonic (RM)

Static priority — shorter period, higher priority

Schedulable if CPU utilisation ≤ n(2^(1/n) − 1), about 69% for many tasks

Earliest deadline first (EDF)

Dynamic priority — nearest deadline runs first

Can reach 100% utilisation in theory

Example

Tasks P1 (period 50, burst 20) and P2 (period 100, burst 35): utilisation = 0.40 + 0.35 = 0.75. RM bound for two tasks is 0.83, so RM can schedule them.

11

Topic 11

Deadlocks: necessary conditions and resource allocation graph

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

12

Topic 12

Deadlock prevention, avoidance, detection and recovery

  • 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 termsDeadlock prevention by condition
Mutual exclusion
Make resources sharable where possible (read-only files)
Hold and wait
Request all resources at start, or release before requesting
No preemption
Take resources away from a waiting process
Circular wait
Number resource types and request in increasing order
  • Recovery: abort all deadlocked processes, abort one at a time until the cycle breaks, or preempt resources (choose a victim, roll back, avoid starvation).

Key terms

Critical section
Code accessing shared data
Mutex lock
Lock giving mutual exclusion
Semaphore
Integer variable used with wait and signal
Real-time system
System whose correctness depends on meeting deadlines
Safe state
State from which all processes can complete

Quick revision

  • Race conditions; mutual exclusion, progress, bounded waiting.
  • Mutex and spinlock; binary and counting semaphores; monitors.
  • Bounded buffer with mutex, empty, full; readers–writers with read_count.
  • Scheduling criteria; FCFS, SJF, SRTF, priority, RR; multiprocessor; RM and EDF.
  • Coffman conditions; RAG; prevention, Banker's avoidance, detection, recovery.

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.State the three requirements of a critical section solution.
  2. Q2.What is a spinlock?
  3. Q3.Which semaphores are used in the bounded buffer problem?
  4. Q4.Distinguish hard and soft real-time systems.
  5. Q5.State the four necessary conditions for deadlock.
  6. Q6.What is a safe state?

Long-answer questions

  1. Q1.Explain semaphores and solve the bounded buffer problem.
  2. Q2.Solve the readers–writers problem using semaphores.
  3. Q3.Compare CPU scheduling algorithms with a numerical example.
  4. Q4.Explain deadlock prevention, avoidance with the Banker's algorithm, detection and recovery.

Stuck on this unit?

Message SBS on WhatsApp for help with Operating System, or to ask about studying M.Sc IT at Synetic.

WhatsApp us