Start Lecture #8
Remarks: Lab 2 is available (scheduling), but not
Lab 3, which will NOT be assigned for a few weeks
will require C (or C++, your choice) programming.
Shortest Remaining Time Next (PSPN, SRT, PSJF/SRTF, SRTF)
Preemptive version of above.
Indeed some authors call it preemptive shortest job first.
- Permit a process that enters the ready list to preempt the running
process if the time for the new process (or for its next burst) is
less than the remaining time for the running process (or for
its current burst).
- It will never happen that a process already in the ready list
will require less time than the remaining time for the currently
Answer: When the process joined the ready list
it would have started running if the current process had more
Since that didn't happen the currently running job had less time
remaining and now it has even less.
- PSPN Can starve a process that requires a long burst.
- This is fixed by the standard technique.
- What is that technique?
Answer: Priority aging (see below).
2.4.3 Scheduling in Interactive Systems
The following algorithms can also be used for batch systems, but in
that case, the gain may not justify the extra complexity.
Round Robin (RR, RR, RR, RR)
- An important preemptive policy.
- Essentially the preemptive version of FCFS.
- The key parameter is the quantum size
- When a process is put into the running state a timer is set to q.
- If the timer goes off and the process is still running, the OS
preempts the process.
- This process is moved to the ready state (the
preempt arc in the diagram), where it is placed at the
rear of the ready list.
- The process at the front of the ready list is removed from
the ready list and run (i.e., moves to state running).
- Note that the ready list is being treated as a queue.
Indeed it is sometimes called the ready queue, but not by me
since for other scheduling algorithms it is not accessed in a
- When a process is created, it is placed at the rear of the ready
- Note that RR works well if you have a 1 hr job and then a 3 second
- As q gets large, RR approaches FCFS.
Indeed if q is larger that the longest time any process will run
before terminating or blocking, then RR IS FCFS.
A good way to see this is to look at my favorite diagram and note
the three arcs leaving running.
triggered by three conditions: process
terminating, process blocking, and process preempted.
If the first trigger condition to arise is never preemption, we
can erase that arc and then RR becomes FCFS.
- As q gets small, RR approaches PS (Processor Sharing, described
- What value of q should we choose?
- A trade-off
- A small q makes the system more responsive, a long
compute-bound job cannot starve a short job.
- A large q makes the system more efficient since there is
less process switching.
- A reasonable time for q is a few tens of milliseconds or
perhaps a few milliseconds for a fast processor.
(millisecond = 1/1000 second and is abbreviated ms).
This means every other job can delay your job by at most q
(plus the context switch time CS, which is normally less than
Also the overhead is CS/(CS+q), which is small.
- A student found the
following reference for the name Round Robin.
A similar, but less detailed, citation can be found in wikipedia.
The round robin was originally a petition, its signatures
arranged in a circular form to disguise the order of signing.
Most probably it takes its name from the
(round ribbon), in 17th-century France, where government
officials devised a method of signing their petitions of
grievances on ribbons that were attached to the documents in a
In that way no signer could be accused of signing the document
first and risk having his head chopped off for instigating
Ruban rond later became
round robin in English and
the custom continued in the British navy, where petitions of
grievances were signed as if the signatures were spokes of a
wheel radiating from its hub.
round robin usually means a sports tournament where
all of the contestants play each other at least once and losing
a match doesn't result in immediate elimination.
Encyclopedia of Word and Phrase Origins by Robert Hendrickson
(Facts on File, New York, 1997).
Homework: 20, 35.
Round-robin schedulers normally maintain a list of all runnable
processes, with each process occurring exactly once in the list.
What would happen if a process occurred more than once in the list?
Can you think of any reason for allowing this?
Homework: Give an argument favoring a large
quantum; give an argument favoring a small quantum.
|Process||CPU Time||Creation Time|
- Consider the set of processes in the table to the right.
- When does each process finish if RR scheduling is used with q=1,
if q=2, if q=3, if q=100?
- First assume (unrealistically) that context switch time is zero.
- Then assume it is .1. Each process performs no I/O (i.e., no
process ever blocks).
- All times are in milliseconds.
- The CPU time is the total time required for the process (excluding
any context switch time).
- The creation time is the time when the process is created.
So P1 is created when the problem begins and P3 is created 5
- If two processes have equal priority (in RR this means if thy
both enter the ready state at the same cycle), we give priority
(in RR this means place first on the queue) to the process with
the earliest creation time.
If they also have the same creation time, then we give priority
to the process with the lower number.
- Remind me to discuss this last one in class next time.
Redo the previous homework for q=2 with the following change.
After process P1 runs for 3ms (milliseconds), it blocks
P1 never blocks again.
P2 never blocks.
After P3 runs for 1 ms it blocks for 1ms.
Remind me to answer this one in class next lecture.
Processor Sharing (PS, **, PS, PS)
Merge the ready and running states and permit all ready jobs to be
run at once.
However, the processor slows down so that when n jobs are running at
once, each progresses at a speed 1/n as fast as it would if it were
- Clearly impossible as stated due to the overhead of process
- Of theoretical interest (easy to analyze).
- Approximated by RR when the quantum is small.
Make sure you understand this last point.
For example, consider the last homework assignment (with zero
context switch time and no blocking) and consider q=1, q=.1,
- Show what happens for 3 processes, A, B, C, each requiring 3
seconds of CPU time.
A starts at time 0, B at 1 second, C at 2.
- Consider three processes all starting at time 0.
One requires 1ms, the second 100ms, the third 10sec (seconds).
Compute the total/average waiting time for RR q=1ms, PS, SJF,
SRTN, and FCFS.
Note that this depends on the order the processes happen to be
The effect is huge for FCFS, modest for RR with modest quantum,
and non-existent for PS and SRTN.
Variants of Round Robin
- State dependent RR
- Same as RR but q is varied dynamically depending on the state
of the system.
- Favor processes holding important resources, for example,
- Perhaps this should be considered medium term scheduling
since you probably do not recalculate q each time.
- External priorities: RR but a user can pay more and get
That is, one process can be given a higher priority than another.
But this is not an absolute priority: the lower priority (i.e.,
less important) process does get to run, but not as much as the
higher priority process.
Each job is assigned a priority (externally, perhaps by charging
more for higher priority) and the highest priority ready job is run.
- Similar to
External priorities above
- If many processes have the highest priority, use RR among
Indeed one often groups several priorities into a priority class
and employs RR within a class.
- Can easily starve processes, which can be fixed by the
- Can have the priorities changed dynamically to favor processes
holding important resources (similar to state dependent RR).
- Sometimes a large priority means an important job; sometimes a
small priority means an important job.
- Many policies can be thought of as priority scheduling in
which we run the job with the highest priority.
The different scheduling policies have different notions of
- FCFS and RR are priority scheduling where the priority is
the time last inserted on the ready list.
- SJF and SRTN are priority scheduling, where the priority
of the job is the time it needs to run in order to complete
(or complete its current CPU burst).
As a job is waiting, increase its priority; hence it will
eventually have the highest priority.
- Starvation means that some process is never
run, because it never has the highest priority.
It is also called starvation, if process runs for a while, but
then is never able to run again, even though it is ready.
The formal way to say this is that a system is free of
No job can remain in the ready state forever.
- Priority aging is the
standard technique used to
prevent starvation (assuming all jobs terminate or the policy is
- There may be many processes with the maximum priority.
- If so, can use FIFO among those with max priority (risks
starvation if a job doesn't terminate) or can use RR.
- Can apply priority aging to many policies, in particular to priority
scheduling described above.
Homework: 36, 37.
Note that when the book says RR with each process getting its fair
share, it means Processor Sharing.
Selfish RR (SRR, **, SRR, **)
SRR is a preemptive policy in which unblocked (i.e. ready and
running) processes are divided into two classes the
processes, which run RR and the
others (perhaps SRR
really stands for
- Accepted process have their priority increase at rate a≥0.
- A new process starts at priority 0; its priority increases at
- An unaccepted process becomes an accepted process when its
priority reaches that of the accepted processes (or when there are
no accepted processes).
- Hence, once a process is accepted, it remains accepted until it
terminates (or blocks, see below) and all accepted processes have
- Note that, when the only accepted process terminates (or blocks,
see below), all the process with the next highest priority become
The behavior of SRR depends on the relationship between a and b
- If a=0, get RR.
- If a≥b>0, get FCFS.
- If b>a>0, it is interesting.
- If a>b=0, you get RR in
This is similar to
n-step scan for disk I/O.
It is not clear what is supposed to happen when a process
Should its priority get reset to zero and have unblock act like
Should the priority continue to grow (at rate a or b)?
Should its priority be frozen during the blockage.
Let us assume the first case (reset to zero) since it seems