Operating Systems-4-CPU Scheduling
Operating Systems-4-CPU Scheduling
CPU
Scheduling
Basic Concepts
● Maximum CPU utilization obtained with multiprogramming
● Continuous Cycle :
Processes in
○ one process has to wait (I/O) Ready Queue
○ Operating system takes the CPU away
Schedule
○ Give CPU to another process
○ This pattern continues
Processor
2
CPU Scheduler
● Selects from among the processes in ready queue, and allocates the CPU to one of
them
○ FIFO queue
○ Priority queue
○ Tree
○ Unordered linked-list
● CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state (I/O request)
2. Switches from running to ready state (e.g. when interrupt occurs)
3. Switches from waiting to ready (e.g. at completion of I/O)
4. Terminates
● Scheduling under 1 and 4 is nonpreemptive
● All other scheduling is preemptive
○ Consider access to shared data
○ Consider preemption while in kernel mode
○ Consider interrupts occurring during crucial OS activities
3
Scheduling Criteria
● CPU utilization – keep the CPU as busy as possible
● Turnaround time
– amount of time to execute a particular process
-- the interval from the time of submission of a process to the time of the completion.
-- sum of the periods spent waiting to get into memory, waiting in the ready queue,
executing on the CPU, doing I/O
● 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)
4
Scheduling Algorithm
Optimization Criteria
● Max CPU utilization
● Max throughput
● Min turnaround time
● Min waiting time
● Min response time
5
OPERATING SYSTEMS
CPU Scheduling Algorithms
-
First Come First Serve
(FCFS)
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
7
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
8
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
● The Gantt chart for the schedule is:
P2 P3 P1
3
0 3 6
0
9
OPERATING SYSTEMS
CPU Scheduling
Algorithms -
Shortest Job First (SJF)
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
known as the
Shortest-Remaining-Time-First (SRTF)
● SJF is optimal – gives minimum average waiting time for a given set of
processes
11
Example of SJF
Process Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
● SJF scheduling chart
P4 P1 P3 P2
3 9 1 2
0
6 4
12
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
13
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
14
Example of Shortest-
remaining-time-first
● Now we add the concepts of varying arrival times and preemption to the
analysis
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
P1 P2 P4 P1 P3
1 1 2
0 1 5
0 7 6
15
OPERATING SYSTEMS
CPU Scheduling
Algorithms -
Priority Scheduling
Priority Scheduling
● A priority number (integer) is associated with each process
17
Example of Priority Scheduling
Non Preemptive:
P2 P5 P1 P3 P4
1 1 1
0 1 6
6 8 9
● Problem ≡ Starvation – low priority processes may never execute
18
Example of Priority Scheduling
Preemptive:
P2 P1 P3 P1 P5 P1 P4
1 1 1
0 1 3 5 7
2 8 9
● Problem ≡ Starvation – low priority processes may never execute
19
OPERATING SYSTEMS
CPU Scheduling
Algorithms -
Round Robin (RR)
Round Robin (RR)
● Each process gets a small unit of CPU time (time quantum q),
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.
● Timer interrupts every quantum to schedule next process
● Performance
○ q large ⇒ FIFO
○ q small ⇒ q must be large with respect to context switch,
otherwise overhead is too high
21
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
P1 P2 P3 P1 P1 P1 P1 P1
1 1 1 2 2 3
0 4 7
0 4 8 2 6 0
23
Turnaround Time Varies With
The Time Quantum
24
OPERATING SYSTEMS
CPU Scheduling -
Multilevel Queue,
Multilevel Feedback Queue
Multilevel Queue
● Another class of scheduling algorithm needs- in which processes are
classified into different groups, e.g.:
○ foreground (interactive) processes
○ background (batch) processes
● They have different response time requirements-so different scheduling
needs.
● Foreground processes may have priority over background processes.
● A multilevel queue-scheduling algorithm partitions the ready queue into several separate queues-
we can see it in the figure of next slide:-
27
Multilevel Feedback Queue scheduling
● Scheduling
○ A new job enters queue Q0 which is served for 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
29