Search HoldMyTwist

Browse all posts

Episode 44

The Carousel of Small Jobs

Duck's teeny mug waits behind two big jobs on one polishing wheel.

Episodes

Computing Basics

    Duck is holding the bowl up to the only polishing wheel.
    A robot standing in front of the screen with a timer in his hand. Duck with a cup inside the screen next to a circle bowl,pitcher,cup diagram.
    Wheel does A for 2 ticks. A has 3 ticks left. A is behind B and C.
    Next queue shows: B -- 3 ticks C -- 1 tick A -- 3 ticks Next interval is hidden.
    The pitcher joins the back of the line. It needs one more tick of polish.
    C finishes one tick of shining (at time 5) and Duck puts the cup on the done tray. A has 3 ticks remaining, its turn in the queue. B has 1 tick remaining. A's turn, starting at time 5 with no idle tick.
    Duck is proud of his final C cup. A needs 3 ticks. B needs 1 tick.
    robot points at the first 5 ticks on the timeline. duck has finished cup. bowl and pitcher are still to go.
    A goes 2 more ticks. The last tick of A is behind B. C is still done.
    B works another unit of time. B goes to the done tray with C. A still waits.
    A's final tick passes. All three jobs are complete. Ready queue is empty.
    all three shiny things (bowl,pitcher,cup) are shiny on the done tray. Nothing in ready queue.
    A new queue with practice which will begin with P requiring 3 ticks and Q requiring 2.
    P takes two ticks. (p has one tick left) Q takes two ticks. (time is now four)
    P uses 4 to 5 last tick. Q is done before P.
    duck inside of monitor sets two hourglasses next to finished bowl,pitcher,cup. Robot pointing at turn diagrams.

    Episode transcript

    Three impatient chores

    Duck is holding the bowl up to the only polishing wheel.

    Diagram values: ONE WORKER; READY AT t=0; A:5 B:3 C:1; QUEUE A B C

    Duck: One polishing wheel three jobs

    A robot standing in front of the screen with a timer in his hand. Duck with a cup inside the screen next to a circle bowl,pitcher,cup diagram.

    Diagram values: QUANTUM 2; NO NEW ARRIVALS; NO SWITCH COST

    Robot: Only two ticks at most

    Wheel does A for 2 ticks. A has 3 ticks left. A is behind B and C.

    Diagram values: t=0..2 A; REMAIN A3 B3 C1; QUEUE B C A

    Duck: A gets 2 ticks... Still 3 to go... Big bowl, back of the line.

    Next queue shows: B -- 3 ticks C -- 1 tick A -- 3 ticks Next interval is hidden.

    Diagram values: t=2; QUEUE B3 C1 A3; QUANTUM 2

    Robot: Which job takes over the wheel next? Will it be done this turn?

    Two ticks each

    The pitcher joins the back of the line. It needs one more tick of polish.

    Diagram values: t=2..4 B; REMAIN A3 B1 C1; QUEUE C A B

    Duck: Two ticks gone. Just a tick more and the pitcher is done.

    C finishes one tick of shining (at time 5) and Duck puts the cup on the done tray. A has 3 ticks remaining, its turn in the queue. B has 1 tick remaining. A's turn, starting at time 5 with no idle tick.

    Diagram values: t=4..5 C; C DONE AT5; QUEUE A3 B1

    Duck: A is up next. No delay.

    Duck is proud of his final C cup. A needs 3 ticks. B needs 1 tick.

    Diagram values: C DONE; A3 B1 WAITING

    Duck: My cup is complete

    robot points at the first 5 ticks on the timeline. duck has finished cup. bowl and pitcher are still to go.

    Diagram values: A 0–2; B 2–4; C 4–5; A3 B1

    Robot: But there's still work to be done.

    The last little turn

    A goes 2 more ticks. The last tick of A is behind B. C is still done.

    Diagram values: t=5..7 A; REMAIN A1 B1 C0; QUEUE B A

    Duck: A is back!

    B works another unit of time. B goes to the done tray with C. A still waits.

    Diagram values: t=7..8 B; B DONE AT8; QUEUE A1

    Robot: B's final tick finishes.

    A's final tick passes. All three jobs are complete. Ready queue is empty.

    Diagram values: t=8..9 A; A DONE AT9; QUEUE EMPTY; 5+3+1=9

    Duck: All 9 ticks of work are accounted for.

    all three shiny things (bowl,pitcher,cup) are shiny on the done tray. Nothing in ready queue.

    Diagram values: ONE WORKER; NO ARRIVALS OR BLOCKING; ZERO SWITCH COST

    Robot: it's a simplified equal-priority model.

    A fresh queue

    A new queue with practice which will begin with P requiring 3 ticks and Q requiring 2.

    Diagram values: PRACTICE t=0; QUEUE P3 Q2; QUANTUM 2

    Duck: Predict finish order.

    P takes two ticks. (p has one tick left) Q takes two ticks. (time is now four)

    Diagram values: P 0–2; Q 2–4; Q DONE4; QUEUE P1

    Robot: P goes from zero to 2, leaving 1.

    P uses 4 to 5 last tick. Q is done before P.

    Diagram values: P 4–5; FINISH Q4 THEN P5

    Duck: Q wins first, at four. P wins at five, on its last tick.

    duck inside of monitor sets two hourglasses next to finished bowl,pitcher,cup. Robot pointing at turn diagrams.

    Diagram values: TURN <=2 TICKS

    Robot: No more than two ticks each time.

    The lesson

    You have 1 polishing wheel which can do 3 jobs with equal priority. They're all ready at t=0. A takes 5 ticks, B takes 3, C takes 1. quantum = max 2 ticks per turn. If it's not done the job is pushed to the back of the queue. If the job's done it leaves right away. C runs from 4-5. Then A takes over at 5. C doesn't hold the wheel up for an extra tick. No new arrivals, no blocking, no switching costs for this toy example.

    If you complete jobs in their order of arrival, then A does 0-5, B does 5-8, C does 8-9. The tiny cup is done at 9. In the story, each job has at most 2 ticks to go around. C is 4-5 so the cup ends at 5. Both of them do 9 ticks of work and finish all work at 9.

    The cup finishes early because these long jobs are giving up the wheel before they finish. A takes longer: 9 with shared turns, vs 5 when run to completion first. The tasks and work are the same. All these numbers assume no cost for changing tasks, no new tasks coming in and no blocking. There are other rules in actual schedulers.

    Try it

    Start a new ready queue from order R, S, T at time 0. Job R takes 4 ticks of work, job S takes 1 tick of work, job T takes 2 ticks of work. Our toy example uses one worker. Jobs are equal priority. All jobs are ready at t=0. Quantum is fixed, 2 ticks. There are no arrivals or blocking. There is no switching cost. Show the intervals of time in which each job runs. Show the order and time of job completion.

    Answer

    R starts from zero to 2. It has 2 ticks left. It moves to the end of the queue. S starts from 2 to 3 and completes. T starts from 3 to 5 and completes. R starts from 5 to 7. It completes. So order of completion: S(time = 3) T(time = 5) R(time = 7)

    Sources

    Linux man-pages: sched(7), SCHED_RR (opens in a new tab)