CPU Scheduling
CPU Scheduling
Objectives
If a process having high priority frequently arrives in If a process with a long burst time is running CPU, then later
Starvation
the ready queue, a low priority process may starve coming process with less CPU burst time may starve
Overhead It has overheads of scheduling the processes It does not have overheads
Response Time Preemptive scheduling response time is less Non-preemptive scheduling response time is high
Decisions are made by the scheduler and are Decisions are made by the process itself and the OS just follows
Decision making
based on priority and time slice allocation the process’s instructions
Overhead Higher overhead due to frequent context switching Lower overhead since context switching is less frequent
Examples of preemptive scheduling are Round Examples of non-preemptive scheduling are First Come First
Examples
Robin and Shortest Remaining Time First Serve and Shortest Job First
Terminologies Used in CPU Scheduling
▪ Arrival Time: The time at which the process arrives in the ready queue.
▪ Completion Time: The time at which the process completes its
execution.
▪ Burst Time: Time required by a process for CPU execution.
▪ Turn Around Time: Time Difference between completion time and
arrival time.
▪ Turn Around Time = Completion Time – Arrival Time
▪ Waiting Time(W.T): Time Difference between turn around time and
burst time.
▪ Waiting Time = Turn Around Time – Burst Time
Things to Take Care While Designing a CPU Scheduling Algorithm
▪ Different CPU Scheduling algorithms have different structures and
the choice of a particular algorithm depends on a variety of factors.
▪ CPU Utilization: The main purpose of any CPU algorithm is to keep
the CPU as busy as possible. Theoretically, CPU usage can range from
0 to 100 but in a real-time system, it varies from 40 to 90 percent
depending on the system load.
▪ Throughput: The average CPU performance is the number of
processes performed and completed during each unit. This is called
throughput. The output may vary depending on the length or duration
of the processes.
▪ Turn Round Time: For a particular process, the important conditions are
how long it takes to perform that process. The time elapsed from the time
of process delivery to the time of completion is known as the conversion
time. Conversion time is the amount of time spent waiting for memory
access, waiting in line, using CPU and waiting for I/O.
▪ Waiting Time: The Scheduling algorithm does not affect the time required
to complete the process once it has started performing. It only affects the
waiting time of the process i.e. the time spent in the waiting process in the
ready queue.
▪ Response Time: In a collaborative system, turn around time is not the
best option. The process may produce something early and continue to
computing the new results while the previous results are released to the
user. Therefore another method is the time taken in the submission of the
application process until the first response is issued. This measure is
called response time.
Scheduling Criteria
▪ Waiting time is the total time spent by the process in the ready state
waiting for CPU. For example, consider the arrival time of all the below 3
processes to be 0 ms, 0 ms, and 2 ms and we are using the First Come
First Serve scheduling algorithm.
Then the waiting time for all the 3 processes will be:
P1: 0 ms
P2: 8 ms because P2 have to wait for the complete execution of P1
and arrival time of P2 is 0 ms.
P3: 13 ms becuase P3 will be executed after P1 and P2 i.e. after
8+7 = 15 ms and the arrival time of P3 is 2 ms. So, the waiting
time of P3 will be: 15-2 = 13 ms.
Waiting time = Turnaround time - Burst time
▪ There is a difference between waiting time and response time. Response time is the time
spent between the ready state and getting the CPU for the first time. But the waiting time
is the total time taken by the process in the ready state. Let's take an example of a
round-robin scheduling algorithm. The time quantum is 2 ms.
▪ In the above example, the response time of the process P2 is 2 ms because after 2 ms, the CPU
is allocated to P2 and the waiting time of the process P2 is 4 ms i.e turnaround time - burst
time (10 - 6 = 4 ms).
Turnaround time
▪ Turnaround time is the total amount of time spent by the process from coming in the ready
state for the first time to its completion.
▪ Turnaround time = Burst time + Waiting time
▪ or
▪ Turnaround time = Exit time - Arrival time
▪ There is a difference between waiting time and response time. Response time is the
time spent between the ready state and getting the CPU for the first time. But the
waiting time is the total time taken by the process in the ready state. Let's take an
example of a round-robin scheduling algorithm. The time quantum is 2 ms.
Let's take an example of a round-robin scheduling
algorithm. The time quantum is 2 ms.
▪ First Come, First Serve (FCFS) is one of the simplest types of CPU
scheduling algorithms. It is exactly what it sounds like: processes are
attended to in the order in which they arrive in the ready queue, much like
customers lining up at a grocery store.
▪ FCFS Scheduling is a non-preemptive algorithm, meaning once a process
starts running, it cannot be stopped until it voluntarily relinquishes the
CPU, typically when it terminates or performs I/O. This method schedules
processes in the order they arrive, without considering priority or other
factors.
How Does FCFS Work?
▪ The mechanics of FCFS are straightforward:
▪ Arrival: Processes enter the system and are placed in a queue in the
order they arrive.
▪ Execution: The CPU takes the first process from the front of the
queue, executes it until it is complete, and then removes it from the
queue.
▪ Repeat: The CPU takes the next process in the queue and repeats the
execution process.
Scenario 1: Processes with Same Arrival Time
▪ Consider the following table of arrival time and burst time for three
processes P1, P2 and P3
Process Arrival Time Burst Time
p1 0 5
p2 0 3
p3 0 8
Step-by-Step Execution:
1.P1 will start first and run for 5 units of time (from 0 to 5).
2.P2 will start next and run for 3 units of time (from 5 to 8).
3.P3 will run last, executing for 8 units (from 8 to 16).
▪ Turnaround Time = Completion Time - Arrival Time
▪ Waiting Time = Turnaround Time - Burst Time
Processe
AT BT CT TAT WT
s
P1 0 5 5 5-0 = 5 5-5 = 0
P2 0 3 8 8-0 = 8 8-3 = 5
P3 0 8 16 16-0 = 16 16-8 = 8
P1 5 ms 2 ms
P2 3 ms 0 ms
P3 4 ms 4 ms
Step-by-Step Execution:
1.P2 arrives at time 0 and runs for 3 units, so its completion time is:
Completion Time of P2=0+3=3
2. P1 arrives at time 2 but has to wait for P2 to finish. P1 starts at time 3 and runs
for 5 units. Its completion time is:
Completion Time of P1=3+5=8
3. P3 arrives at time 4 but has to wait for P1 to finish. P3 starts at time 8 and runs
for 4 units. Its completion time is:
Completion Time of P3=8+4=12
Completion Time Turnaround Time Waiting Time (WT =
Process
(CT) (TAT = CT – AT) TAT – BT)
P2 3 ms 3 ms 0 ms
P1 8 ms 6 ms 1 ms
P3 12 ms 8 ms 4 ms
P2 8 ms 2 ms
P3 3 ms 4 ms
Step-by-Step Execution:
P1 0 6 6 6-0 = 6 6-6 = 0
P2 2 8 17 17-2 = 15 15-8 = 7
P3 4 3 9 9-4 = 5 5-3 = 2
P2 8 ms 0 ms
P3 5 ms 0 ms
Step-by-Step Execution:
Time 0-5 (P3): P3 runs for 5 ms (total time left: 0 ms) as it has shortest
remaining time left.
Time 5-11 (P1): P1 runs for 6 ms (total time left: 0 ms) as it has shortest
remaining time left.
Time 11-19 (P2): P2 runs for 8 ms (total time left: 0 ms) as it has
shortest remaining time left.
Arrival Burst
Completion Turn Around
Process Time Time Waiting Time (WT)
Time (CT) Time (TAT)
(AT) (BT)
P1 0 6 11 11-0 = 11 11-6 = 5
P2 0 8 19 19-0 = 19 19-8 = 11
P3 0 5 5 5-0 = 5 5-5 = 0
P2 3 ms 1 ms
P3 7 ms 2 ms
Step-by-Step Execution:
Time 0-1 (P1): P1 runs for 1 ms (total time left: 5 ms) as it has shortest
remaining time left.
Time 1-4 (P2): P2 runs for 3 ms (total time left: 0 ms) as it has shortest
remaining time left among P1 and P2.
Time 4-9 (P1): P1 runs for 5 ms (total time left: 0 ms) as it has shortest
remaining time left among P1 and P3.
Time 9-16 (P3): P3 runs for 7 ms (total time left: 0 ms) as it has shortest
remaining time left.
Arrival Completio
Burst Time Turn Around Waiting Time
Process Time n
(BT) Time (TAT) (WT)
(AT) Time (CT)
P1 0 6 9 9-0 = 9 9-6 = 3
P2 1 3 4 4-1 = 3 3-3 = 0
P3 2 7 16 16-2 = 14 14-7 = 7
P2 5 ms 0 ms
P3 3 ms 0 ms
Step-by-Step Execution:
1.Time 0-2 (P1): P1 runs for 2 ms (total time left: 2 ms).
2.Time 2-4 (P2): P2 runs for 2 ms (total time left: 3 ms).
3.Time 4-6 (P3): P3 runs for 2 ms (total time left: 1 ms).
4.Time 6-8 (P1): P1 finishes its last 2 ms.
5.Time 8-10 (P2): P2 runs for another 2 ms (total time left: 1 ms).
6.Time 10-11 (P3): P3 finishes its last 1 ms.
7.Time 11-12 (P2): P2 finishes its last 1 ms.
•Turnaround Time = Completion Time - Arrival Time
•Waiting Time = Turnaround Time - Burst Time
Processes AT BT CT TAT WT
P1 0 4 8 8-0 = 8 8-4 = 4
P2 0 5 12 12-0 = 12 12-5 = 7
P3 0 3 11 11-0 = 11 11-3 = 8
P1 5 ms 0 ms
P2 2 ms 4 ms
P3 4 ms 5 ms
▪ Step-by-Step Execution:
▪ Time 0-2 (P1 Executes):
• P1 starts execution as it arrives at 0 ms.
• Runs for 2 ms; remaining burst time = 5 - 2 = 3 ms.
• Ready Queue: [P1].
▪ Time 2-4 (P1 Executes Again):
• P1 continues execution since no other process has arrived yet.
• Runs for 2 ms; remaining burst time = 3 - 2 = 1 ms.
• P2 arrive at 4 ms.
• Ready Queue: [P2, P1].
▪ Time 4-6 (P2 Executes):
• P2 starts execution as it arrives at 4 ms.
• Runs for 2 ms; remaining burst time = 2 - 2 = 0 ms.
• P3 arrive at 5ms
• Ready Queue: [P1, P3].
▪ Time 6-7 (P1 Executes):
• P1 starts execution.
• Runs for 1 ms; remaining burst time = 1 - 1 = 0 ms.
• Ready Queue: [P3].
▪ Time 7-9 (P3 Executes):
• P3 starts execution.
• Remaining burst time = 4 - 2 = 2 ms.
• Ready Queue: [P3].
▪ Time 9-11 (P3 Executes Again):
• P3 resumes execution and runs for 2 ms and complete its
execution
• Remaining burst time = 2 - 2 = 0 ms.
• Ready Queue: [].
Turnaround Time (TAT Waiting Time (WT =
Process Completion Time (CT)
= CT - AT) TAT - BT)
P1 7 ms 7 ms 2 ms
P2 6 ms 2 ms 0 ms
P3 11 ms 6 ms 1 ms
P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
If the CPU scheduling policy is Round Robin with time quantum = 2 unit,
calculate the average waiting time and average turn around time.
Ready Queue-
P5, P1, P2, P5, P4, P1, P3, P2, P1
▪ Average Turn Around time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6
unit
▪ Average waiting time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
If the CPU scheduling policy is Round Robin with time quantum = 2, calculate the
average waiting time and average turn around time.
▪ Ready Queue-
▪ P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1
▪ Turn Around time = Exit time – Arrival time
▪ Waiting time = Turn Around time – Burst time
Disadvantages-
P1 0 4 2
P2 1 2 1
P3 2 6 3
Step-by-Step Execution:
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
P1 4 4–0=4 4–4=0
P2 15 15 – 1 = 14 14 – 3 = 11
P3 12 12 – 2 = 10 10 – 1 = 9
P4 9 9–3=6 6–5=1
P5 11 11 – 4 = 7 7–2=5
If the CPU scheduling policy is priority preemptive, calculate the average waiting
time and average turn around time. (Higher number represents higher priority)
Process Id Arrival time Burst time Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
P1 15 15 – 0 = 15 15 – 4 = 11
P2 12 12 – 1 = 11 11 – 3 = 8
P3 3 3–2=1 1–1=0
P4 8 8–3=5 5–5=0
P5 10 10 – 4 = 6 6–2=4
▪ User Level Thread is a type of thread that is not created using system
calls.
▪ The kernel has no work in the management of user-level threads.
▪ User-level threads can be easily implemented by the user.
▪ In case when user-level threads are single-handed processes,
kernel-level thread manages them.
▪ Let’s look at the advantages and disadvantages of User-Level Thread.
Advantages of User-Level Threads
▪ Implementation of the User-Level Thread is easier than Kernel Level Thread.
▪ Context Switch Time is less in User Level Thread.
▪ User-Level Thread is more efficient than Kernel-Level Thread.
▪ Because of the presence of only Program Counter, Register Set, and Stack
Space, it has a simple representation.
Disadvantages of User-Level Threads
▪ The operating system is unaware of user-level threads, so kernel-level
optimizations, like load balancing across CPUs, are not utilized.
▪ If a user-level thread makes a blocking system call, the entire process (and all its
threads) is blocked, reducing efficiency.
▪ User-level thread scheduling is managed by the application, which can become
complex and may not be as optimized as kernel-level scheduling
Kernel Level Threads
Processes use more resources and hence Threads share resources and hence they are
they are termed as heavyweight processes. termed as lightweight processes.
Creation and termination times of processes Creation and termination times of threads are
are slower. faster compared to processes.
Eg: Opening two different browsers. Eg: Opening two tabs in the same browser.