Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Ch # 5 CPU SCHEDULING

Basic Concepts:
 Almost all programs have some alternating cycle of CPU number crunching and waiting for I/O of
some kind. ( Even a simple fetch from memory takes a long time relative to CPU speeds. )
 In a simple system running a single process, the time spent waiting for I/O is wasted, and those CPU
cycles are lost forever.
 A scheduling system allows one process to use the CPU while another is waiting for I/O, thereby
making full use of otherwise lost CPU cycles.
 The challenge is to make the overall system as "efficient" and "fair" as possible, subject to varying
and often dynamic conditions, and where "efficient" and "fair" are somewhat subjective terms, often
subject to shifting priority policies.

Scheduling Criteria:
Each scheduling algorithm favors particular criteria:

1. CPU utilization (maximize)


2. Throughput: number of processes which complete execution per time unit (maximize)
3. Turnaround Time (TA): total amount of time to execute a particular process (minimize)
4. Waiting Time: amount of time a process has been waiting in the ready queue (minimize)
5. Response Time: amount of time it takes from when a request is submitted to when the response is
produced (minimize); does not include the time for a response to be output.

CPU Scheduling Algorithms


1. First-Come First Serve (FCFS or FIFO) (non-preemptive)
2. Priority Scheduling
3. Shortest Job First
i. (SJF; non-preemptive)
ii. or Shortest Remaining Time First (SRTF; preemptive))
4. Round Robin Scheduling (preemptive)
5. Multi-level Queue Scheduling
6. Multi-level Feedback Queue Scheduling

1. First-Come First-Serve Scheduling, FCFS

 FCFS is very simple - Just a FIFO queue, like customers waiting in line at the bank or the post office
or at a copying machine.
 Unfortunately, however, FCFS can yield some very long average wait times, particularly if the first
process to get there takes a long time. For example, consider the following three processes:

Example # 1
Processes Burst Time (BT) Arrival Time(AT)
P1 24 0
P2 3 0
P3 3 0
∑ BT = 30

Q: Calculate Average Waiting Time (A.W.T) and Average Turnaround Time (A.T.T)?

Solution:
Gant Chart

Waiting Time (WT) = W.T – A.T


Turnaround Time (T.A.T)= T.A.T – A.T

Waiting Time for P1 = 0-0=0


Waiting Time for P2 = 24-0=24
Waiting Time for P3 = 27-0=27

Average Waiting Time: (0+24+27)/3 = 17

Turnaround Time for P1 = 24-0=24


Turnaround Time for P2 = 27-0=27
Turnaround Time for P3 = 30-0=30

Average Turnaround Time: (24+27+30) / 3 = 27

Example # 2

Processes Burst Time (BT) Arrival Time(AT)


P1 12 0
P2 6 1
P3 9 4
∑ BT = 27

Q: Calculate Average Waiting Time (A.W.T) and Average Turnaround Time (A.T.T)?

Do it yourself.

2. Priority Scheduling
 Priority scheduling is a more general case of SJF, in which each job is assigned a priority and the job
with the highest priority gets scheduled first. ( SJF uses the inverse of the next expected burst time as
its priority - The smaller the expected burst, the higher the priority. )
 Note that in practice, priorities are implemented using integers within a fixed range, but there is no
agreed-upon convention as to whether "high" priorities use large numbers or small numbers. This
book uses low number for high priorities, with 0 being the highest possible priority.

Example:

Processes Burst Time (BT) Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
∑ BT = 19

Q: Calculate Average Waiting Time (A.W.T) and Average Turnaround Time (A.T.T)?

Solution:

Gant Chart

Average Waiting Time: (6+0+16+18+1)/5 = 8.2

Average Turnaround Time: (1+6+16+18+19)/5 = 12

3. Shortest Job First Scheduling

(i) Shortest-Job-First (SJF) Scheduling (Non Pre-emptive Version)


A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling algorithm. This algorithm
associates with each process the length of the process's next CPU burst. When the CPU is available, it is
assigned to the process that has the smallest next CPU burst. If the next CPU bursts of two processes are the
same, FCFS scheduling is used to break the tie. Note that a more appropriate term for this scheduling method
would be the shortest-next-CPU-burst algorithm, because scheduling depends on the length of the next CPU
burst of a process, rather than its total length.
The waiting time is 3 milliseconds for process P1, 16 milliseconds for process P2, 9 milliseconds for
process P3, and 0 milliseconds for process P4 . Thus, the average waiting time is (3 + 16 + 9 + 0) I 4 = 7
milliseconds.
Average Turnaround Time can be calculated as: (9+24+16+3) / 4 = 13

(ii) Shortest-Remaining-Time-First (SRTF) Scheduling (Pre-emptive Version)


 It is the preemptive version of the SJN algorithm.
 The processor is allocated to the job closest to completion but it can be preempted by a newer ready
job with shorter time to completion.
 Impossible to implement in interactive systems where required CPU time is not known.
 It is often used in batch environments where short jobs need to give preference.
Average Turnaround Time = [(17-1-0)+(5-1)+(26-2)+(10-3)]/4=12.75

4. Round Robin Scheduling


 Round Robin is the preemptive process scheduling algorithm.
 Each process is provided a fix time to execute, it is called a quantum.
 Once a process is executed for a given time period, it is preempted and other process executes for a
given time period.
 Context switching is used to save states of preempted processes.

You might also like