Os 52008
Os 52008
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
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
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
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
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
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
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
while (true) {
// perform CPU-intensive task
...
Thread.yield();
}
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
Due: