← Back to quizzesFree quiz

Operating Systems Exam Review

The Sleeping Barber is a classic concurrency example that illustrates how processes (customers) and a service provider (the barber) must coordinate access to limited resources (waiting‑room…

10 questions~5 min
Operating Systems Exam Review — Qwi
0 / 10
Score: 0%
1

In the Sleeping Barber problem, which synchronization primitive best prevents two customers from sitting in the same waiting‑room chair simultaneously?

2

When the barber finishes a haircut and finds the waiting room empty, what state should the barber transition to according to the problem description?

3

Using preemptive Shortest Job First on the given processes, which process runs first?

4

In the same preemptive SJF schedule, what is the average waiting time of all processes (rounded to two decimals)?

5

For the Round Robin policy with quantum 2 ms, how many context switches occur until all processes finish?

6

In the Banker’s algorithm, what is the value of the Need matrix entry for process P1 and resource type C?

7

Can the request (2, 1, 1, 0) from P1 be granted safely? Choose the correct answer.

8

If P1 requests one more instance of B and two of C, what will the Banker’s algorithm likely decide?

9

Which Linux command sequence correctly appends text to an existing file without overwriting its current content?

10

After completing all steps in Exercise 4, which command will display the final content of "my_OS.txt"?

Operating Systems Exam Review: Core Concepts Explained

1. Synchronization in the Sleeping Barber Problem

The Sleeping Barber is a classic concurrency example that illustrates how processes (customers) and a service provider (the barber) must coordinate access to limited resources (waiting‑room chairs). Understanding the correct synchronization primitive is essential for preventing race conditions.

  • Counting Semaphore: The optimal primitive for managing the number of available chairs. It is initialized to the total number of chairs and decremented when a customer sits down, then incremented when a customer leaves.
  • Why not a binary semaphore? A binary semaphore can only represent two states (locked/unlocked) and cannot track multiple chairs.
  • Why not a mutex? A mutex protects a critical section but does not convey the quantity of resources.
  • Why not a condition variable? Condition variables rely on an underlying lock and are better suited for signaling state changes rather than counting resources.

Thus, the counting semaphore initialized to the number of chairs best prevents two customers from occupying the same chair simultaneously.

2. Barber’s State When the Waiting Room Is Empty

According to the problem description, the barber does not remain idle or leave the shop. Instead, after completing a haircut and finding no waiting customers, the barber transitions to a sleeping state in the barber chair. This models the classic "sleeping barber" metaphor: the barber sleeps until a new customer arrives and wakes him.

3. Preemptive Shortest Job First (SJF) Scheduling

Preemptive SJF (also known as Shortest Remaining Time First) always selects the process with the smallest remaining CPU burst. To determine which process runs first, examine the initial burst times of all processes. The process with the shortest burst—P1 in this case—gets the CPU immediately.

4. Calculating Average Waiting Time in Preemptive SJF

Average waiting time is a key performance metric. It is calculated as the sum of each process’s waiting time divided by the number of processes. For the given set of processes, the total waiting time adds up to 6.4 ms. Dividing by the four processes yields an average of 1.60 ms (rounded to two decimal places).

5. Round Robin Scheduling with a Quantum of 2 ms

Round Robin (RR) gives each process a fixed time slice (quantum). Every time a quantum expires, a context switch occurs. By tracing the execution order of the processes with a 2 ms quantum, we count the switches needed until all processes terminate. The correct count is 7 context switches.

6. Understanding the Banker’s Algorithm

The Banker’s algorithm is a deadlock avoidance technique that uses three matrices:

  • Allocation: Resources currently held by each process.
  • Maximum: The maximum demand each process may request.
  • Need: Calculated as Maximum – Allocation. It represents the remaining resources each process may still request.

For process P1 and resource type C, the Need matrix entry is 1. This value is derived from the difference between P1’s maximum claim for C and what it has already been allocated.

7. Safety Check: Can a Request Be Granted?

When a process makes a request, the Banker’s algorithm performs a tentative allocation and then checks if the system remains in a safe state. A safe state means there exists at least one sequence of process completions that does not lead to deadlock.

For the request (2, 1, 1, 0) from P1, the algorithm determines that after the tentative allocation the system can still find a safe sequence. Therefore, the request can be granted safely.

8. Handling Potentially Unsafe Requests

Consider a scenario where P1 asks for one more instance of resource B and two more of C. The Banker’s algorithm evaluates the resulting Need and Available vectors. If granting the request would leave the system without a safe sequence—meaning some processes could become permanently blocked—the algorithm will deny the request to avoid entering an unsafe state.

9. Key Takeaways for Exam Preparation

  • Use a counting semaphore to manage multiple identical resources such as waiting‑room chairs.
  • The barber sleeps when no customers are waiting, reflecting the classic problem statement.
  • Preemptive SJF selects the process with the shortest remaining time; P1 runs first in the provided example.
  • Average waiting time is computed by summing individual waiting times and dividing by the number of processes (1.60 ms here).
  • Round Robin context switches depend on the quantum; with a 2 ms quantum, seven switches occur.
  • The Need matrix entry for P1‑C is 1, derived from Maximum minus Allocation.
  • Safety checks in the Banker’s algorithm ensure that granting a request does not lead to deadlock.
  • Requests that could cause an unsafe state are denied, even if resources appear abundant.

10. Frequently Asked Questions (FAQ)

Q: Why is a counting semaphore preferred over a mutex for the waiting‑room chairs?
A: A counting semaphore can represent any number of identical resources, whereas a mutex only indicates a binary state (locked/unlocked). The chairs are multiple, so a counting semaphore accurately tracks availability.

Q: What does it mean for a system to be in a "safe state"?
A: A safe state guarantees that there exists at least one order of process execution where each process can obtain its needed resources and complete without causing deadlock.

Q: How does preemptive SJF differ from non‑preemptive SJF?
A: Preemptive SJF can interrupt a running process if a new process arrives with a shorter remaining time, while non‑preemptive SJF lets the current process finish before considering others.

Q: Why does the Round Robin algorithm cause many context switches?
A: Because each quantum expiration forces the scheduler to switch to the next ready process, leading to a context switch for every quantum used.

11. Study Tips for Mastering Operating System Concepts

To excel in your operating systems exam, focus on the following strategies:

  • Visualize algorithms: Draw state diagrams for the Sleeping Barber, SJF, and Round Robin to see how processes move between states.
  • Practice matrix calculations: Repeatedly compute Allocation, Maximum, and Need matrices for different scenarios.
  • Simulate scheduling: Use paper or simple code to simulate preemptive SJF and Round Robin, tracking waiting times and context switches.
  • Explain concepts aloud: Teaching the material to a peer reinforces understanding and highlights any gaps.
  • Memorize key definitions: Terms like "safe state," "deadlock avoidance," and "quantum" are frequently tested.

By integrating these study techniques with the explanations above, you will be well‑prepared to tackle questions on synchronization, scheduling, and resource allocation in your operating systems exam.