{"id":250,"date":"2026-11-13T12:35:54","date_gmt":"2026-11-13T04:35:54","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-round-robin-shares-a-processor-by-a-quantum\/"},"modified":"2026-11-13T21:53:41","modified_gmt":"2026-11-13T13:53:41","slug":"how-round-robin-shares-a-processor-by-a-quantum","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-round-robin-shares-a-processor-by-a-quantum\/","title":{"rendered":"How round-robin shares a processor by a quantum"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Round-robin<\/strong> scheduling runs the job at the head of a ready queue for a fixed quantum, then moves it to the tail if it is still ready. With n jobs continuously ready, a quantum q, and a context switch of length s, each job waits at most (n \u2212 1)(q + s) between slices and receives a 1\/n share of the processor, switches included. The policy prevents starvation. It does not minimize average turnaround, and it does not meet deadlines by itself.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A first-come queue lets one long job hold the processor while short jobs wait. Round-robin slices the hold. The quantum is the longest a job may run before the scheduler looks at the queue again. A job that blocks for input leaves the queue early. A job that uses the whole quantum goes to the back, behind jobs that were already waiting.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The quantum is the entire policy decision. A very long quantum means each job runs to completion once it starts, which is first-come service in practice. A very short quantum spends the processor on context switches. The useful range sits where q is large next to s and small next to the interactive response the system wants.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">If n jobs stay ready, the wait between the start of two consecutive slices of the same job is at most (n \u2212 1)(q + s). No ready job waits forever while the scheduler keeps running. Over a long interval the processor time, including switches, is split evenly among the jobs that were continuously ready. Extra memory is the queue. Preemption is required at quantum boundaries.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The even split is not an optimality claim about turnaround time. When every burst length is known in advance, shortest job first produces a smaller mean turnaround. Round-robin does not use burst lengths. It also does not use deadlines. A deadline test belongs to earliest-deadline-first or rate-monotonic analysis.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Keep ready jobs in a queue. Choose a quantum q.<\/li>\n<li>Dequeue the head and run it until it blocks, finishes, or has consumed q, whichever comes first.<\/li>\n<li>If it is still ready, enqueue it at the tail. If it blocked, put it on the structure that will wake it.<\/li>\n<li>A newly woken or newly arrived job joins the tail, unless a separate rule says otherwise.<\/li>\n<li>Charge the context switch s in any accounting that quotes a processor share. The share of the processor spent in switches is about s\/(q + s) when every job uses a full quantum.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The wait bound assumes the scheduler actually preempts at q and that ready jobs are only those in this queue. Deadline feasibility is earliest deadline first or rate-monotonic scheduling. Mean-turnaround optimality with known bursts is shortest job first. Round-robin is the equal-slice queue.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the quantum grows to the longest job<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Set q larger than every burst and still call the policy fair slicing<\/td>\n<td>No job is preempted. The queue behaves as first-come, first-served. Fairness between a short job and a long job in front of it disappears.<\/td>\n<\/tr>\n<tr>\n<td>Set q near the context-switch cost and quote a full 1\/n of useful work<\/td>\n<td>A large fraction of each cycle is the switch. The useful share is q\/(q + s), split among the jobs.<\/td>\n<\/tr>\n<tr>\n<td>Use the queue order as a deadline proof<\/td>\n<td>The queue does not look at deadlines. A job can be sliced evenly and still finish late.<\/td>\n<\/tr>\n<tr>\n<td>Leave a finished job in the queue<\/td>\n<td>The remaining jobs keep paying a slice of processor time to a job that has no work. Completion has to unlink it.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Deadlines on one processor are earliest deadline first. Fixed priorities by period are rate-monotonic scheduling. Round-robin is the quantum queue.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment runs each ready job for a quantum of 2 and sends leftover work to the tail. Jobs of length 3 and 4 produce the slices <code>A A B B A B<\/code>.<\/p>\n\n<pre><code class=\"language-python\">from collections import deque\njobs = deque([(&quot;A&quot;, 3), (&quot;B&quot;, 4)])\nquantum = 2\nslices = []\nwhile jobs:\n    name, left = jobs.popleft()\n    run = min(quantum, left)\n    slices.extend([name] * run)\n    left -= run\n    if left:\n        jobs.append((name, left))\nassert slices == [&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;]\nprint(*slices)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Linux <strong><code>SCHED_RR<\/code><\/strong> is round-robin among real-time tasks of equal priority: each task runs for a quantum, then joins the tail of its priority. Ordinary desktop scheduling is the fair scheduler, not this quantum. A quantum that is too long becomes first-come service.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop round-robin when burst times are known and mean turnaround is the objective. Shortest job first uses those times. Keep the quantum when the requirement is that a ready job cannot be starved by one long neighbor.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for round-robin scheduling returns the quantum, the tail requeue, and the wait bound (n \u2212 1)(q + s). <strong>Round-robin runs each ready job for a quantum and then sends it to the tail, so a job that stays ready waits at most (n \u2212 1)(q + s) between slices.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Round-robin scheduling runs the job at the head of a ready queue for a fixed quantum, then moves it to the tail if it is still ready. With n jobs continuously ready, a quantum q, and a context switch of length s, each job waits at most&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,51],"tags":[],"class_list":["post-250","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-scheduling"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":649,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/250\/revisions\/649"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}