ICS 431-Ch5-Processes Scheduling
ICS 431-Ch5-Processes Scheduling
Weeks 6 - 7
Ch. 5: Processes Scheduling
Dr. Tarek Helmy El-Basuny
Dr. Tarek Helmy, ICS-KFUPM
Ch: 5 Process Scheduling
• Schedulers Overview (Short, Medium and Long)
• Time Quantum/Burst Time and Context Switch
• Scheduler, Dispatcher and Swapper modules in the OS
• Basic Assumptions for Process Scheduling
• Types of Short-term Scheduling Algorithms (Preemptive, and Non-preemptive)
– Scheduling decision and the Types of processes
• Evaluation Criteria of Scheduling Algorithms
• Scheduling Algorithms
– First-Come, First-Served (FCFS)
– Shortest Job First (SJF)
– Shortest Remaining Time First (SRTF)
– Process Burst Length Prediction
– Round Robin
– Priority-Based
– Multilevel Queue
– Multilevel Feedback Queue
• Scheduling Algorithms Evaluation
– Deterministic and Queuing models
– Simulations, and Implementation
• Scheduling Polices in Different OS
• Multiprocessor Systems: Just Introduction, Scheduling of Multiprocessors systems
Ready Run
1. OS Kernel:
• Sets the PC register with the address of the Interrupt Handler (IH),
which is an address in the interrupt vector of OS kernel.
• Setting the mode bit to switch from user mode to kernel mode.
• Copies A’s state from CPU registers to A’s PCB.
• Sets A’s state to Ready or Blocked.
• Inserts a pointer of A’s PCB on Ready-Queue or I/O-Queue.
2. Scheduler: Selects a process B to run, based on its scheduling policy.
3. OS Kernel:
• Sets B’s state to Running,
• Copies B’s state from B’s PCB to CPU registers.
• Kernel transfers control to B and switches from kernel mode back to
user mode.
load A
add A } CPU CPU Burst
write to file
} CPU
Wait for I/O
CPU Burst
I/O Burst
load A
add A
read from file
} CPU CPU Burst
• Batch Process
– Users are not waiting at their terminals for a response time.
– Should scheduling be preemptive or non-Preemptive?
• Since a batch process does not need any responsiveness, the OS
can use non-preemptive and save the cost of context switch.
• Interactive Process
– Environment has interactive users
– Should scheduling be preemptive or non-Preemptive?
CPU.
• Scheduling decisions may take Admit Exit
Scheduler
place when a process: Dispatch
5. Terminates.
– Scheduling under 1 and 5 are
non-preemptive while 2, 3 and 4
are preemptive.
• Fairness
– Does the algorithm fairly share the CPU time among processes?
• CPU utilization: The fraction of time a CPU is executing processes. (ratio of in-use
time/total observation time). Note that CPU might be busy but in context switching.
• Resources utilization: Efficient utilization of all resources, are all recourses used?
• Throughput: # of processes that complete their execution per a time unit.
• Turnaround Time: Elapsed time from the submission/arrival of a process to its
completion.
• Waiting Time: Amount of time a process has been waiting in the ready queue,
not in the waiting I/O queues, may consist of several separate periods.
• Response Time (interactive users): Amount of time it takes from the submission
until the first response is given to the process, not output (for time-sharing
environment).
• Context Switches: # of context switches: Indication for the amount of overhead
• Complexity of the scheduling algorithm
– Indication of the time needed to take a decision and select the next process.
– It also indicates the amount of memory going to be used by the algorithm.
• Waiting Time: how much time the process spends on the ready queue
waiting to execute (this does not include time the process spends doing or
waiting for I/O).
– TW : Amount of time a process has been waiting in the ready queue, it
can be accumulated times, i.e. in round robin scheduling policy.
• Waiting Time starts when the process enters the ready queue (not when it
enters the system).
• Average Waiting Time (AWT of n processes) = Tw / n
• Response Time (of one process):
– TR= TStart to use CPU – T Arrive
• Average Response Time (ART of n processes) = TR / n
• Turnaround Time (of one process)
– TT = TFinish the process – T Arrive
• Average Turnaround Time (ATT of n processes) = TT / n
Dr. Tarek Helmy, ICS-KFUPM 14
Ideal Process Scheduling Algorithm
• Should Maximize
– CPU and resources utilization
• Keep the CPU and resources busy all the time.
– Throughput
• # processes completed within a time unit.
• Should Minimize
– Waiting Time:
• Minimizes it leads to minimizing the Turnaround time.
– Turnaround Time
• Minimizes it leads to maximizing the throughput.
– Response Time
• Minimizes it leads to maximizing the user’s satisfaction.
0 24 27 30
– Waiting time for P1 = 0; P2 = 24; P3 = 27
– Tw= TStart to use CPU – T Arrive
• Average waiting time: (0 + 24 + 27)/3 = 17
• Do you think, changing the order of processes arrival will
affect the average waiting time and the throughput?
• Since the arrival time of all processes is 0 and all have the same priority, the order
of processes will be used to resolve the confliction.
0 5 10 15 20 25 30
• Since the arrival time of all processes is 0 and all have the same priority, the order
of processes will be used to resolve the confliction.
P1
0 5 10 15 20 25 30
• Since the arrival time of all processes is 0 and all have the same priority, the order
of processes will be used to resolve the confliction.
P1 P2
0 5 10 15 20 25 30
• Since the arrival time of all processes is 0 and all have the same priority, the order
of processes will be used to resolve the confliction.
P1 P2 P3
0 5 10 15 20 25 30
• Since the arrival time of all processes is 0 and all have the same priority, the order
of processes will be used to resolve the confliction.
P1 P2 P3 P4
0 5 10 15 20 25 30
• Since the arrival time of all processes is 0 and all have the same priority, the order
of processes will be used to resolve the confliction.
P1 P2 P3 P4 P5
0 5 10 15 20 25 30
• Since the arrival time of all processes is 0 and all have the same priority, the order
of processes will be used to resolve the confliction.
P1 P2 P3 P4 P5
0 5 10 15 20 25 30
• Shortest Job First (SJF) or Shortest Job Next (SJN) or Shortest Process
Next (SPN): Select the waiting process with the smallest execution/burst time
to execute first.
• How do we know the burst length of each process?
• Predict for each process the length of CPU time it needs. One of its limitations.
• Use these predicted lengths to schedule the process with the shortest time
first.
• Shortest-Job-First (SJF):
– It could be Non-Preemptive: once CPU is given to the process, it cannot
be preempted until the processes completes its burst time.
– Do scheduling when a process finishes;
– Assume arrival time 0 for all processes!, practically is not.
– Assume the same priorities for all processes!, practically is not.
– Different (expected) burst lengths, how about if two have the same length.
– It could be Preemptive: upon arrival of a shorter process than the
running one, the CPU can be preempted and given to another process.
• SJF is optimal: it gives minimum average waiting time for a given set of
processes. Let us verify that.
Dr. Tarek Helmy, ICS-KFUPM 27
SJF Policy Example
Proces s # Arriv al Time Burs t Length Priority
P1 0 6 1
P2 0 15 1
P3 0 3 1
P4 0 4 1
P5 0 2 1
P5
0 5 10 15 20 25 30
P5 P3
0 5 10 15 20 25 30
P5 P3 P4
0 5 10 15 20 25 30
P5 P3 P4 P1
0 5 10 15 20 25 30
P5 P3 P4 P1 P2
0 5 10 15 20 25 30
P5 P3 P4 P1 P2
0 5 10 15 20 25 30
• Slight modification
– Same priorities
0 3 7 8 12 16
P2 P3 P4
arrives arrives arrives
Dr. Tarek Helmy, ICS-KFUPM 35
SJF Example 2: Different Arrival Times
Proces s # Arriv al Time Burs t Length Priority
P1 0 6 1
P2 3 15 1
P3 5 3 1
P4 8 4 1
P5 14 2 1
0 5 10 15 20 25 30
P1
0 5 10 15 20 25 30
P2: 15
P1: 6 P2: 15 P3: 3
ready queue at time 5
P1 P3
0 5 10 15 20 25 30
P2: 15 P2: 15
P1: 6 P2: 15 P3: 3 P4: 4
P1 P3 P4
0 5 10 15 20 25 30
P2: 15 P2: 15
P1: 6 P2: 15 P3: 3 P4: 4 P2: 15
P1 P3 P4 P2
0 5 10 15 20 25 30
P2: 15 P2: 15
P1: 6 P2: 15 P3: 3 P4: 4 P2: 15 P5: 2
P1 P3 P4 P2 P5
0 5 10 15 20 25 30
P2: 15 P2: 15
P1: 6 P2: 15 P3: 3 P4: 4 P2: 15 P5: 2
P1 P3 P4 P2 P5
0 5 10 15 20 25 30
Pro ces s # W aiting Time R es p o ns e Turnaro und # o f C o ntex t
Time Time S w itches
P1 0 0 6 1
P2 13-3 = 10 13-3 = 10 28-3 = 25 1
P3 6-5 = 1 6-5 = 1 9-5 = 4 1
P4 9-8 = 1 9-8 = 1 13-8 = 5 1
P5 28-14 = 14 28-14 = 14 30-14 = 16 1
A v erag e 26/5 = 5. 2 26/5 = 5. 2 50/5 = 10 1
long b short
short b long
• Much better average waiting time for processes when we use SJF
than FCFS.
– Optimal with respect to the average waiting time.
• Non-preemptive
• Relies on knowing the length of the CPU bursts
– In general, it is difficult to impossible to get it accurately.
• Implementation is more complex than FCFS
• Time of selecting a process is variant.
– Linear w.r.t. number of processes in the ready queue
• Impractical due to burst length prediction problem
• Starvation is possible
– If new, short processes keep on arriving, long processes may
never be served.
• How do we overcome this problem?
Shortest-Remaining-Time-First (SRTF)
– Preemptive: If a new process arrives with burst length less than
remaining time of current executing process, preempt and make a
context switch.
– Remaining time: (The process burst length - the time the CPU has
already spent serving this process).
• A scheduling decision must be made when
– A process is done with its CPU burst
– A new process arrives in the ready queue
• Arrival time of new processes is important
• It is important to keep track of the processes currently in the ready queue
– The policy used here: preempted processes go to the end of the ready
queue.
– It may depend on another policy for implementation.
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
P1: 6
P1
0 5 10 15 20 25 30
P1: 3
P1: 6 P2: 15
P1 P1
0 5 10 15 20 25 30
P1: 3
P1: 6 P2: 15 P2: 15
P1 P1 P2
0 5 10 15 20 25 30
P1: 3 P2: 12
P1: 6 P2: 15 P2: 15 P3: 3
P1 P1 P2 P3
0 5 10 15 20 25 30
P1: 3 P2: 12
P1: 6 P2: 15 P2: 15 P3: 3 P2: 12
P1 P1 P2 P3 P2
0 5 10 15 20 25 30
P1 P1 P2 P3 P2 P4
0 5 10 15 20 25 30
P1 P1 P2 P3 P2 P4
0 5 10 15 20 25 30
P1 P1 P2 P3 P2 P4 P5
0 5 10 15 20 25 30
P1 P1 P2 P3 P2 P4 P5 P2
0 5 10 15 20 25 30
P1 P1 P2 P3 P2 P4 P5 P2
0 5 10 15 20 25 30
• With the same set of processes, we can compare the performance of the
FCFS, SJF and SRTF.
Proces s # Arriv al Time Burs t Length Priority
P1 0 6 1
P2 3 15 1
P3 9 3 1
P4 14 4 1
P5 17 2 1
• Practically, the CPU burst length of a process is not known and needs
to be predicted/estimated.
– Algorithms such as SJF, SRTF in their pure form can’t be used in
practical systems unless there is an algorithm to predict the next
process burst length. How?
• The past can be a good predictor of the future, moreover
– Recent bursts might be given more importance than older bursts.
• The CPU burst length can be estimated based on the previous CPU
bursts of the process.
• This requires analysis of the code to be executed while the
scheduling decision is made.
• That means, additional overhead during the scheduling decision
– Time to estimate next CPU burst of the process and calculate the remaining time.
– Memory space to keep values of recent CPU burst lengths.
• A generic formula to estimate the length of the next CPU burst from the
lengths of previous ones is:
• E i = * A i-1 + (1- ) * E i-1
• Ei: estimated burst length of a process at time i
• Ai - 1: actual burst length of a process at time (i -1)
• Ei-1: previously estimated burst of a process at time (i -1)
• : factor to balance the importance of recent and not so recent bursts
– If =0 then E i = E i-1
– If =1 then E i = A i -1
• At time ti we estimate the length of the next CPU burst based on information
we have about previous bursts according to:
E i = * A i -1 + (1- ) * E i -1
– We have to select an initial value for (here 0.75)
– For the first burst A0, we randomly set it, after that we use the measured
time for the previous burst A i - 1.
– The measured burst time may be significantly different from the estimate.
E i = * A i -1 + (1- ) * E i -1
Actual
12
10
burst length
Predicted
8
6
4
2
time
Process Burst-time
P1 24
P2 3 Set quantum at 4 milliseconds
P3 3
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1: 6
P1
0 5 10 15 20 25 30
P1 P2
0 5 10 15 20 25 30
P1: 3 P2: 12
P1: 6 P2: 15 P1: 3
P1 P2 P1
0 5 10 15 20 25 30
P1 P2 P1 P2
0 5 10 15 20 25 30
P1 P2 P1 P2 P3
0 5 10 15 20 25 30
P1 P2 P1 P2 P3 P2
0 5 10 15 20 25 30
P2: 6
P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2
P1: 6 P2: 15 P1: 3 P2: 12 P3: 3 P2: 9 P4: 4
P1 P2 P1 P2 P3 P2 P4
0 5 10 15 20 25 30
P2: 6 P4: 1
P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2 P2: 6
P1: 6 P2: 15 P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2
P1 P2 P1 P2 P3 P2 P4 P5
0 5 10 15 20 25 30
P2: 6 P4: 1
P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2 P2: 6 P4: 1
P1: 6 P2: 15 P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2 P2: 6
P1 P2 P1 P2 P3 P2 P4 P5 P2
0 5 10 15 20 25 30
P
P1 P2 P1 P2 P3 P2 P4 P5 P2
4
0 5 10 15 20 25 30
P2: 6 P4: 1
P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2 P2: 6 P4: 1 P2: 3
P1: 6 P2: 15 P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2 P2: 6 P4: 1 P2: 3
P
P1 P2 P1 P2 P3 P2 P4 P5 P2 P2
4
0 5 10 15 20 25 30
P2: 6 P4: 1
P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2 P2: 6 P4: 1 P2: 3
P1: 6 P2: 15 P1: 3 P2: 12 P3: 3 P2: 9 P4: 4 P5: 2 P2: 6 P4: 1 P2: 3
P
P1 P2 P1 P2 P3 P2 P4 P5 P2 P2
4
0 5 10 15 20 25 30
• Tries to give a fair share of CPU to each process (i.e. 1/N of the CPU time to
each process if there are N processes of the same type).
• The algorithm should compute the ratio of actual CPU time used to the CPU
time that the process should have been assigned (for a fair CPU assignment)
– Runs the process with the lowest ratio until the ratio moves above its closest competitor.
• Takes into account the owner of the process and the number of processes
per user when scheduling in multiuser system.
• Is CPU time shared among the users or processes fairly?
– Example: User 1 has 4 processes, A, B, C, D, and user 2 has 1 process
E. Then with round robin scheduling the execution sequence would be: A
E B E C E D E …..
P2 P5 P1 P3 P4
0 1 6 16 18 19
processes.
assignment.
– aging: if a process has not received service for a long time, its
priority is increased again.
Highest priority
System Processes
Interactive Processes
Background Processes
Batch Processes
Lowest priority
Possibility I
• If each queue has absolute priority over lower-priority queues then no process in the lower priority
queues could run unless the processes of the highest-priority queue were all done.
• For example, in the above figure no process in the batch queue could run unless the queues for
system processes, interactive processes, and interactive editing processes will all empty.
Possibility II
• If there is a time slice between the queues then each queue gets a certain amount of CPU times,
which can then schedule it among the processes in its queue. For instance;
– 30% of the CPU time to System Processes using Priority Scheduling Policy
– 25 % of CPU time for RT processes using RR.
– 20 % of CPU time for Interactive using SRT
– 15% of the CPU time to background queue using SRTF.
– 10% of the CPU time to Batch processes using FCFS
• Proposed Solution
– Feedback queues attempt to solve this by allowing a process to move
between various queues by:
• Separating processes with different CPU-burst characteristics.
• Leaving I/O-bound and interactive processes in the higher-priority
queues.
• Migrating a process waiting too long in a lower-priority queue to a
higher-priority queue (aging can be implemented this way).
Highest priority
Quantum = 2
Quantum = 4
Quantum = 8
FCFS
???
Lowest priority
Highest priority
Quantum = 2
Quantum = 4
Quantum = 8
FCFS
Lowest priority
• Three queues:
– Q0 – Time quantum 8 milliseconds
– Q1 – Time quantum 16 milliseconds
– Q2 – Time quantum , what is the appropriate scheduling
algorithm?
• Scheduling
– A new job enters queue Q0, when it gains CPU, it receives 8
milliseconds.
– If it does not finish in 8 milliseconds, it moves to queue Q1.
– At Q1, the job receives 16 milliseconds, if it still does not
complete, it is preempted and moved to queue Q2.
Q0 = 8
Q1 = 16
Q2 = FCFS
P1 P2 P3 P4 P1 P2 P4 P1 P4
0 8 16 21 29 45 47 63 67 69
• In Q0 (Quantum time = 8): P BT
– P1 uses 8 and needs 20 more P1 28
– P2 uses 8 and needs 2 more P2 10
– P3 uses 5 and terminates P3 5
– P4 uses 8 and needs 18 more P4 26
P1 P2 P3 P4 P1 P2 P4 P1 P4
0 8 16 21 29 45 47 63 67 69
• P1, P2, P4 in Q1 (Quantum time =16): P BT
– P1 uses 16 and needs 4 more; P1 20
– P2 uses 2 and terminates;
P2 2
– P4 uses 16 and needs 2 more;
P4 18
• So P1(4), P4(2) will go to Q2 (Quantum
time = )
– P1 uses 4 and terminates;
– P4 uses 2 and terminates.
P BT
• AWT, ART & ATT ?
P1 4
• You need to calculate them for all
processes. P4 2
• Evaluation methods
– Deterministic modeling
– Queuing models
– Simulations
– Implementation
• Deterministic modeling:
useful.
• Consider the following scenario with 3 processes P1, P2, and P3.
P1 P2 P3
2 46 56 70
SJF
P2 P3 P1
2 11 25 70
P1 P2 P3 P1 P3 P1 P1 P1
2 12 22 33 44 48 59 70 75
Arrival Terminate
Q1: D1=10
Terminate
Q2: D2=20
Terminate
Q3: D3= infinity
P1 P2 P3 P1 P3 P1
2 12 22 33 54 58 73
FCFS 56.33 4
SJF 34.33 1
RR 47.33 2
MLFQ 48.67 3
• Put the coded algorithm in the real system for evaluation under real
operating conditions.
• Difficulty
priority classes
levels
within
class
Important Difference:
P2
– May cause overheads if a processes
P3
migrates to a new processor.
P4
– Static scheduling reduces this problem.
systems.
Thank you
Any Questions?