You are on page 1of 57

Overview

Answer the following:

All of the Above


Answer the following:

CPU Utilization

Throughput
Answer the following:
Optimization Criteria
1. _______CPU utilization
2. _______ throughput
3. _______ turnaround time
4. _______ waiting time
5. _______ response time

1. Max
2. Max
3. Min
4. Min
5. Min 5
Introduction to Process Management Continued….

CPU Scheduling
Questions answered in this lecture:
What is scheduling?
What is preemptive vs. non-preemptive scheduling?
What are FCFS, SJF, STCF, RR and priority-based
scheduling policies?
What are their advantages and disadvantages?
Scheduling
• Resource: CPU
• OS management: scheduling
– Decide order in which requests are
serviced
– Decide how long process keeps
resource
Levels of CPU Management

Dispatcher
• Low-level mechanism
• Performs context-switch
– Save execution state of old process in PCB
– Add PCB to appropriate queue (ready or blocked)
– Load state of next process from PCB to registers
– Switch from kernel to user mode
– Jump to instruction in user process
Scheduler
• Policy to determine which process gets CPU, when
Allocator – Multiprocessor Systems
• Policy to determine which processes compete for which
CPU
• Needed for multiprocessor, parallel, and distributed
systems
CPU Workload Model
Workload contains collection of jobs (processes)
Job model
• Job alternates between CPU and I/O bursts (i.e.,
moves between ready and blocked queues)
• CPU-bound job: Long CPU bursts
• I/O-bound job: Short CPU bursts
Do not know type of job before it executes
• Do not know duration of CPU or I/O burst
Need job scheduling for each ready job
• Schedule each CPU burst
Scheduling Performance Metrics
Minimize waiting time
• Do not want to spend much time in Ready queue
Minimize turnaround time
• Do not want to wait long for job to complete
Maximize throughput
• Want many jobs to complete per unit of time
Minimize response time
• Schedule interactive jobs promptly so users see output quickly
Maximize resource utilization
• Keep expensive devices busy
Minimize overhead
• Reduce number of context switches
Maximize fairness
• All jobs get same amount of CPU over some time interval
When may Scheduler switch
Processes?
Invoking the Scheduler
Need a mechanism to call the scheduler
• Voluntary call
– Process blocks itself
– Calls the scheduler
• Involuntary call
– External force (interrupt) blocks the process
– Calls the scheduler
Contemporary Scheduling
Involuntary CPU sharing – timer interrupts
• Time quantum determined by interval timer – usually
fixed size for every process using the system
• Sometimes called the time slice length
Types of CPU Scheduling
Scheduling Algorithms
• The Scheduling algorithms are either non-
preemptive or preemptive.
• Non-preemptive algorithms are designed so
that once a process enters the running state, it
cannot be preempted until it completes its
allotted time or terminates
• Whereas the preemptive scheduling is based
on priority where a scheduler may preempt a
low priority running process anytime when a
high priority process enters into a ready state.
When scheduling is Preemptive
or Non-Preemptive?
To determine if scheduling is preemptive or non-
preemptive, consider these four parameters:
1. A process switches from the running to the waiting
state.
2. Specific process switches from the running state to
the ready state.
3. Specific process switches from the waiting state to
the ready state.
4. Process finished its execution and terminated.

Only conditions 1 and 4 apply, the scheduling is called non-


preemptive. All other scheduling are preemptive.
Important CPU scheduling
Terminologies
• Burst Time/Execution Time: It is a time required by the
process to complete execution. It is also called running time.
• Arrival Time: when a process enters in a ready state
• Finish Time: when process complete and exit from a system
• Multiprogramming: A number of programs which can be
present in memory at the same time.
• Jobs: It is a type of program without any kind of user
interaction.
• User: It is a kind of program having user interaction.
• Process: It is the reference that is used for both job and user.
• CPU/IO burst cycle: Characterizes process execution, which
alternates between CPU and I/O activity. CPU times are
usually shorter than the time of I/O.
Types of Scheduling Algorithms
Scheduling Algorithms
1. First come First served
2. Shortest Job First
3. Shortest Remaining Time
4. Round Robin
5. Priority
6. Multilevel Feedback Queues

20
Preemptive vs Non-Preemptive
Scheduling
Simulating the Scheduling of
Process
Example:

Job Arrival CPU


Time burst
(ms) (ms)
A 0 10
B 1 2
C 2 4
Job Arrival CPU
Gantt Chart Time burst
(ms) (ms)
A 0 10
B 1 2
C 2 4

Illustrates how jobs are scheduled over time on CPU

Example:

A B C
0 10 12 16
Time
To find the effectiveness of an
algorithm
Compute the average wait time of the processes
after applying the scheduling algorithm

Compute average turnaround time of the


processes after applying the scheduling algorithm
First-Come-First-Served (FCFS)
Idea: Maintain FIFO list of jobs as they arrive
• Non-preemptive policy
• Allocate CPU to job at head of list

Job Arrival CPU


burst Ready Queue
A 0 10
A B C
B 1 2
C 2 4

A B C
0 10 12 16
Time
First-Come-First-Served (FCFS)
Idea: Maintain FIFO list of jobs as they arrive
• Non-preemptive policy
• Allocate CPU to job at head of list
Waiting time for A = 0; B =(10-1)= 9; C = (12-2)= 10
Job Arrival CPU Average wait time:
burst
(0 + 9 + 10)/3= 6.33
A 0 10 Turnaround time for
A =10; B =(12-1) =11; C = (16-2)=14
B 1 2
Average turnaround time:
C 2 4
(10 + 11 + 14)/3=11.67
A B C
0 10 12 16
Time
First-Come, First-Served (FCFS)
Scheduling
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27

Average waiting time: (0 + 24 + 27)/3 = 17


Average turnaround time: ? Ans: 27
27
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1
The Gantt chart for the schedule is:
P2 P3 P1

0 3 6 30

Waiting time for P1 = 6; P2 = 0; P3 = 3


Average waiting time: (6 + 0 + 3)/3 = 3
Average Turnaround Time: ? Ans: 13
Much better than previous case
Convoy effect short process behind long process
28
FCFS Discussion
Advantage: Very simple implementation
Disadvantage
• Waiting time depends on arrival order
• Potentially long wait for jobs that arrive later
• Convoy effect: Short jobs stuck waiting for long jobs
– Hurts waiting time of short jobs
– Reduces utilization of I/O devices
– Example: 1 mostly CPU-bound job, 3 mostly I/O-bound jobs

CPU
A B C D A B C D
I/O Devices
Idle A B C D Idle A B C D

Time
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its
next CPU burst. Use these lengths to
schedule the process with the shortest time
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

30
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 = 4ms
Average Turnaround Time = ( 7 + 10 + 4 + 11)/4 = 8ms

31
Shortest-Job-First (SJF)
Idea: Minimize average wait time by running
shortest CPU-burst next
• Non-preemptive
• Use FCFS if jobs are of same length

Job Arrival CPU


burst Average wait:
A 0 10
B 0 2 Average turnaround:
C 0 4

B C A
0 2 6 16
Time
SJF Discussion
Advantages
• Provably optimal for minimizing average wait time (with
no preemption)
– Moving shorter job before longer job improves waiting time of
short job more than it harms waiting time of long job
• Helps keep I/O devices busy
Disadvantages
• Not practical: Cannot predict future CPU burst time
– OS solution: Use past behavior to predict future behavior
• Starvation: Long jobs may never be scheduled
Shortest-Time-to-Completion-First
OR Shortest-Remaining-Time-Next
(STCF or SCTF or SRTN)
Preemptive SJF

Idea: Add preemption to SJF


• Schedule newly ready job if the new job has shorter burst time
than remaining burst for running job
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16
Average waiting time = (9 + 1 + 0 +2)/4 = 3ms
Average Turnaround Time = ( 16 + 5 + 1 + 6)/4 = 7ms

35
Shortest-Time-to-Completion-First
OR Shortest-Remaining-Time-Next
(STCF or SCTF or SRTN)

Job Arrival CPU


burst
SJF Average wait: ?
A 0 8
Average turnaround: ?
B 1 4

C 2 9
SRTN Average wait: ?
D 3 5 Average turnaround: ?
A B D C SJF
0 8 12 17 26
A B D A C SRTN
01 5 10 17 26
Time
Round Robin (RR)
• Each process gets a small unit of CPU time
(time quantum), usually 10-100
milliseconds.
• After this time has elapsed, the process is
preempted and added to the end of the
ready queue.
• If there are n processes in the ready queue
and the time quantum is q,
• then each process gets 1/n of the CPU time in chunks of at
most q time units at once.
• No process waits more than (n-1)q time units.

37
Round-Robin (RR)
Idea: Run each job for a time-slice and then move to
back of FIFO queue
• Preempt job if still running at end of time-slice
Let the time slice = 1 ms
Job Arrival CPU
burst
Average wait: ?
A 0 10
B 1 2 Average Turnaround Time: ?
C 2 4

A B C A B C A C A C A
Time
Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68 Average wait: ?
P4 24
Average Turnaround Time: ?
The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121134 154162

Typically, higher average turnaround than SJF, but better


response
Compute Wait time for each process and Average
Wait time.
Compare the performance of the different scheduling 39
schemes for an example.
RR Discussion
Advantages
• Jobs get fair share of CPU
• Shortest jobs finish relatively quickly
Disadvantages
• Poor average waiting time with similar job lengths
– Example: 10 jobs each requiring 10 time slices
– RR: All complete after about 100 time slices
– FCFS performs better!
• Performance depends on length of time-slice
– If time-slice too short, pay overhead of context switch
– If time-slice too long, degenerate to FCFS
Time Quantum and Context
Switch Time

41
RR Time-Slice
IF time-slice too long, degenerate to FCFS
• Example:
– Job A w/ 1 ms compute and 10ms I/O
– Job B always computes
– Time-slice is 50 ms
CPU
A B A B
Disk
A Idle A Idle
Time
Goal: Adjust length of time-slice to match CPU burst
Priority-Based
Idea: Each job is assigned a priority (A priority number
(integer) is associated with each process)
(smallest integer  highest priority)
• Schedule highest priority ready job
• May be preemptive or non-preemptive
• Priority may be static or dynamic
Advantages
• Static priorities work well for real time systems
• Dynamic priorities work well for general workloads
Disadvantages
• Low priority jobs can starve
• How to choose priority of each job?
Goal: Adjust priority of job to match CPU burst
Priority-Based
• Each process is assigned a priority. Process with
highest priority is to be executed first and so on.
• Processes with same priority are executed on
first come first served basis.
• Priority can be decided based on memory
requirements, time requirements or any other
resource requirement.
Priority-Based

• As Non-preemptive algorithm : One of the


most common scheduling algorithms in batch
systems.
Priority-Based: Non-Preemptive
• Given: Table of processes, and Execution time,
and priority.
• Here we are considering 1 is the lowest priority.
Process Execution Priority
Time Ready Queue
P0 5 1
P3 P1 P0 P2
P1 3 2
P2 8 1 Compute: Total Wait Time for each process
Average Wait Time
P3 6 3 Total Turnaround Time for each Process
Average Turnaround Time.

P3 P1 P0 P2
0 6 9 14 22
Time
Priority-Based

• As Preemptive algorithm :
Priority-Based: Preemptive
• Given: Table of processes, and their Arrival
time, Execution time, and priority.
• Here we are considering 1 is the lowest priority.
Process Arrival Execution Priority
Ready Queue
At 0 ms
Time Time P0 P1
P0 0 5 1
P1 0 3 2 Sorted based on Priority P1 P0

P2 1 8 1
At 1 ms
P3 1 6 3 P0 P2 P3

Sorted based on Priority P3 P0 P2


Compare the executing process’s and new
process’s priority based on Priority and P0 P2 P1
pre-empt the running process

Sorted based on Priority P1 P0 P2


Priority-Based: Preemptive
• Given: Table of processes, and their Arrival
time, Execution time, and priority.
• Here we are considering 1 is the lowest priority.

Process Arrival Execution Priority


Ready Queue
Time Time P1 P0
P0 0 5 1
P1 0 3 2 P3 P1 P0 P2
P2 1 8 1
Compute: Total Wait Time for each process
P3 1 6 3 Average Wait Time
Total Turnaround Time for each
Process
Average Turnaround Time.

P1 P3 P1 P0 P2
Time0 1 7 9 14 22
Priority Scheduling
The CPU is allocated to the process with the highest
priority
• Preemptive
• Non-preemptive
SJF is a priority scheduling where priority is the
predicted next CPU burst time
Problem  Starvation – low priority processes may
never execute
Solution  Aging – as time progresses increase the
priority of the process

50
Multilevel Queue
Ready queue is partitioned into separate queues:
• foreground (interactive)
• background (batch)
Each queue has its own scheduling algorithm
• foreground – RR
• background – FCFS
Scheduling must be done between the queues
• Fixed priority scheduling; (i.e., serve all from
foreground then from background). Possibility of
starvation.
• Time slice – each queue gets a certain amount of CPU
time which it can schedule amongst its processes; i.e.,
80% to foreground in RR and 20% to background in
FCFS
51
Multilevel Queue Scheduling

52
Multilevel Feedback Queue
 A process can move between the various queues;
aging can be implemented this way
 Multilevel-feedback-queue scheduler defined by the
following parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a
process
 method used to determine when to demote a
process
 method used to determine which queue a process
will enter when that process needs service

53
Example of Multilevel Feedback Queue
Three queues: For example:
• Q0 – RR with time quantum, say, 8 milliseconds
• Q1 – RR time quantum , say, 16 milliseconds
• Q2 – FCFS
Scheduling
• A new job enters queue Q0 which is served RR.
When it gains CPU, job receives 8 milliseconds. If it
does not finish in 8 milliseconds, job is moved to
queue Q1.
• At Q1 job is again served RR and receives 16
additional milliseconds. If it still does not complete,
it is preempted and moved to queue Q2.

54
Multilevel Feedback Queues

55
Scheduling Schemes Based on
Types of Systems
Scheduling in Batch Systems:
• FCFS
• SJF
• SRTN
• Priority: Non-preemptive
Scheduling in Interactive Systems:
• RR
• Priority
• Multilevel Queues
• Shortest Process Next
Scheduling Schemes Based on
Types of Systems
Scheduling in Real Time Systems:
• A hard real-time task must be performed at a specified time which could
otherwise lead to huge losses.
• In soft real-time tasks, a specified deadline can be missed. This is because
the task can be rescheduled (or) can be completed after the specified time.
• In real-time systems, the scheduler is considered as the most important
component which is typically a short-term task scheduler.
• The main focus of this scheduler is to reduce the response time
associated with each of the associated processes instead of handling the
deadline.
• An approach is designed by combining both preemptive and non-
preemptive scheduling.
• This can be done by introducing time-based interrupts in priority based
systems which means the currently running process is interrupted on a
time-based interval and if a higher priority process is present in a ready
queue, it is executed by preempting the current process.
Scheduling Schemes Based on
Types of Systems
Scheduling in Real Time Systems:
Based on schedulability, implementation (static or dynamic), and the result (self or dependent) of analysis,
the scheduling algorithm are classified as follows.

1. Static table-driven approaches:


These algorithms usually perform a static analysis associated with scheduling and capture the
schedules that are advantageous. This helps in providing a schedule that can point out a task with
which the execution must be started at run time.

2. Static priority-driven preemptive approaches:


Similar to the first approach, these type of algorithms also uses static analysis of scheduling. The
difference is that instead of selecting a particular schedule, it provides a useful way of assigning
priorities among various tasks in preemptive scheduling.

3. Dynamic planning-based approaches:


Here, the feasible schedules are identified dynamically (at run time). It carries a certain fixed time
interval and a process is executed if and only if satisfies the time constraint.

4. Dynamic best effort approaches:


These types of approaches consider deadlines instead of feasible schedules. Therefore the task is
aborted if its deadline is reached. This approach is used widely is most of the real-time systems.

You might also like