CPU Schedulingجدولة المعالج

الشريحة 1 من 18

مقدمة الفصل

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦2–3⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦2–3⁩

CPU–I/O burst cycleدورة الدفعات

  • Maximum CPU utilization is obtained with multiprogramming.
  • CPU–I/O burst cycle: process execution consists of a cycle of CPU execution and I/O wait.
  • CPU bursts vary greatly, but in general follow an exponential distribution: many short bursts, few long bursts (slide 6).
تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦4–6⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦4–6⁩

When does the CPU scheduler decide?قرارات الجدولة

The CPU scheduler: preemptive vs nonpreemptive

CPU scheduler
Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them.
CPU scheduling decisions (slide 7)
#رقمThe process…العملية…Schedulingنوع الجدولة
1Switches from running to waiting stateNonpreemptive
2Switches from running to ready statePreemptive
3Switches from waiting to readyPreemptive
4TerminatesNonpreemptive

A running process is interrupted and moves to the ready state, and the scheduler picks another process. What kind of scheduling decision is this?

تفاصيل المصدرشرائح الفصل 2a · الشريحة ⁦7⁩
المصدر
شرائح الفصل 2a · الشريحة ⁦7⁩

Nonpreemptive vs preemptiveالمقاطعة وعدمها

The CPU scheduler: preemptive vs nonpreemptive

Nonpreemptiveغير قابلة للمقاطعة

  • Once a process is allocated the CPU, it does not leave unless:
  • 1. it has to wait (e.g., for an I/O request or for a child to terminate)
  • 2. it terminates

Preemptiveقابلة للمقاطعة

  • The OS can force (preempt) a process from the CPU at any time
  • Say, to allocate the CPU to another higher-priority process
Which is harder to implement?
Preemptive is harder: it needs to maintain consistency of data shared between processes and, more importantly, kernel data structures (e.g., I/O queues) — think of a preemption while the kernel is executing a system call on behalf of a process (many OSs wait for the system call to finish).

Why is preemptive scheduling harder to implement?

تفاصيل المصدرشرائح الفصل 2a · الشريحة ⁦8⁩
المصدر
شرائح الفصل 2a · الشريحة ⁦8⁩

The dispatcherالمُرسِل

Dispatcher
The module that gives control of the CPU to the process selected by the short-term scheduler.

This involves

  1. 1

    Switching context

  2. 2

    Switching to user mode

  3. 3

    Jumping to the proper location in the user program to restart that program

Dispatch latency
Time it takes for the dispatcher to stop one process and start another running.

What is dispatch latency?

تفاصيل المصدرشرائح الفصل 2a · الشريحة ⁦9⁩
المصدر
شرائح الفصل 2a · الشريحة ⁦9⁩

Scheduling criteriaمعايير الجدولة

Scheduling criteria (slide 10)
CriterionالمعيارDefinition (slide 10)التعريفGoalالهدف
CPU utilizationKeep the CPU as busy as possibleMaximize
Throughput# of processes that complete their execution per time unitMaximize
Turnaround timeAmount of time to execute a particular process (time from submission to termination)Minimize
Waiting timeAmount of time a process has been waiting in the ready queueMinimize
Response timeTime from when a request is submitted until the first response is produced, not output (for timesharing environment)Minimize
  • In most cases we optimize the average measure; sometimes the min or max — e.g., minimize the max response time so all users get good service (slide 11).

Which scheduling criterion counts only the time a process spends in the ready queue?

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦10–11⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦10–11⁩

FCFS and the convoy effectFCFS

First-Come, First-Served (FCFS)

المسألة

P1 = 24, P2 = 3, P3 = 3 arrive in the order P1, P2, P3 (slide 13).

خطوات الحل

  1. Gantt chart: P1 0–24, P2 24–27, P3 27–30.
  2. Waiting time: P1 = 0, P2 = 24, P3 = 27.

النتيجة

Average waiting time = (0 + 24 + 27)/3 = 17.

المسألة

The same processes arrive in the order P2, P3, P1 (slide 14).

المسألة

FCFS with arrival times (slide 15): P1 burst 7, arrives 0; P2 burst 12, arrives 3; P3 burst 2, arrives 6.

Using FCFS, three processes arrive at time 0 in the order A (5), B (2), C (8). What is the average waiting time?

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦13–15⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦13–15⁩

FCFS: fairness and drawbacksتقييم FCFS

First-Come, First-Served (FCFS)

  • FCFS is fair in the formal sense or human sense of fairness…
  • …but unfair in the sense that long jobs take priority over short jobs and unimportant jobs make important jobs wait.
  • Major drawback: the waiting time and the average turnaround time is often quite long.
  • Not useful in scheduling interactive users because it cannot guarantee good response time.
  • The code for FCFS scheduling is simple to write and understand.
تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦16–17⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦16–17⁩
ملاحظات حول المصدر (1)
  • الشريحة 16 تكتب «FCFC» والمقصود FCFS.

Shortest-Job-First (SJF)SJF

SJF
Associate with each process the length of its next CPU burst; schedule the process with the shortest time.

Nonpreemptiveغير قابلة للمقاطعة

  • Once the CPU is given to the process it cannot be preempted until it completes its CPU burst.

Preemptive (SRTF)قابلة للمقاطعة

  • If a new process arrives with CPU burst length less than the remaining time of the current process, preempt.
  • Known as Shortest-Remaining-Time-First (SRTF).
  • SJF is optimal — gives minimum average waiting time for a given set of processes.
  • The difficulty is knowing the length of the next CPU request.

المسألة

All at time 0: P1 = 6, P2 = 8, P3 = 7, P4 = 3 (slide 19).

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦18–19⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦18–19⁩

SJF with arrival times: nonpreemptive vs SRTFSJF وSRTF

Shortest-Job-First (SJF) and SRTF

Processes of slides 20 and 24
ProcessالعمليةArrival timeزمن الوصولBurst timeزمن الدفعة
P10.07
P22.04
P34.01
P45.04

المسألة

Nonpreemptive SJF (slide 20).

المسألة

Preemptive SJF — SRTF (slide 24).

P1 (burst 6) starts at time 0. P2 (burst 2) arrives at time 1. Under SRTF, what happens at time 1?

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦20, 24⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦20, 24⁩

Predicting the next CPU burstالمتوسط الأسي

المسألة

Exponential averaging (slide 21).

المعطيات

tₙ
actual length of the nth CPU burst
τₙ₊₁
predicted value for the next CPU burst
α
0 ≤ α ≤ 1

الصيغة

τₙ₊₁ = α tₙ + (1 − α) τₙ

النتيجة

The next burst is estimated from the length of previous CPU bursts.

α = 0

  • τₙ₊₁ = τₙ
  • Recent history does not count

α = 1

  • τₙ₊₁ = tₙ
  • Only the actual last CPU burst counts
  • Since both α and (1 − α) are ≤ 1, each successive term has less weight than its predecessor (slide 23).

In exponential averaging, which value of α means that only the actual last CPU burst counts?

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦21–23⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦21–23⁩

Priority schedulingالأولوية

  • A priority number (integer) is associated with each process; the CPU goes to the highest priority (smallest integer ≡ highest priority); preemptive or nonpreemptive.
  • SJF is a priority scheduling where priority is the predicted next CPU burst time.
Problem: starvationالتجويع
Low-priority processes may never execute.
Solution: agingالتقادم
As time progresses, increase the priority of the process.

المسألة

Nonpreemptive priority, all arrive at 0 (slide 26): P1 10 (pr. 3), P2 1 (pr. 1), P3 2 (pr. 4), P4 1 (pr. 5), P5 5 (pr. 2).

What is the solution to starvation in priority scheduling?

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦25–26⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦25–26⁩

Round Robin (RR)التناوب الدائري

  • Each process gets a small unit of CPU time (time quantum), usually 10–100 milliseconds; after it elapses, the process is preempted and added to the end of the ready queue.
  • n processes, quantum q → each gets 1/n of the CPU time in chunks of at most q; no process waits more than (n − 1)q time units.
  • Performance: q large ⇒ FIFO; q small ⇒ q must be large with respect to context switch, otherwise overhead is too high.

المسألة

RR with q = 4: P1 = 24, P2 = 3, P3 = 3 (slide 28).

Round robin with 5 processes in the ready queue and a quantum of 20 ms. What is the longest any process waits before its next turn?

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦27–28⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦27–28⁩

Time quantum and context switch timeالكم وتبديل السياق

Round Robin (RR)

Process time = 10 (slide 29)
QuantumالكمContext switchesعدد تبديلات السياق
120
61
19
تفاصيل المصدرشرائح الفصل 2a · الشريحة ⁦29⁩
المصدر
شرائح الفصل 2a · الشريحة ⁦29⁩

Multilevel queueالطوابير متعددة المستويات

  • The ready queue is partitioned into separate queues, e.g., foreground (interactive) and background (batch).
  • Each queue has its own scheduling algorithm: foreground — RR; background — FCFS.
Scheduling between the queues (slide 31)

Fixed priority preemptiveأولوية ثابتة

  • Serve all from foreground, then from background
  • Possibility of starvation

Time sliceتقسيم الوقت

  • Each queue gets a certain amount of CPU time to schedule amongst its processes
  • E.g., 80% to foreground in RR, 20% to background in FCFS

A multilevel queue serves all foreground processes first, then background ones. What risk does this create?

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦31–32⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦31–32⁩

Multilevel feedback queueالتغذية الراجعة

  • A process can move between the various queues; aging can be implemented this way.

Example: Q0 (RR, 8 ms), Q1 (RR, 16 ms), Q2 (FCFS) — slide 33

  1. 1

    A new job enters Q0; when it gains the CPU it receives 8 ms.

  2. 2

    If it does not finish in 8 ms, it is moved to Q1, served RR, and receives 16 additional ms.

  3. 3

    If it still does not complete (in 24 ms), it is preempted and moved to Q2.

  4. 4

    At Q2 the job is served FCFS.

Notes (slide 34)

  • Short processes get served faster (higher priority) → more responsive.
  • Long processes (CPU bound) sink to the bottom → served FCFS → more throughput.

A multilevel feedback queue has Q0 (RR, quantum 8 ms), Q1 (RR, quantum 16 ms) and Q2 (FCFS). A job needs 30 ms of CPU. Where does it finish?

تفاصيل المصدرشرائح الفصل 2a · الشرائح ⁦33–35⁩
المصدر
شرائح الفصل 2a · الشرائح ⁦33–35⁩

Parameters of a multilevel feedback queueمعاملات المجدول

  1. 1Number of queues
  2. 2Scheduling algorithms for each queue
  3. 3Method used to determine when to upgrade a process
  4. 4Method used to determine when to demote a process
  5. 5Method used to determine which queue a process will enter when that process needs service
تفاصيل المصدرشرائح الفصل 2a · الشريحة ⁦35⁩
المصدر
شرائح الفصل 2a · الشريحة ⁦35⁩

الخلاصة

تفاصيل المصدر
المصدر
شرائح الفصل 2a

خريطة الفصل

    1. 1مقدمة الفصل
  1. Basic concepts: CPU–I/O burst cycleالمفاهيم الأساسية: دورة الدفعات

    1. 2CPU–I/O burst cycleدورة الدفعات
  2. The CPU scheduler: preemptive vs nonpreemptiveمجدول المعالج: بالمقاطعة وبدونها

    1. 3When does the CPU scheduler decide?قرارات الجدولة
    2. 4Nonpreemptive vs preemptiveالمقاطعة وعدمها
    1. 5The dispatcherالمُرسِل
    1. 6Scheduling criteriaمعايير الجدولة
  3. First-Come, First-Served (FCFS)الأسبق وصولًا يُخدم أولًا (FCFS)

    1. 7FCFS and the convoy effectFCFS
    2. 8FCFS: fairness and drawbacksتقييم FCFS
  4. Shortest-Job-First (SJF) and SRTFالأقصر مهمة أولًا (SJF) وSRTF

    1. 9Shortest-Job-First (SJF)SJF
    2. 10SJF with arrival times: nonpreemptive vs SRTFSJF وSRTF
  5. Predicting the next CPU burst: exponential averagingتقدير طول الدفعة التالية: المتوسط الأسي

    1. 11Predicting the next CPU burstالمتوسط الأسي
    1. 12Priority schedulingالأولوية
  6. Round Robin (RR)التناوب الدائري (Round Robin)

    1. 13Round Robin (RR)التناوب الدائري
    2. 14Time quantum and context switch timeالكم وتبديل السياق
    1. 15Multilevel queueالطوابير متعددة المستويات
  7. Multilevel feedback queueالطوابير متعددة المستويات ذات التغذية الراجعة

    1. 16Multilevel feedback queueالتغذية الراجعة
    2. 17Parameters of a multilevel feedback queueمعاملات المجدول
    1. 18الخلاصة