Chapter 3
Chapter 3
When to schedule
Scheduling Criteria
Types of scheduling
Thread Scheduling
2
Introduction to CPU scheduling
• To make your day more logical and efficient, you work on a
schedule.
• An OS operates in a similar manner by scheduling tasks, improving
efficiency, reducing delays and wait times, and managing CPU
resources better.
• This activity is called process scheduling.
• CPU scheduling is the basis of multi-programmed OS.
• Multi-programmed computer
– Multiple processes running concurrently.
– Processes compete for the CPU.
– If there is a single CPU, a choice has to be made which process to
run next.
3
Cont..
The aim of process scheduling is to assign processes to
be executed by the processor or processors over time in
a way that meets the system objectives.
Fundamentally, scheduling is a matter of managing
queues to minimize delays and to optimize
performance.
Scheduling in batch systems was simple.
But in time sharing- algorithms are becoming more and
more complex.
4
When to schedule
When to Schedule?
A new process is created
– Since the parent and child processes are in ready state, decision needs
to be made whether to run the parent process or the child process.
A process exits
– That process can no longer run (since it no longer exists), so some other
process must be chosen from the set of ready processes.
A process blocks
– when a process blocks on I/O, on a semaphore, or for some other
reason, another process has to be selected to run.
– The reason for blocking may play a role in the selection of the next
process, but the scheduler doesn’t have enough information.
I/O interrupt
– Scheduler decides to run the newly ready process, continue the
interrupted process or run another process in the ready queue.
5
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible.
• Throughput – the number of processes that complete their
execution per time unit
• Turnaround time – amount of time to execute a particular
process.
• Waiting time – amount of time a process has been waiting
in the ready queue
• Response time – amount of time it takes from when a
request was submitted until the first response is produced,
not output (for time-sharing environment).
6
Types of scheduling
1. Non-preemptive Scheduling
• A scheduling algorithm picks a process to run and then just lets it
run until it terminates or voluntary releases the CPU.
9
Schedulers
Scheduler
• Part of operating system which selects the process
• Uses scheduling algorithm.
Their main task is to select the jobs to be submitted into
the system and to decide which process to run.
14
Scheduling Algorithm Goals
All systems
• Fairness - giving each process a fair share of the CPU
• Policy enforcement - seeing that stated policy is carried out
• Balance - keeping all parts of the system busy. (avoid
overload in 1 part)
Batch systems
• Throughput - maximize jobs per hour
• Turnaround time - minimize time between submission and
termination
• CPU utilization - keep the CPU busy all the time
15
Cont.…
Interactive systems
• Response time - respond to requests quickly
• Waiting Time (wt): for each process time spent in ready
queue.
• Proportionality - meet users’ expectations.
Real-time systems
– Meeting deadlines - avoid losing data
– Predictability - avoid quality degradation in multimedia
systems
16
Cont…
Scheduling of processes is done to finish the work on time.
Below are different time with respect to a process.
• Arrival Time: Time at which the process arrives in the ready
queue.
• Completion Time: Time at which process completes its
execution.
• Burst Time: Time required by a process for CPU execution.
• TurnAround Time: Time Difference between completion time
and arrival time.
Turn Around Time = Completion Time - ArrivalTime
• Waiting Time(W.T): Time Difference between turn around time
and burst time.
Waiting Time = Turn Around Time - Burst Time
17
Scheduling policies in different environments
18
Scheduling in Batch Systems
• Batch systems are designed for processing large amounts of
data without user interaction.
• Jobs are submitted in advance, and the system processes them
in a batch mode without requiring user intervention during
execution.
• Long-term scheduling is more relevant in batch systems, and
throughput is a key performance metric.
• It commonly uses Non-preemptive algorithms with long time
periods for each process are often acceptable.
• Ex. of batch system scheduling algorithms:-
• First Come First Served
• Shortest Job first
• Priority scheduling 19
Scheduling in Interactive System
• Interactive systems involve direct user interaction.
• They are designed to respond quickly to user inputs
• Short-term scheduling is crucial in interactive systems to
ensure responsiveness.
• Round Robin or Priority Scheduling algorithms are commonly
used.
• The focus is on minimizing response time and providing a fair
share of the CPU to interactive tasks.
• Ex. Interactive system scheduling algorithms:-
• Round-robin scheduling
• Priority Scheduling
• Multiple Queues
• Lottery Scheduling
• Fair-share Scheduling 20
Cont…
1. First Come First Served
• It’s the simplest of all algorithms.
• It is non preemptive and use linked list to keep track the jobs
• When the running process blocks or exits, the first process on the
queue is run next.
21
Cont.…
Example
EXAMPLE DATA:
Process Arrival Service
Time Time
1 0 8
2 1 4
3 2 9
4 3 5
0 P1 8 P2 12 P3 21 P4 26
23
Cont…
Example
24
Preemptive SJF – (Shortest Remaining Time First)
• If a new process arrives with CPU burst length less than remaining
time of current executing process, preempt the currently executing
process and allocate the CPU to the new process.
25
Cont…
Example #2: Preemptive SJF (Shortest-remaining-time-first)
Process Arrival Burst
Time Time
1 0 7
2 2 4
3 4 1
4 5 4
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
P5 P1 P2 P3 P4
0 12 20 22 26 27
Completion time for p1,p2,p3,p4 and p5 is 20,22,26,27 and 12 respectively
Turnaround time = CT-AT=((20-0) + (22-1) + (26-3) + (27-4) + (12-5) )/5=
Average waiting time = TAT-BT=((20-8)+(21-2)+(23-4)+(23-1)+(7-12))/5 =
28
Cont…
• Draw Gantt chart for Priority preemptive
scheduling and also calculate average waiting
time, Average turn-around time of the above
usin.
29
Cont.…
4. Round- Robin scheduling
• Processes will be given equal priority
• Each process is assigned a time interval called quantum
• Therefore each process will be given an interval of time
(quanta of time).
1 quantum, 2 quanta…..
• Setting the quantum should have to be done carefully
• The two main things in setting quantum
– Setting the quantum too short causes to many process
switches that will result in performance degradation.
– But setting it too long may cause poor response to short
interactive commands.
30
Cont…
Example
EXAMPLE DATA:
Process Arrival Service
Time Time
1 0 8
2 1 4
3 2 9
4 3 5
P1 P2 P3 P4 P1 P3 P4 P3
0 4 8 12 16 20 24 25 26
32
Scheduling in Real Time System
• A real-time system is a computing system that must meet specific timing
constraints or deadlines associated with the tasks it performs.
• Real-time systems are classified into two main categories(hard real-time
systems and soft real-time systems) based on the strictness of the timing
requirements and the consequences of missing deadlines.
1. Hard real-time systems – required to complete a critical task within a
guaranteed amount of time.
– A Real time system in which, the violation of time constraints will cause critical
failure and loss of life or property damage
– Ex: Deadline in a missile control embedded system, in RADAR system
2. Soft real-time computing – requires that critical processes receive
priority over less fortunate ones.
– The violation of time constraints will cause only the degraded quality, but the
system can continue to operate is known as a Soft real time system.
– Ex: washing machine, and TV remote etc.
33
Cont..
Algorithms under real time system
34
Cont…
Rate monotonic Scheduling
Assumptions:
Each periodic process must complete within its period
No process is dependent on any other process
Each process needs the same amount of CPU time on
each burst
Any non-periodic process have no deadline
preemption has no overhead
35
Cont…
Earliest Job First
Dynamic scheduling
Do not need process to be periodic
Process can use different amount of CPU time
Scheduler keeps list of runnable process based on their deadline
order
if the process at ready state ,the system checks at deadline and pick
the nearest deadline.
36
Thread Scheduling
1. User-level threads:
• Since the kernel is not aware of the existence of threads, it operates
as it always does, picking a process.
• The runtime system of the process decides which thread to run
next.
• Since there are no clock interrupts to threads, this thread may
continue running as long as it wants to.
• Round-robin scheduling and priority scheduling are most common.
2. Kernel-level threads
• Here the kernel picks a particular thread to run.
• The executions of thread in the process is scheduled by the kernel
rather than runtime system.
37
Cont.…
39