0% found this document useful (0 votes)
19 views67 pages

Os 52008

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views67 pages

Os 52008

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 67

CPU Scheduling

Outline
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Multiple-Processor Scheduling
Real-Time Scheduling
Thread Scheduling
Operating Systems Examples
Java Thread Scheduling
Algorithm Evaluation
Basic Concepts
The idea of multiprogramming:
Keep several processes in memory. Every time one
process has to wait, another process takes over the
use of the CPU
CPU-I/O burst cycle: Process execution consists
of a cycle of CPU execution and I/O wait (i.e.,
CPU burst and I/O burst).
 Generally, there is a large number of short CPU
bursts, and a small number of long CPU bursts.
 A I/O-bound program would typically has many very
short CPU bursts.
 A CPU-bound program might have a few long CPU
bursts
Alternating Sequence of CPU And I/O
Bursts
Histogram of CPU-burst
Times
(exponential or hyper-
exponential)
CPU Scheduler
Selects 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
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 (no choice in terms
of scheduling) is nonpreemptive
All other scheduling is preemptive
Diagram of Process State

interrupt terminated
new
admitted
ex
i
t
ready running

I/O or scheduler dispatchI/O or event wait


event completion
waiting
Preemptive Issues
Inconsistent cases may occur: preemptive
scheduling incurs a cost associated with access
to shared data
Affect the design of OS kernel: What happens if
the process is preempted in the middle of critical
changes (for instance, I/O queues) and the kernel
(or the device driver) needs to read or modify the
same structure?
 Unix solution: waiting either for a system call to
complete or for an I/O block to take place before doing
a context switch
 However, weak in supporting real-time computing and
multiprocessing
Windows 95 and subsequent versions, Mac OS X
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
Dispatch Latency
Scheduling Criteria
CPU utilization
 theoretically: 0%~100%
max
 real systems: 40% (light)~90% (heavy)
Throughput
 number of completed processes per time
unit
Turnaround time (of a process)
min
 submission ~ completion
Waiting time (of a process)
 total waiting time in the ready queue
Response time (of a request)
 submission ~ the first response is produced
Scheduling Algorithms
first-come, first-served (FCFS)
scheduling
shortest-job-first (SJF) scheduling
priority scheduling
round-robin scheduling
multilevel queue scheduling
multilevel feedback queue
scheduling
Nonpreemptive or
cooperative
The FCFS scheduling algorithm is
nonpreemptive.
Once the CPU has been allocated to a
process, that process keeps the CPU
until it releases the CPU, either by
terminating or by requesting I/O.
First-Come, First-Served Scheduling
(1)
Simplest, non-preemptive, often having a
long average waiting time
Example: Process Burst Time
P1 24
P2 3
P3 3

P1 P2 P3 AWT = (0+24+27)/3
24 27 = 17
30

P2 P3 P1 AWT = (6+0+3)/3
3 6 =3
30
First-Come, First-Served Scheduling
(2)Convoy effect: short process behind long
process
1. P1: a CPU bound process

2. others: I/O bound

 P1 uses CPU, all others wait for CPU


 I/O devices idle
 P1 uses I/O, all others use CPU and
then soon wait for I/O
 CPU idle
 P1 uses CPU, all others uses I/O and
then soon wait for CPU
 lower CPU and I/O utilization
Shortest-Job First (SJF) Scheduling
(1)
The smallest next CPU burst is selected
Provide the minimum average waiting time
(optimal)
difficulty: no way to know length of the next
CPU burst. Thus, it cannot be implemented in
short-termProcess
scheduler
Burst Time
P1 6
P2
P3 8 7
P4 3
AWT = (3+16+9+0)/4
P4 P1 P3 P2
=7
3 9 16 24
Frequently used in long-term scheduling
 A user is asked to estimate the job length. A
lower value means faster response. Too low a
value will cause timeout.
Approximate SJF: the next burst can be
predicted as an exponential average of the
measured length of previous CPU bursts
τ n 1 α tn  (1  α )τ n history
new one

Commonly, αtn  (1  α )αtn  1  (1  α ) 2 αtn  2  ...


α 1/2 1 1 2 1 3
( )tn  ( ) tn  1  ( ) tn  2  ...
2 2 2
Exponential predication of next
CPU burst
burst length
12
10
8
6
4
2 time

PU burst tn t0=6 4 6 4 13 13 13 ...


guess n 0=10 8 6 6 5 9 11 12

Diff. 4 4 0 2 -8 -4 -2 ...
Shortest-Job First (SJF) Scheduling
(2)
Two schemes
 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 know as the
Shortest-Remaining-Time-First (SRTF)
Example of Non-Preemptive
SJF
ProcessArrival TimeBurst 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 + (8-2) + 3 + 7]/4 = 4
Example of Preemptive SJF
ProcessArrival TimeBurst 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 = [(16-7) + 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
 nonpreemptive
SJF is a priority scheduling where priority is the
predicted next CPU burst time
Problem  Starvation – low priority processes
may never execute. (At MIT, there was a job submitted in
1967 that had not be run in 1973.)
Solution  Aging – as time progresses increase
the priority of the process
An Example
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

P2 P5 P1 P3 P4
0 1 6 16 18 19

AWT =
(6+0+16+18+1)/5=8.2
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.
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 Burst Time
P1 53
P2 17
P3 68
P4 24
The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Typically, higher average turnaround than


SJF, but better response
Time Quantum and Context
Switch Time
Turnaround Time Varies With The
Time Quantum

531512

15+
8+
9+
17 = 49

49/4 = 12.25
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
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
Example of Multilevel Feedback
Queue
Three queues:
 Q0 – time quantum 8 milliseconds
 Q1 – 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
Multiple-Processor
Scheduling
Approaches to Multiple-Processor
Scheduling

Asymmetric Multiprocessing: All


scheduling decisions, I/O processing,
and other system activities handled
by a single processor- master server.
Symmetric Multiprocessing (SMP):
Each processor is self-scheduling. All
processes may be in a common ready
queue, or each processor may have
its own private queue of ready
processes.
Processor Affinity
Avoid migration of processes from one
processor to another and instead
attempt to keep a process running on
the same processor. This is known as
processor affinity.
i.e. a process has an affinity for the
processor on which it is currently
running.
Types of Affinity
Soft affinity: It is possible for a
process to migrate between
processors.
Hard affinity: Allowing a process to
specify that it is not to migrate to
other processors.
Multiple-Processor Scheduling
Only homogeneous systems are discussed here
Scheduling
1. Each processor has a separate queue
2. All processes use a common ready queue
a. Each processor is self-scheduling
b. Appoint a processor as scheduler for the
other processes (the master-slave structure)
Load sharing: Push migration, Pull migration
Asymmetric multiprocessing: all system activities are
handled by a processor, the others only execute user
code (allocated by the master), which is far simple than
symmetric multiprocessing (SMP,
SMT(hyperthreading: one CPU, multi-logical
processors))
Processor affinity: a process has an affinity
Cachefor the
memory
processor on which
Keep running on itthe
is currently runningvalidating
same processor
Real-Time Scheduling (1)
Hard real-time systems – required to
complete a critical task within a
guaranteed amount of time
 Resource reservation is needed
 special purpose software on dedicated HW
Soft real-time computing – requires
that critical processes receive priority
over less fortunate ones
Real-Time Scheduling (2)
Scheduler for soft real-time system
Priority scheduling (real-time process must have
the highest priority). Easy!
The dispatch latency must be small
 Problem: Most OS waits a system call to

complete (or I/O block to take place) before


doing context switch
Solution: system calls should be pre-emptible
1. Insert preemption points in long-duration
system calls (The latency is still long, there are
a few preemption points.)
2. Make the entire kernel preemptible
Real-Time Scheduling (3)
Make the entire kernel preemptible
 All kernel data structure used by lower-
priority must be protected from modification
by high-priority process.
 Problem: priority inversion (A higher-
priority process is waiting for a lower-priority
one to finish and release some resources)
Solution: priority inheritance protocol (all
lower-priority processes inherit the high
priority until they are done with the resource
in question.)
Thread Scheduling
User-level threads are managed by a thread
library, and the kernel is unaware of them. To run
on a CPU, user-level threads are ultimately
mapped to an associated kernel-level thread, or
LWP.
 Process local scheduling: Thread scheduling

is done local to the application. The threads


library schedules user-level threads to run on
an available LWP
How different threads library locally schedule threads
is beyond this course. We omit the discussion here.
 System global scheduling: The kernel
decides which kernel thread to schedule
Pthread Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
int main(int argc, char *argv[])
{
int i;
pthread_t tid[NUM_THREADS];
pthread_attr_t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* set the scheduling algorithm to PROCESS or 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);
Pthread Scheduling API
/* 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)
{
printf("I am a thread\n");
pthread_exit(0);
}
Operating System Examples
Solaris scheduling
Windows XP scheduling
Linux scheduling
Solaris 2 Scheduling (1)
priority-based scheduling
Solaris 2 Scheduling (2)
Four classes of scheduling: real-time ->
system -> time sharing -> interactive.
A process starts with one LWP and is able to create
new LWPs as needed. Each LWP inherits the
scheduling class and priority of the parent process.
Default : time sharing (multilevel feedback queue)
queue
Inverse relationship between priorities and time
slices: the high the priority, the smaller the time
slice.
Interactive processes typically have a higher
priority; CPU-bound processes have a lower
priority.
Solaris 2 Scheduling (3)
Uses the system class to run kernel
processes, such as the scheduler and page
daemon. The system class is reserved for
kernel use only.
only
Threads in the real-time class are given the
highest priority to run among all classes.
There is a set of priorities within each
class. However, the scheduler converts the
class-specific priorities into global
priorities. (round-robin queue)
Solaris Dispatch Table
Windows XP Priorities
Linux Scheduling – preemptive &
priority
Version 2.5: support SMP, Load balancing &
Processor affinity
Time-sharing (100-140) and Real-time (0-99)
Higher priority with longer time quanta
Time-sharing
 Prioritized credit-based – process with most credits is scheduled next
 Credit subtracted when timer interrupt occurs
 When credit = 0, another process chosen
 When all processes have credit = 0, recrediting occurs
 Based on factors including priority and history

Real-time
 Soft real-time
 Posix.1b compliant – two classes
 FCFS and RR
 Highest priority process always runs first
The Relationship Between Priorities and
Time-slice length
List of Tasks Indexed According
to Prorities
Java Thread Scheduling
(1)
Preemptive, priority-based scheduling
(FIFO queue)
Time slicing:
 Java does not indicate whether or not threads
are time-sliced or not – it is up to the
particular implementation of the JVM.
 All threads have an equal amount of CPU

time on a system that does not perform time


slicing, a thread may yield control of the CPU
with the yield() method. Cooperative
multitasking.
Thread.yield();
Java Thread Scheduling
(2)
Thread priorities:
 Threads are given a priority when they are
created and – unless they are changed
explicitly by the program – they maintain the
same priority throughout their lifetime; the
JVM does not dynamically alter priorities.
 The priority of a thread can also be set

explicitly with the setPriority() method. The


priority can be set either before a thread is
started or while a thread is active.
Java-Based Round-robin Scheduler
Time-Slicing
Since the JVM Doesn’t Ensure Time-Slicing, the yield()
Method
May Be Used:

while (true) {
// perform CPU-intensive task
...
Thread.yield();
}

This Yields Control to Another Thread of Equal Priority


Thread Priorities
Priority Comment
Thread.MIN_PRIORITY Minimum Thread
Priority
Thread.MAX_PRIORITY Maximum Thread Priority
Thread.NORM_PRIORITY Default Thread
Priority

Priorities May Be Set Using setPriority() method:


setPriority(Thread.NORM_PRIORITY + 2);
Algorithm Evaluation
Criteria to select a CPU scheduling
algorithm may include several measures,
such as:
 Maximize CPU utilization under the constraint
that the maximum response time is 1 second
 Maximize throughput such that turnaround time
is (on average) linearly proportional to total
execution time
Evaluation methods ?
 deterministic modeling
 queuing models
 simulations
 implementation
Deterministic modeling
(1)
Analytic evaluation
Input: a given algorithm and a system
workload to
Output: performance of the algorithm for
that workload
Deterministic modeling
 Taking a particular predetermined
workload and defining the performance
of each algorithm for that workload.
Deterministic modeling
(2)
Process Burst Time
P1 10
P2 29
P3 3
P4 7
P5 12
 FCFS
P1 P2 P3 P4 P5 AWT = 28 ms
10 39 42 49 61
 SJF

P3 P4 P1 P5 P2 AWT = 13 ms
3 10 20 32 61
 RR (q = 10)
P1 P2 P3 P4 P5 P2 P5 P2 AWT = 23 ms
10 20 23 30 40 50 52 61
Deterministic modeling
(3)
: A simple and fast method. It gives the exact
numbers, allows the algorithms to be compared.
: It requires exact numbers of input, and its
answers apply to only those cases.
cases In general,
deterministic modeling is too specific, and
requires too much exact knowledge, to be useful.
Usage
 Describing algorithm and providing examples
 A set of programs that may run over and over again
 Indicating the trends that can then be proved
Queuing Models (1)
Queuing network analysis
 Using
 the distribution of service times (CPU and I/O
bursts)
 the distribution of process arrival times
 The computer system is described as a
network of servers. Each server has a queue of
waiting processes.
 Determining
 utilization, average queue length, average waiting
time, and so on
Queuing Models (2)
Little's formula (for a stable system):
14 persons in queue =
n =   W 7 arrives/per second 
 : average arrival rate n : average queue length 2 seconds waiting

queue server

W: average waiting time


Queuing analysis can be useful in comparing
scheduling algorithms, but it also has limitations.
: Queue model is only an approximation of a real
system. Thus, the result is questionable.
 The arrival and service distributions are often defined in
unrealistic, but mathematically tractable, ways.
ways
 Besides, independent assumptions may not be true.
Simulations
Simulations involve programming a model of the
system. Software data structures represent the
major components of the system.
: Simulations get a more accurate evaluation of
scheduling algorithms.
: 1. expensive (several hours of computer time).
2. large storage
3. coding a simulator can be a major task
Generating data to drive the simulator
 a random number generator.
 trace tapes:
tapes created by monitoring the real system,
recording the sequence of actual events.
Evaluation of CPU Schedulers
by Simulation
Implementation
Put data into a real system and see how it
works.
: The only accurate way
: 1. cost is too high
2. environment will change (All methods
have this problem!)
e.g., To avoid moving to a lower priority
queue, a user may output a meaningless char
on the screen regularly to keep itself in the
interactive queue.
SMT: Symmetric
Multithreading
Hyper-threading technology (HTT), intel©
 providing useful work for execution units that

would otherwise be idle, for example during a


cache miss, or branch misprediction.
SMP: Allow several threads to run concurrently by
providing multiple physical processors (PPs).
SMT: … providing multiple logical processors (LPs).
Each LP has its own architecture state, including
general-purpose and machine-state registers.
 LP is responsible for its own interrupt handling
SMT is a feature provided by H/W (state, interrupt
handling), not S/W.
 Certain performance gain are possible if OS is aware that.
 e.g., Consider a system with two PPs, both are idle. The
scheduler should first try scheduling separate threads on
each PP rather than on separate LPs on the same PP.
Homework 3
Reading Assignment
Bibliographical Notes
Written Assignment
 Problems:

Due:

You might also like