ENع
→FCFS and SJF
الدرس 13 · CPU scheduling

First come first served and shortest job first

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

When several jobs are ready and there is one CPU, the scheduler decides who runs next. Follow three jobs, A needing 100 seconds and B and C needing 10, through two classic policies. First come first served runs them in arrival order and leaves the short jobs stuck behind the long one: the convoy effect. Shortest job first runs the short ones first and cuts the average turnaround from 110 to 50 seconds, until jobs arrive at different times and its limits show. The lesson on turnaround, response time and fairness defines the measures used here, and The context switch and the timer interrupt explains how a kernel can stop a running job.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 13 · تفاعلي
01 / 10

Three jobs, one CPU

Three jobs wait in the ready queue, the list of jobs ready to run. A needs 100 seconds of CPU time; B and C need 10 each. All arrive at time 0. The scheduler, part of the kernel, picks who runs next.

job   arrives   needs (run time)
A     0         100 s
B     0         10 s
C     0         10 s

one CPU: one job runs at a time
turnaround = time finished - time arrived
(defined in the lesson on turnaround, response time
 and fairness)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Three jobs, one CPU

    Three jobs wait in the ready queue, the list of jobs ready to run. A needs 100 seconds of CPU time; B and C need 10 each. All arrive at time 0. The scheduler, part of the kernel, picks who runs next.

    job   arrives   needs (run time)
    A     0         100 s
    B     0         10 s
    C     0         10 s
    
    one CPU: one job runs at a time
    turnaround = time finished - time arrived
    (defined in the lesson on turnaround, response time
     and fairness)
  2. First come, first served

    First come first served (FCFS, also called FIFO) runs jobs in the order they arrived. Each job runs to the end before the next one starts. A, B and C arrived together in that order, so A goes first.

    FCFS: take the job at the front of the queue,
          run it until it is done, repeat
    ties (same arrival time): queue order A, B, C
    
    Linux has a real FCFS policy: SCHED_FIFO.
    A SCHED_FIFO thread runs until it blocks, yields or a
    higher-priority thread arrives; equal priorities go
    in arrival order (sched(7)). C99 / POSIX:
    
    #include <sched.h>
    #include <stdio.h>
    
    int main(void) {
        printf("SCHED_FIFO priorities: %d to %d\n",
               sched_get_priority_min(SCHED_FIFO),
               sched_get_priority_max(SCHED_FIFO));
        return 0;
    }
    
    on Linux prints: SCHED_FIFO priorities: 1 to 99
  3. A runs; B and C wait

    A holds the CPU from 0 to 100 seconds. B and C need only 10 seconds each, yet they sit in the queue the whole time. Short jobs stuck behind a long one: this is called the convoy effect.

    FCFS, time 0 to 100
    CPU     A runs 0-100
    queue   B waiting, C waiting
    
    convoy effect: short jobs queue up behind
    one heavy job, like a long line at one till
  4. Then B, then C

    B finally runs from 100 to 110, and C from 110 to 120. Turnaround is finish time minus arrival time. All three arrived at 0, so their turnarounds are 100, 110 and 120 seconds.

    FCFS schedule:
    A   arrives   0   runs   0-100   turnaround 100 - 0 = 100
    B   arrives   0   runs 100-110   turnaround 110 - 0 = 110
    C   arrives   0   runs 110-120   turnaround 120 - 0 = 120
  5. FCFS average: 110 seconds

    Average turnaround: (100 + 110 + 120) / 3 = 110 seconds. B and C each ran for 10 seconds but finished after more than 100. The order of the queue, not the work itself, made them slow.

    average turnaround  (100 + 110 + 120) / 3 = 110 s
    average response    (0 + 100 + 110) / 3 = 70 s
    (response = first run - arrival)
    
    FCFS is simple and fair in one sense: nobody jumps
    the queue. But one long job delays everyone behind it.
  6. Shortest job first

    Same three jobs, new rule. Shortest job first (SJF) picks the ready job with the shortest run time, then the next shortest, and so on. A started job still runs to the end. The scheduler must know every run time in advance.

    SJF: among the ready jobs, run the one that needs
         the least CPU time; run it to the end; repeat
    ties: queue order (B before C)
    
    B 10 s   C 10 s   A 100 s   ->  order B, C, A
  7. B, C, then A

    B runs from 0 to 10, C from 10 to 20, then A from 20 to 120. A now waits 20 seconds, but B starts at once and C waits only 10. The CPU still finishes all the work at 120.

    SJF schedule:
    B   arrives   0   runs   0-10    turnaround 10 - 0 = 10
    C   arrives   0   runs  10-20    turnaround 20 - 0 = 20
    A   arrives   0   runs  20-120   turnaround 120 - 0 = 120
    
    the CPU is busy 0-120 in both runs: same work, new order
  8. SJF average: 50 seconds

    Average turnaround: (10 + 20 + 120) / 3 = 50 seconds, less than half of FCFS's 110. When all jobs arrive together, SJF gives the lowest average turnaround of any order.

    SJF   average turnaround  (10 + 20 + 120) / 3 = 50 s
    FCFS  average turnaround  110 s
    SJF   average response    (0 + 10 + 20) / 3 = 10 s
    
    why: every job waits for all the jobs before it,
    so the shortest jobs should go first. Moving a long
    job earlier only adds its length to more waits.
  9. Late arrivals

    Now B and C arrive at time 10. At time 0 only A is ready, so SJF starts A, and A runs to the end. B and C wait anyway: (100 + 100 + 110) / 3 = 103.33 seconds average.

    SJF, B and C arrive at 10:
    A   arrives   0   runs   0-100   turnaround 100 - 0 = 100
    B   arrives  10   runs 100-110   turnaround 110 - 10 = 100
    C   arrives  10   runs 110-120   turnaround 120 - 10 = 110
    
    average turnaround  ((100 - 0) + (110 - 10) + (120 - 10)) / 3 = 103.33 s
    SJF never stops a running job, so a short job that
    arrives late still waits behind a long one.
  10. What SJF really needs

    SJF needs two things. It must know run times in advance, which a real OS rarely does. And to help late short jobs it must stop a running job, called preemption. Preemptive schedulers, such as round robin, come next.

    the whole example as a Python simulation:
    
    def schedule(jobs, policy):          # jobs: (name, arrival, run time)
        t, turnaround = 0, []
        waiting = sorted(jobs, key=lambda j: j[1])
        while waiting:
            ready = [j for j in waiting if j[1] <= t]
            if not ready:                # nothing has arrived yet
                t = waiting[0][1]
                continue
            if policy == "SJF":
                job = min(ready, key=lambda j: j[2])
            else:                        # FCFS: earliest arrival
                job = ready[0]
            waiting.remove(job)
            t += job[2]                  # runs to the end
            turnaround.append(t - job[1])
        return sum(turnaround) / len(turnaround)
    
    same = [("A", 0, 100), ("B", 0, 10), ("C", 0, 10)]
    late = [("A", 0, 100), ("B", 10, 10), ("C", 10, 10)]
    print("FCFS     ", schedule(same, "FCFS"))
    print("SJF      ", schedule(same, "SJF"))
    print("SJF late ", round(schedule(late, "SJF"), 2))
    
    prints:
    FCFS      110.0
    SJF       50.0
    SJF late  103.33

اختبار يتاح بعد الخطوة الأخيرة.