Instiq
Chapter 2 · Computer systems·v1.0.0·Updated 7/9/2026·~16 min

What's changed: Initial version

2.5The OS and Middleware

Key points

Building on the FE/SG basics of task management and mutual exclusion, this section goes deeper into comparing scheduling schemes (round robin, priority, SJF), how semaphores implement mutual exclusion, and the four necessary conditions for deadlock and how to avoid it. It also covers the relationship between virtual memory and process management, the role of middleware (DBMS, application servers, message queues), and OSS licenses (GPL, MIT, etc.).

FE/SG requires only a phenomenon-level understanding: "mutual exclusion is implemented with semaphores" and "deadlock can occur." AP requires a more structural understanding of why deadlock occurs and which conditions must hold, and how breaking one of those conditions avoids it. This section also organizes practical knowledge of middleware and OSS licenses.

2.5.1Comparing scheduling schemes

SchemeSelection criterionWeakness
Round robinFair, fixed time slices; suits response-focused interactive systemsToo short a time quantum causes frequent context switches, raising overhead
Priority schedulingSuits environments with a mix of tasks at different urgency levelsRisk of ==starvation==, where low-priority tasks never get to run
SJF (Shortest Job First)Suits batch processing aiming to minimize average wait timeExecution time is hard to know in advance, and long jobs risk perpetual ==starvation==
  • SJF (Shortest Job First) is a scheduling scheme that prioritizes the task expected to have the shortest execution time, which has the property that it theoretically minimizes average wait time. In practice, though, accurately estimating execution time in advance is difficult, and it has the weakness that a task with a long execution time can suffer perpetual starvation, always being pushed to the back.
  • The starvation common to both priority scheduling and SJF is a state where a task meeting a certain condition (low priority, long execution time, etc.) never gets allocated the CPU. A representative countermeasure is aging, which dynamically raises a task's priority the longer it has been waiting.

2.5.2The conditions for deadlock and how to avoid it

  • For a deadlock to occur, the following four conditions must all hold simultaneously: (1) mutual exclusion (a resource can be used by only one task at a time); (2) hold and wait (a task holds a resource while waiting for another to free up); (3) no preemption (a resource held by another task cannot be forcibly taken away); and (4) circular wait (multiple tasks form a cycle, each waiting on a resource held by the next). Conversely, breaking even one of these four conditions prevents deadlock from occurring.
  • Practical deadlock-avoidance strategies can be organized by which of the four conditions they break. "Standardizing the order in which all tasks acquire resources" is a representative strategy for breaking circular wait (4)—if you always decide to acquire resource 1 before resource 2, a circular wait structurally cannot form. "Timing out and releasing already-held resources to retry if acquisition does not succeed within a set time" breaks hold and wait (2). "Letting a higher-priority task forcibly preempt a resource held by a lower-priority task" breaks no preemption (3), though it has the side effect of complicating how the preempted task maintains consistency.
Exam point

The staples: the four deadlock conditions are mutual exclusion, hold-and-wait, no preemption, and circular wait (deadlock occurs only when all four hold); standardizing resource-acquisition order breaks circular wait; SJF theoretically minimizes average wait time, but long jobs risk starvation; aging raises priority based on wait time to prevent starvation. The classification of middleware (DBMS, application server, message queue) and the difference between OSS licenses (GPL requires republishing modifications; MIT is more permissive about modification and redistribution) also recur.

Let us apply the four deadlock conditions to a real scenario. In an online banking system, suppose a funds-transfer task A holds a lock on "account X" while waiting for a lock on "account Y" to free up, and simultaneously a second funds-transfer task B holds a lock on "account Y" while waiting for a lock on "account X." Here, all four conditions hold: (1) mutual exclusion (each account lock can be held by only one task); (2) hold and wait (both tasks hold one lock while waiting for the other); (3) no preemption (a lock held by the other task cannot be forcibly released); and (4) circular wait (A waits on B's resource and B waits on A's, forming a cycle)—this is a deadlock. The most practical countermeasure is to establish a standard rule that all funds-transfer tasks acquire the lock for the lower-numbered account first. If this rule is consistently enforced, both tasks will always attempt to acquire locks in the same order regardless of which account number is lower, so the circular wait described above (condition 4) can no longer structurally form, and breaking one of the four conditions avoids deadlock. It is also worth touching on the role of middleware. Middleware sits between the OS and applications, providing common functionality to reduce development burden—examples include the DBMS (database management system), which handles data persistence; the application server, which provides the execution environment for business logic; and the message queue, which passes messages asynchronously between systems (absorbing speed differences between the sender and receiver, so processing can resume later even if one side is temporarily paused). When using OSS, license differences also matter. GPL (GNU General Public License) imposes an obligation to publish the modified source code, also under GPL, if you modify and distribute the software (copyleft), whereas the MIT License places almost no restriction on modification, redistribution, or commercial use as long as the copyright notice is retained, making it easy to embed in a proprietary product. Embedding OSS without understanding this distinction risks unintentionally taking on an obligation to publish your own source code.

Warning

Trap: "deadlock occurs once mutual exclusion, hold-and-wait, and no preemption are all present" is wrong—deadlock occurs only once all four conditions, including circular wait (4), are present together; without a circular structure, those three alone do not cause it. Also, "SJF treats long and short tasks fairly" is wrong—SJF prioritizes short tasks, so long tasks risk starvation, being perpetually pushed to the back. "The MIT License, like GPL, requires publishing modified source code" is also wrong—that obligation (copyleft) is imposed by GPL; the MIT License leaves modification and redistribution nearly unrestricted as long as the copyright notice is retained.

Task management, mutual exclusion, virtual memory.
Resources coordinated by the OS

2.5.3Section summary

  • The four deadlock conditions: mutual exclusion, hold-and-wait, no preemption, circular wait (occurs only when all four hold). Standardizing resource-acquisition order breaks circular wait, a representative avoidance strategy
  • SJF theoretically minimizes wait time but risks starvation for long jobs (mitigated by aging). Middleware includes the DBMS, application server, and message queue
  • GPL imposes an obligation to republish modifications (copyleft). The MIT License leaves modification and redistribution nearly unrestricted if the copyright notice is retained

Sign in to track progress — Log in.

Quick check

(just a quick review)

Q1. Among the four necessary conditions for deadlock, which one refers to the state where multiple tasks form a cycle, each waiting for another to release a resource?

Q2. Which deadlock condition does establishing a standard rule that tasks always acquire the lower-numbered resource first prevent?

Q3. When modifying an OSS library and embedding it in a redistributed proprietary product, which license imposes an obligation (copyleft) to also publish the modified source code under the same license?

Check your understandingPractice questions for Chapter 2: Computer systems