Lec 3,4
Lec 3,4
Ready Queue
Process need to
be executed
CPU scheduling
• The method to select a process from the ready queue to
be executed by CPU whenever the CPU becomes idle.
• Some Examples:
– First Come First Serviced (FCFS) scheduling.
– Shortest Job First (SJF) scheduling.
– Priority scheduling. FCFS
SJF
CPU Scheduling Priority
Algorithm
Process 1
Ready
Process 2
Queue CPU
Process 3 ? Dispatcher
Memory
Q: Explain the main differences between
preemptive and non preemptive scheduling?
Sol:
Preemptive scheduling:
Allows releasing current executing process from CPU
when another higher priority process comes and need
execution.
Non-preemptive scheduling:
Once the CPU has been allocated to a process The
process keeps the CPU until it release the CPU .
Preemptive
Scheduling
CPU
Non- Preemptive
Scheduling
CPU
Q: discuss in details, what is meant by the
following parameters:
CPU utilization.
System throughput.
Turnaround time.
Waiting time.
Response time.
Turnaround time:
The total time needed for process execution (from the time of
submission to the time of completion).
Waiting time:
The sum of all periods the process spends waiting in the ready
queue.
Response time.
It is the time from the submission of a process until the first
response is produced (the time the process takes to start
responding).
It is desirable to:
• Maximize:
– CPU utilization.
– System throughput.
• Minimize:
– Turnaround time.
– Waiting time.
– Response time.
First Come First Serviced (FCFS)
Ready queue
FCFS Scheduling
CPU
Consider the following set of processes, with the length of the CPU
burst (Execution) time given in milliseconds:
Process Burst Time
The processes arrive in the order 1 P1 24
P1, P2, P3. All at time 0. 2 P2 3
3 P3 3
• Gant chart:
Process P1 P2 P3
Waiting Time (WT) 0 24 27
Turnaround Time (TAT) 24 27 30
+
Execution
• Hence, average waiting time= (0+24+27)/3=17 milliseconds Time
Repeat the previous example, assuming that the processes arrive in
the order P2, P3, P1. All at time 0.
Process P1 P2 P3
Waiting Time (WT) 6 0 3
Turnaround Time (TAT) 30 3 6
Sol:
Because:
SJF Scheduling
10 5 18 7
X
18 10 7 5
CPU
Note: numbers indicates the process execution time
Consider the following set of processes, with the length of the
CPU burst time given in milliseconds:
Process Burst Time
The processes arrive in the order P1 6
1. Using FCFS
• Gant chart:
Process P1 P2 P3 P4
Waiting Time (WT) 0 6 14 21
Turnaround Time (TAT) 6 14 21 24
Sol:
Because: by moving a short process before a long one, the waiting time of the
short process decreases more than it increases the waiting time of the long
process. Hence, the average waiting time decreases.
P1 P2 P2 P1
0 30 32 0 2 32
Waiting time(P1)=0 Waiting time(P1)=2
Waiting time(P2)=30 Waiting time(P2)=0
Average waiting time=(0+30)/2=15 Average waiting time=(0+2)/2=1
Shortest-Remaining-Time-First (SRTF)
SRTF Scheduling
2 10 7 5 3
4
CPU
Consider the following set of processes, with the length of the CPU
burst time given in milliseconds:
Process Burst Time Arrival Time
The processes arrive in the order P1 7 0
P1, P2, P3, P4. as shown in table. P2 4 2
P3 1 4
P4 4 5
1. Using SJF
• Gant chart:
Process P1 P2 P3 P4
Waiting Time (WT) 0 6 3 7
Turnaround Time (TAT) 7 10 4 11
Priority Scheduling
10 5 18 7
X
18 10 7 5
CPU
Note: numbers indicates the process priority
Problems with Priority scheduling
Very lowVery
priority
low process
priority process
8 28
26 30
8 5 4 2
Starvation
Aging
Consider the following set of processes, with the length of the CPU
burst time given in milliseconds:
Process Burst Time priority
• Gant chart:
P1 5 3 0
P2 9 4 2
P3 4 1 3
P4 2 2 5
P5 1 5 6
P6 8 2 8
– Preemptive priority.
– Non-preemptive priority.
Round Robin scheduling
• Allocate the CPU for one Quantum time (also called
time slice) Q to each process in the ready queue.
• This scheme is repeated until all processes are
finished.
• A new process is added to the end of the ready
queue.
Q Q
Q Q
CPU
Consider the following set of processes, with the length of the CPU burst
time given in milliseconds:
Process Burst Time
The processes arrive in the order P1 24
P1, P2, P3. All at time 0. P2 3
use RR scheduling with Q=2 and Q=4
P3 3
RR with Q=4
• Gant chart:
Process P1 P2 P3
Waiting Time (WT) 6 4 7
Turnaround Time (TAT) 30 7 10
• Gant chart:
Process P1 P2 P3
Waiting Time (WT) 6 6 7
Turnaround Time (TAT) 30 9 10
Sol:
CPU
Queue 0
Queue 1
Queue 2
Solution
Time Available Processes Allocated Process
0 P1(10) at Q1 P1
8 P2(26) at Q1 P2
P1(2) at Q2
16 P3(7) at Q1 P3
P1(2) at Q2
P2(18) at Q2
23 P1(2) at Q2 P1
P2(18) at Q2
25 P2(18) at Q2 P2
41 P2(2) at Q3 P2
43 Finished
P1 P2 P3
WT 15 12 4
TAT 25 38 11
Queue 0
Queue 1
Queue 2
In multi-level Queuing Scheduling with feedback, using 3 queues Q1,
Q2, and Q3 with the corresponding quantum times 10, 20, FCFS
respectively. Show how to execute the following processes;
P4 2 30
P5 8 37
P6 40 41
Any Questions?