Docsity

Notes on CPU Scheduling - Operating Systems | CSC 4103, Study notes of Operating Systems

Partial preview of the text

Download Notes on CPU Scheduling - Operating Systems | CSC 4103 and more Study notes Operating Systems in PDF only on Docsity!

CPU Scheduling

Source: Operating System Concepts by by Silberschatz, Galvin and Gagne

CPU Scheduling

 CPU scheduling is fundamental to multiprogramming OS  Deals with problem of deciding which of processes in the ready queue is to be allocated the CPU.

 What we cover:  Basic concepts  Scheduling criteria  Scheduling algorithms  Multiple-processor scheduling  Thread scheduling  Real-time scheduling  Algorithm evaluation  OS examples

Histogram of CPU-Burst Times

 Durations of CPU bursts have a typical frequency curve (exponential or hyper-exponential):  many short bursts – an I/O bound program  a few long bursts – a CPU bound program.

CPU Scheduler

 CPU scheduler (also called short-term scheduler) selects from among the processes in ready queue, and allocates the CPU to one of them.

 CPU scheduling decision may take place when a process:

  1. Switches from running to waiting state.
  2. Switches from running to ready state.
  3. Switches from waiting to ready.
  4. Terminates.

 Scheduling under 1 and 4 is nonpreemptive.

 Once the CPU is allocated to a process, the process keeps the CPU until it switches to waiting state or terminates.

 All other scheduling is preemptive.

 Incurs a cost.

Scheduling Criteria

Criteria for comparing CPU-scheduling algorithms:

 CPU utilization – keep the CPU as busy as possible (0 to 100% efficiency).

 Throughput – # of processes that complete their execution per time unit

 Turnaround time – amount of time to execute a particular process  waiting for memory + ready queue + execution + I/O

 Waiting time – amount of time a process waits in the ready queue

 Response time – amount of time it takes from when a request was submitted until the first response is produced.

Optimization Criteria

 Max CPU utilization

 Max throughput

 Min turnaround time

 Min waiting time

 Min response time

 Optimize the max or min values or average measure or variance.

FCFS Scheduling (Cont.)

Process Burst Time (ms) P 1 24 P 2 3 P 3 3

 Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is:

 Average waiting time: (0 + 24 + 27)/3 = 17 milliseconds

 Average turnaround time =?

P 1 P 2 P 3

0 24 27 30

FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order

P 2 , P 3 , P 1

 The Gantt chart for the schedule is:

 Average waiting time: (6 + 0 + 3)/3 = 3 ms

 Much better than previous case.

 Convoy effect - short process behind long process

P 2 P 3 P 1

0 3 6 30

CSC 4103: Operating System 5.13 BB Karki, LSU

Determining Length of Next CPU Burst

 Challenge is to know the length of the next CPU request:

 Approximate prediction of the length of next CPU burst:  from the lengths of previous CPU bursts by using exponential averaging  running average of each burst for each process.

4. Define :

2. predictedvaluefor thenextCPU burst

1. actual(observed)lengthof CPU burst

1

α α

τ (^) n

th

t n n

€

τ (^) n + 1 = α tn + (^) ( 1 − α) τ (^) n

Examples of Exponential Averaging

α =

τn+1 = τn Recent history does not count.

α =

τn+1 = tn Only the actual last CPU burst counts.

If we expand the formula, we get:

τn+1 = α tn + (1 - α) α tn-1 + …

  • (1 - α )^ j^ α tn-j + …
  • (1 - α )^ n+1 τ 0

Since both α and (1 - α) are less than or equal to 1, each successive term has less weight than its predecessor.

SJF Scheduling (Cont.)

 Two schemes:

 nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst.  preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is known as the Shortest-Remaining-Time-First ( SRTF ).

 Preemptive improves average waiting time.

Process Arrival Time Burst Time P 1 0 7 P 2 2 4 P 3 4 1 P 4 5 4

 SJF (non-preemptive)

 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 ms.

Example of Non-Preemptive SJF

P 1 P 3 P 2

0 3 7 16

P 4

8 12

Priority Scheduling

 A priority is associated with each process, and the CPU is allocated to the process with the highest priority.  smallest integer ≡ highest priority.

 SJF is a priority scheduling where priority is the inverse of the predicted next CPU burst time.

Process Burst Time Priority P 1 10 3 P 2 1 1 P 3 2 4 P 4 1 5 P 5 5 2

Average waiting time is 8.2 ms

P 2 P 5 P 1 P 3

0 1 6 16 18 19

P 4

Priority Scheduling

 Priorities can be defined internally or externally

 Priority scheduling can be preemptive or non-preemptive

 Problem ≡ Starvation

Low priority processes may never be executed. e.g., a low-priority process submitted in 1967 never ran until the shutdown of IBM 7094 at MIT in 1973.

 Solution ≡ Aging

As time progresses, increase the priority of the process.