Transcript ready queue

OPERATING SYSTEM
CONCEPTS
Lecture Contents
• CPU scheduling
• Dispatcher
• Criteria for evaluation of scheduling algorithms
•
•
•
•
•
performance
FCFS scheduling algorithm
Shortest job first algorithm
Round Robin algorithm
Multi-level feedback scheduling
Multi-level feedback queues scheduling
CPU scheduling
• Multiprogramming Systems
• Target of multiprogramming systems is
maximum CPU
utilization
• CPU scheduler selects from among the processes in memory
that are ready to execute, and allocates the CPU to one of
them
Process Life Cycle
• Life cycle of a process literally consists of I/O burst and
CPU burst
• I/O burst- (disk read/write, I/O from user, waiting for
termination of a child) CPU burst- (using the CPU)
• Types of programs which are CPU bound, I/O bound .. ?
Histogram of CPU-burst Times
Dispatcher
Dispatcher module gives control of the CPU to the process
selected by the short-term scheduler; this involves:
Dispatch latency – time it takes for the dispatcher to stop one
process and start another running
Events that lead to invocation of CPU
scheduler ..
• CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state (e.g., due to interrupt)
3. Switches from waiting to ready (e.g., in completion of I/O)
4. Terminates
 Scheduling under 1 and 4 is non-preemptive
 Once CPU has been allocated to a process, the process keeps the CPU until it
releases the CPU either by terminating or by switching to “waiting” state
 All types of schedulers who do this are non-preemptive schedulers
 All other scheduling is preemptive
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible (40-90%)
• Throughput – # of processes that complete their execution per time unit
• Turnaround time – amount of time to execute a particular process,
includes:
• Time spent waiting to get into memory
• Waiting in the ready queue
• Executing on CPU
• Doing I/O
• Waiting time – amount of time a process has been waiting in the ready
queue (this is the only time affected by the scheduling algorithm)
• 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)
Optimization Criteria
• Maximum CPU utilization (for all systems)
• Maximum throughput
• Minimum turnaround time
• Minimum waiting time
• Minimum response time (critical for real-time systems and
real-time systems
First-Come, First-Served (FCFS) Scheduling
• The process that comes first in ready queue is scheduled
first
• Order in which the processes get the CPU depends on
time they are submitted in the system
• Non-preemptive scheduler
First-Come, First-Served (FCFS) Scheduling
Process
Burst Time
P1
24 ms
P2
3 ms
P3
3 ms
• Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1
0
P2
24
P3
27
• Waiting time for P1 = 0 ms; P2 = 24ms; P3 = 27 ms
• Average waiting time: (0 + 24 + 27)/3 = 17 ms
30
FCFS Scheduling
Suppose that the processes arrive in the order
P2 , P3 , P1 .
• The Gantt chart for the schedule is:
P2
0
P3
3
P1
6
30
• Waiting time for P1 = 6 ms; P2 = 0 ms; P3 = 3 ms
• Average waiting time: (6 + 0 + 3)/3 = 3 ms
• Much better than previous case.
• Convoy effect short process behind long process
• Performance of FCFS critically depends on order in which
processes arrive in system and their next CPU burst
Shortest-Job-First (SJF) Scheduling
• Process with the shortest next CPU burst is scheduled
first
• 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)
Example of Non-Preemptive SJF
Process
P1
P2
P3
P4
• SJF (non-preemptive)
Arrival Time
0.0
2.0
4.0
5.0
P1
0
3
P3
7
Burst Time
7
4
1
4
P2
8
P4
12
16
• Waiting time (total time – arrival time - burst time) for P1 = (7-0-7) ms; P2 =
(12-2-4) ms; P3 = (8-4-1) ms; P4 = (16-5-4) ms
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Example of Preemptive SJF
Process
P1
P2
P3
P4
• SJF (preemptive)
P1
0
Arrival Time
0.0
2.0
4.0
5.0
P2
2
P3
4
P2
5
Burst Time
7
4
1
4
P4
7
P1
11
16
• Waiting time (total time – arrival time - burst time) for P1 = (16-0-7) ms; P2 =
(7-2-4) ms; P3 = (5-4-1) ms; P4 = (11-5-4) ms
• Average waiting time = (9 + 1 + 0 +2)/4 = 3
Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority (smallest integer
 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 (MIT
example)
• Solution  Aging – as time progresses increase the priority of the process
Example of Priority Algorithm
PROCESS
P1
P2
P3
P4
P5
P2
0
BURST TIME
10
1
2
1
5
P5
1
P1
6
PRIORITY
3
1
3
4
2
P4
P3
16
18
AWT = (P1+P2+P3+P4+P5) / 5
= (6+0+16+18+1) / 5
= 8.2 ms
19
Round Robin (RR)
Designed especially for time-sharing systems
Preemptive algorithm
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.
Performance
q large  FIFO
q small  q must be large with respect to context switch,
otherwise overhead is too high
Example of RR with Time Quantum = 20
Process
P1
P2
P3
P4
• The Gantt chart is:
53
17
68
24
33
17
68
24
P1
0
33
0
68
24
P2
20
37
Burst Time
53
17
68
24
33
0
48
24
P3
33
0
48
4
P4
57
13
0
48
4
P1
77
13
0
28
4
P3
97 117
13
0
28
0
P4
0
0
28
0
P1
0
0
8
0
P3
0
0
0
0
P3
121 134 154 162
• Typically, higher average turnaround than SJF, but better response time
• AWT= (134-53-0) + (37-17-0) + (117-68-0) + (121-24-0) / 4 =
Time Quantum and Context Switch Time
 Performance of RR depends on the size of the time quantum
 Variable quantum size -> modern OS
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
• 20% to background in FCFS
Multilevel Queue Scheduling
In this example every queue has full priority over lowerlevel queues (guaranteed with preemptive scheduling)
Multilevel Feedback Queue
• The idea – separate processes according to the characteristics of their CPU
bursts
• A process can move between the various queues; aging can be implemented
this way
Example of Multilevel Feedback Queue
• Three queues:
• Q0 – RR with time quantum 8 milliseconds
• Q1 – RR time quantum 16 milliseconds
• Q2 – FCFS
• Scheduling
• A new job enters queue Q0 which is served FCFS. 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 FCFS and receives 16 additional milliseconds. If
it still does not complete, it is preempted and moved to queue Q2.
Multilevel Feedback Queues
Gets CPU time
only if queue 0
is empty
Gets CPU time
only if queue 1
is empty