0% found this document useful (0 votes)
34 views23 pages

Operating System: Cset209

The document discusses different CPU scheduling algorithms including First Come First Served (FCFS) and Shortest Job First (SJF). It defines key terms used in CPU scheduling like arrival time, burst time, completion time, turnaround time, waiting time and response time. It provides examples to illustrate how FCFS and SJF scheduling works and compares their performance. FCFS is simple but can result in longer wait times due to convoy effect where shorter jobs must wait for longer jobs. SJF aims to minimize wait times but is difficult to implement as burst times are unknown.

Uploaded by

xpershan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views23 pages

Operating System: Cset209

The document discusses different CPU scheduling algorithms including First Come First Served (FCFS) and Shortest Job First (SJF). It defines key terms used in CPU scheduling like arrival time, burst time, completion time, turnaround time, waiting time and response time. It provides examples to illustrate how FCFS and SJF scheduling works and compares their performance. FCFS is simple but can result in longer wait times due to convoy effect where shorter jobs must wait for longer jobs. SJF aims to minimize wait times but is difficult to implement as burst times are unknown.

Uploaded by

xpershan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

OPERATING

SYSTEM: CSET209
S

CONTENTS COVERED

 CPU Scheduling Algorithms


 First Come First Served (FCFS)
 Shortest-Job-First (SJF)
 Shortest Remaining Time First (SRTF)
DIFFERENT TIMES NOTATION IN CPU SCHEDULING
1. Arrival Time (AT):
2. Burst Time (BT):
3. Completion Time (CT): Time at which process completes.
4. Turn Around Time (TAT): Total time spent by the process in the system.
5. Waiting Time (WT): Time spend in the ready queue waiting their turn.
6. Response time (RT): Response time is the time spent when the process is in the
ready state and gets the CPU for the first time.

Turn Around Time = Completion Time -


Waiting Time = Turnaround time -

The average TAT and WT are determined by summing the respective TAT and WT of all
the processes and divided the sum by the total number of processes (n).
METRICS TO MEASURE PERFORMANCE OF A CPU SCHEDULING ALGO

Many criteria have been suggested for comparing CPU


scheduling algorithms, including:
 CPU utilization - CPU should be kept as busy as possible

 Throughput - Number of processes completed per unit time


is called throughput. ? ?
CPU Utilization Turnaround time
 Response time - Response time is the time from the Throughput Waiting Time
submission of a request until the first response is produced. Response time
TWO TYPES CPU SCHEDULING

There are mainly two types of CPU scheduling


FCFS SCHEDULING

 First come first serve (FCFS) scheduling algorithm simply schedules the jobs according
to their arrival time. The job which comes first in the ready queue will get the CPU first.
The lesser the arrival time of the job, the sooner will the job get the CPU.
Advantages of FCFS
• Simple and Easy to implement.
Disadvantages of FCFS
 Non-preemptive
 Average Waiting Time is not optimal
 Cannot utilize resources in parallel (Results in Convoy effect )
(FCFS) SCHEDULING: EXAMPLE 1

The average TAT and WT are determined by summing the respective TAT and WT of all
the processes and divided the sum by the total number of processes.
(FCFS) SCHEDULING: EXAMPLE 1

Here we have 5 processes with process ID P0, P1, P2, P3 and P4. P0 arrives at time 0,
P1 at time 1, P2 at time 2, P3 arrives at time 3 and Process P4 arrives at time 4 in the
ready queue. The processes and their respective Arrival and Burst time are given in
the following table.

Process ID Arrival Time Burst Time Completion Time Turn Around Waiting Time
(millisecond) Time (TAT) (WT)

P0 0 2

P1 1 6

P2 2 4

P3 3 9

P4 6 12
FCFS SCHEDULING
(FCFS) SCHEDULING: EXAMPLE 1

Process ID Arrival Time Burst Time Completion Time Turn Around Waiting Time
(millisecond) Time (TAT) (WT)

P0 0 2 2 2 0

P1 1 6 8 7 1

P2 2 4 12 10 6

P3 3 9 21 18 9

P4 6 12 33 29 17

Avg TAT Time=66/5 = 13.2 msec

Avg Waiting Time=33/5 = 6.6 msec


(FCFS) SCHEDULING: EXAMPLE 2

Example: 2

Process Arrival Time Burst Time (msec)


P1 0 2
P2 2 4
P3 1 2
P4 3 1

Calculate following:
1. Calculate waiting time for all process
2. Average waiting time
3. Calculate turnaround time for all process
4. Average turnaround time
(FCFS) SCHEDULING: EXAMPLE 2

P1 P3 P2 P4

P1 P3 P2 P4

0 1 2 3 4 8 9

Waiting time of P1 = 0 msec; P2 = 2 msec; P3 = 1 ; P4 = 5 msec;


Average waiting time: (0 + 2 + 1+5)/4 = 2 msec

Turn Around time of P1 = 2 msec; P2 = 6 msec; P3 = 3 msec; P4 = 6 msec;


Average turn around time: (2 + 6 + 3+6)/4 = 4.25 msec
ILLUSTRATING CONVOY EFFECT IN FCFS
Example: Consider 3 processes named as P1, P2 and P3. The Burt Time of process P1
is highest.
Case 1: In the First scenario, The Process P1 arrives at the first in the queue although;
the burst time of the process is the highest among all. Since, the Scheduling algorithm,
we are following is FCFS hence the CPU will execute the Process P1 first. In this
scheduling, the average waiting time of the system will be very high. That is because of
the convoy effect. The other processes P2, P3 have to wait for their turn for 40 units of
time although their burst time is very low. This schedule suffers from starvation.
Process ID Arrival Burst Completion Turn Waiting
Time Time Time Around Time
Time
Avg waiting Time = 81/3=27 msec
1 0 40 40 40 0

2 1 3 43 42 39

3 1 1 44 43 42
ILLUSTRATING CONVOY EFFECT IN FCFS

 Case 2: In the Second scenario, If Process P1 would have arrived at the last of the
queue and the other processes P2 and P3 at earlier then the problem of starvation
would not be there. Following example shows the deviation in the waiting times of
both the scenarios. Although the length of the schedule is same that is 44 units but
the waiting time will be lesser in this schedule.

Process Arrival Burst Completi Turn Waiting


ID Time Time on Time Around Time
Time

Avg Waiting Time=6/3 =2 msec 1 1 40 44 43 3


2 0 3 3 3 0
3 0 1 4 4 3
SHORTEST-JOB-FIRST (SJR) SCHEDULING
 In SJF scheduling, the process with the lowest burst time, among the list of available processes in the
ready queue, is going to be scheduled next. However, it is very difficult to predict the burst time needed
for a process hence this algorithm is very difficult to implement in the system.
Advantages of SJF
• Maximum throughput
• Minimum average waiting and turnaround time
 Disadvantages of SJF
• May suffer with the problem of starvation
• It is not implementable because the exact Burst time for a process can't be known in advance.
Two schemes:
 Non-preemptive – 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 know as the
Shortest-Remaining-Time-First (SRTF).
 SJF is optimal – gives minimum average waiting time for a given set of processes.
SHORTEST-JOB-FIRST (SJR) SCHEDULING
Shortest-Job-First (SJF) Example 1

EXAMPLE OF NON-PREEMPTIVE SJF

Process Arrival Time Burst Time


P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
 SJF (non-preemptive)

P1 P3 P2 P4

0 3 7 8 12 16

 Average waiting time = (0 + 6 + 3 + 7)/4 = 4


EXAMPLE 2: NON-PREEMPTIVE SJF
ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


Preemptive SRTF Example 1
 Consider there are three jobs P1, P2 and P3. P1 arrives at time unit 0; it will be scheduled
first for the time until the next process arrives. P2 arrives at 1 unit of time. Its burst time is
4 units which is least among the jobs in the queue. Hence it will be scheduled next.
 At time 2, P3 will arrive with burst time 9. Since remaining burst time of P2 is 3 units
which are least among the available jobs. Hence the processor will continue its execution
till its completion. Because all the jobs have been arrived so no preemption will be done
now and all the jobs will be executed till the completion according to SJF.
Process Arrival Burst Completi Turn Waiting
ID Time Time on Time Around Time
Time

1 0 9 13 13 4
2 1 4 5 4 0
Avt TAT = 37/3=12.33 msec
Avg Waiting Time = (4+0+11)/3 = 5 msec. 3 2 9 22 20 11
PREEMPTIVE SRTF EXAMPLE 2
 Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessA arri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec


THANK YOU
?

You might also like