Processes alternate between using the CPU (called a CPU burst) and doing I/O.
Two categories of processes:
Priorities might be dynamic or static, but usually there is some aging scheme to prevent starvation.
Process Priority could be based on:
e.g., set priority to 1/f, where f is the fraction of last time quantum used
FCFS (First Come First Serve) - strict FIFO queue that is nonpreemptive.
Round Robin - cycle between each process giving each a CPU time quantum (a fixed amount of time); especially good for interactive or time-sharing processes
Quantum size must be chosen carefully
Shortest Job First (SJF) - give CPU to process those next CPU burst is the shortest
Optimal w.r.t. the average waiting time of a process, i.e., it's useful to improve turnaround time of batch jobs or response-time of interactive jobs
Problem: we don't know how long a process' next CPU burst will be, but we can predict it from previous CPU bursts. One approach is to exponentially average previous CPU bursts as
Estimate first burst as t1 (maybe use the system average)
Measure first CPU burst t1
Prediction for second CPU burst: t2 = at1 + (1 - a)t1, where 0 < a < 1
Measure second CPU burst t2
Prediction for third CPU burst: t3 = at2 + (1 - a)t2
...
Prediction for (n+1)st CPU burst: tn+1 = atn + (1 - a)tn,
Recall that tn was recursively based on previous measured CPU bursts:
tn+1 = atn + (1 - a)tn
= a(atn-1 + (1 - a)tn-1) + (1 - a)tn = a2tn-1 + a(1 - a)tn-1 + (1 - a)tn
= a2(atn-2 + (1 - a)tn-2) + a(1 - a)tn-1 + (1 - a)tn
= a3tn-2 + a2(1 - a)tn-2 + a(1 - a)tn-1 + (1 - a)tn
...
= (1 - a)[tn + atn-1 + a 2 tn-2 + a 3 tn-3 + ... + a n-1 t1] + a nt1
Since 0 < a < 1, larger powers of a place less weight on older burst times.
e.g., a = ½ and n = 3 (so n+1 = 4)
t4 = ½ [t3 + ½ t2 + ¼ t1] + 1/8 t1 = ½ t3 + ¼ t2 + 1/8 t1 + 1/8 t1
Special cases:
If a = 0, tn+1 = tn (only use last CPU burst as a predictor).
If a = 1, tn+1 = t1 (don't use past history only system average).
If a = ½ , place ½ weight on last CPU burst and ½ weight on older runs.
(easy to implement when a = ½ or ¼ (or any power of 2)
Often multiple queues are used with different priority processes, e.g.,
Multilevel Feedback Queue - queues each have their own priority and scheduling algorithm, but there is some method to:
Lottery scheduling - useful to allocate a resource (such as the CPU) with varying allocations
Idea: OS holds a lottery 50 times a second to see which process can use the CPU
The winning process uses the CPU for 20 msec. For example,
We'd expect that a process would get the CPU proportional to its fraction of all tickets.
Cooperating processes could exchange tickets to increase chance of running. For example, a server might be allocated zero tickets, but a client holding 30 tickets might transfer them to the server.
Multiple-Processor Scheduling
In a heterogeneous system, a process will only execute on compatible processor(s).
Several homogeneous processors allow load sharing via common ready queue.
Implementation options:
Evaluating New Scheduling Algorithms