Operations Scheduling. Scheduling in a Process-Focused Environment.
Chapter 5: CPU Scheduling - IISPL() · 2011-05-17 · Scheduling Non-goals Starvation(of process) A...
Transcript of Chapter 5: CPU Scheduling - IISPL() · 2011-05-17 · Scheduling Non-goals Starvation(of process) A...
Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 8th Edition,
Chapter 5: CPU Scheduling
2/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Chapter 5: Process Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Thread Scheduling
Multiple-Processor Scheduling
Operating Systems Examples
Algorithm Evaluation
3/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Objectives
To introduce process scheduling, which is the basis for multiprogrammed
operating systems
To describe various process-scheduling algorithms
To discuss evaluation criteria for selecting a process-scheduling algorithm
for a particular system
4/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Basic Concepts
CPU Burst : Time it takes for the CPU to execute an operation
I/O Burst : Time it takes for the CPU to wait for I/O task
Goal of processor scheduling :
Maximum CPU utilization obtained with multiprogramming
CPU–I/O Burst Cycle – Process execution consists of a cycle of
CPU execution and I/O wait
CPU burst distribution
5/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
CPU burst vs. IO burst
(a) A CPU-bound process
(b) An I/O-bound process
5
6/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Histogram of CPU-burst Times
7/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Alternating Sequence of CPU And I/O Bursts
8/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
CPU Scheduler
Selects a process (in ready queue) from among the processes in memory
that are ready to execute,
and allocates the CPU to one of them
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
-I/O request, event wait, completion of child process
2. Switches from running to ready state – interrupt request
3. Switches from waiting to ready – completion of I/O task or event
4. Terminates
Scheduling under 1 and 4
is nonpreemptive
All other scheduling (2 and 3)
is preemptive
- OS가 우선적으로 중단/실행
9/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Dispatcher
Dispatcher module gives control of the CPU to the process
selected by the short-term scheduler; this involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart
that program
Dispatch latency – time it takes for the dispatcher to stop one
process and start another running
10/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – # 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)
11/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Scheduling Algorithm Optimization Criteria
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
12/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Scheduling Goals
All systems
Fairness: giving each process a fair share of the CPU
Balance: keeping all parts of the system busy
Batch systems
Throughput: maximize jobs per hour
Turnaround time: minimize time between submission and termination
CPU utilization: keep the CPU busy all the time
13/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Scheduling Goals (Cont’d)
Interactive systems
Response time: minimize average time spent on ready queue
Waiting time: minimize average time spent on wait queue
Proportionality: meet users’ expectations
Real-time systems
Meeting deadlines: avoid losing data
Predictability: avoid quality degradation in multimedia systems
14/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Scheduling Non-goals
Starvation(of process)
A situation where a process is prevented from making progress
because another process has the resource it requires.
Resource could be the CPU or a lock
A poor scheduling policy can cause starvation
If a high-priority process always prevents a low-priority process from
running on the CPU
Synchronization can also cause starvation
One thread always beats another when acquiring a lock
Constant supply of readers always blocks out writers
15/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
First-Come, First-Served (FCFS) Scheduling
First-Come, First-Served
Jobs are scheduled in order that they arrive
“Real-world” scheduling of people in lines
e.g. supermarket, bank tellers, McDonalds, etc.
Typically, non-preemptive
Jobs are treated equally: no starvation
Problems
Average waiting time can be large if small jobs wait behind long ones
Basket vs. cart
May lead to poor overlap of I/O and CPU
16/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
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:
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 300
17/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
FCFS Scheduling (Cont)
Suppose that the processes arrive in the order
P2 , P3 , P1
The Gantt chart for the schedule is:
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect
short process behind long process
P1P3P2
63 300
18/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
SJF
Shortest Job First
Choose the job with the smallest expected CPU burst
Can prove that SJF has optimal min. average waiting time
Only when all jobs are available simultaneously
Non-preemptive
Problems
Impossible to know size of future CPU burst
Can you make a reasonable guess?
Can potentially starve
19/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
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 of SJF:
Nonpreemptive
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
The difficulty is knowing the length of the next CPU request
20/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Example of Non-Preemptive SJF
SJF (non-preemptive)
Average waiting time = (0 + 6 + 3 + 7)/4 = 4P1 0=0-0 P2:8-2.0=6 P3:7-4.0=3 P4:12-5.0=70: P1(7) =>P1(7)p1(7) ~ 7:P2(4),P3(1),P4(4) => P3(1)p3(1) ~ 8: P2(4),P4(4) =>P2(4)p2(4) ~ 12:P4(4)=>P4(4)p4(4) ~ 16
20
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
21/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Example of Preemptive SJF
SJF (preemptive) (= SRTF)
Average waiting time = (9 + 1 + 0 +2)/4 = 3P1:0-0 + 11-2=9 P2:2-2.0 + 5-4=1 P3=4-4.0=0 P4:7-5=20 : P1(7) =P1(7)P1(7) ~ 2.0 p2(4) : P1(5) , p2(4) => p2(4) P2(4) ~ 4.0 p3(1) : p1(5), p2(2), p3(1) => p3(1) P3(1) ~ 5.0 p4(4) : p1(5),P2(2), p4(4) =>P2(2)P2(2) ~ 7.0 : p1(5),P4(4) =>P4(4)p4(4) ~ 11.0 : P1(5) =>P1(5)p1(5) ~ 16.0 :
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
22/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Determining Length of Next CPU Burst
Can only estimate the length
Can be done by using the length of previous CPU bursts, using exponential
averaging
:Define 4.
10 , 3.
burst CPU next the for value predicted 2.
burst CPU of length actual 1.
1n
thn nt
.1 1 nnn t
23/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Prediction of the Length of the Next CPU Burst
66*5.06*5.01
68*5.04*5.01
810*5.06*5.01
6 ,5.0,10
223
112
001
00
t
t
t
t
24/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Examples of Exponential Averaging
=0
n+1 = n
Recent history does not count
=1
n+1 = tn
Only the actual last CPU burst counts
If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor
25/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
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
nonpreemptive
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
26/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Priority Scheduling
Abstractly modeled as multiple “priority queues”
Put ready job on Queue associated with its priority
27/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Round Robin (RR)
For time sharing system. FCFS, each process occupies for
quantum at a time , preemptive scheduling, circular ready
queue
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
28/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
Typically, higher average turnaround than SJF, but better responsequeue : p1(24),p2(3),p3(3)0 : p1(24) : p2(3),p3(3) timer 4:p2(3) : p3(3),p1(20) release7 : p3(3) : p1(20), release 10:p1(20) : timer14:p1(16): timer 18:p1(12): timer 22:p1(8): timer 26:p1(4): timer 30:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
29/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Time Quantum and Context Switch Time
30/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Turnaround Time Varies With The Time Quantum
31/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Problems of RR
What do you set the quantum to be?
quantum -> : FIFO
quantum -> 0 : processor sharing
If small, then context switches are frequent incurring high overhead
(CPU utilization drops)
If large, then response time drops
A rule of thumb: 80% of the CPU bursts should be shorter than the time
quantum
32/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
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
33/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Multilevel Queue Scheduling
34/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Multilevel Feedback Queue
In multilevel queue scheduling algorithm, processors are
permanently assigned to a queue
In multilevel feedback queue scheduling algorithm, 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
35/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Example of Multilevel Feedback Queue Scheduler
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.
36/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Multilevel Feedback Queues
37/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Thread Scheduling
Thread scheduling for the user level thread
Scheduled by a thread library.
Must ultimately be mapped indirectly to a associated kernel thread
on many-to-one and many-to-many models
The thread library schedules user-level threads to run on LWP(Light
Weight Process), a scheme known as process-contention scope
(PCS) since competition for CPU takes place among threads
belonging to the same process.
Thread scheduling for the kernel thread
Scheduled by OS
scheduled onto available CPU is system-contention scope (SCS)
– competition among all threads in system
38/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Pthread Scheduling
API allows specifying either PCS or SCS during thread creation
PTHREAD SCOPE PROCESS schedules threads using PCS
scheduling
PTHREAD SCOPE SYSTEM schedules threads using SCS
scheduling.
39/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Pthread Scheduling API for POSIX
#include <pthread.h>
#include <stdio.h>
#define NUM THREADS 5
int main(int argc, char *argv[])
{
int I, scope;
pthread_t tid[NUM THREADS];
pthread_attr_t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* first inquire on the current scope */if (pthread_attr_getscope(&attr, &scope) != 0)
printf(stderr, “Unable to get scheduling scope\n”);else {
if (scope == PTHREAD_SCOPE_PROCESS)pritnf(“PTHREAD_SCOPE_PROCESS”);
else if (scope == PTHREAD_SCOPE_SYSTEM)pritnf(“PTHREAD_SCOPE_SYSTEM”);
elseprintf(stderr,”Illegal scope value.\n”);
}
40/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Pthread Scheduling API
/* set the scheduling algorithm to PCS(PROCESS) or (SCS)SYSTEM */
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* set the scheduling policy - FIFO, RT, or OTHER
pthread_attr_setschedpolicy(&attr, SCHED OTHER); */
/* create the threads */
for (i = 0; i < NUM_THREADS; i++)
pthread_create(&tid[i],&attr,runner,NULL);
/* now join on each thread */
for (i = 0; i < NUM THREADS; i++)
pthread_join(tid[i], NULL);
}
/* Each thread will begin control in this function */
void *runner(void *param)
{
/* do some work */
printf("I am a thread\n");
pthread exit(0);
}
41/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are available=> load
sharing
cover only Homogeneous processors within a multiprocessor
Asymmetric multiprocessing
–A single process(master server) handle all scheduling decision, I/O
processing , other system activities, and accesses the system data
structures, alleviating the need for data sharing
Symmetric multiprocessing (SMP)
– each processor is self-scheduling, all processes may be in common
ready queue, or each has its own private queue of ready processes
Processor affinity
– process has affinity for processor on which it is currently running
soft affinity
– when a process is able to migrate between processors.
hard affinity
– when a process is not allowed to migrate between processors.
42/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Multiple-Processor Scheduling
Symmetric multithreading(SMT)
Hyperthreading technology in Intel company
OS offers logical processor to each thread
43/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Operating System Examples
Solaris scheduling
Windows XP scheduling
Linux scheduling
44/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Solaris Scheduling
45/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Solaris Dispatch Table for time-sharing and interactive threads
(ms)
Low
high
46/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Windows XP Priorities
Priority of process priority classes : real-time, high, above normal, …
Relative priority of process thread of a given priority classRelative priorities : time-critical, highest …
47/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Linux Scheduling
Constant order O(1) scheduling time
Two priority ranges: time-sharing and real-time
Real-time range from 0 to 99 and nice value from 100 to 140
(figure 5.15)
48/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Priorities and Time-slice length
49/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
List of Tasks Indexed According to Priorities
50/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Algorithm Evaluation
Examples of criteria to select algorithm
Maximizing CPU utilization under the constraint that
the maximum response time is 1 second.
Maximizing throughput such that turnaround time is
(on average) linearly proportional to total execution
time.
51/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Evaluation Methods of scheduling algorithm
Deterministic modeling
Takes a particular predetermined workload and defines the performance
of each algorithm for that workload
Queueing models
Mathematical models used to compute expected system parameters
Simulation
Algorithmic models which simulate a simplified version of a system
using statistical input
Trace tape (or trace data)
Implementation
Direct implementation of the system under test, with appropriate
benchmarks
52/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Deterministic modeling
Process : p1 p2 p3 p4 p5
Burst time(ms) : 10 29 3 7 12
FCFS scheduling algorithm
Averaging waiting time : (10+39+42+49)/5 = 28 ms
SJF scheduling algorithm
Averaging waiting time : (10 + 32+0+3+20)/5=13 ms
53/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Deterministic modeling
Process : p1 p2 p3 p4 p5
Burst time(ms) : 10 29 3 7 12
RR at time quantum = 10ms
Averaging waiting time : (0+32+20+23+40)/5 = 23 ms
p1 : 0
p2 : 10+20+2 =32
p3 : 20
p4 : 23
p5 : 30+10=40
54/54 Hanbat National Univ. Computer Eng. Dept. Y.J.Kim © 2009Operating System Concepts – 7th Edition
Evaluation of CPU schedulers by Simulation