# Operating Systems

================ Start Lecture #10 ================

### Selfish RR (SRR, **, SRR, **)

• Preemptive.
• Perhaps it should be called “snobbish RR”.
• “Accepted processes” run RR.
• Accepted process have their priority increase at rate b≥0.
• A new process starts at priority 0; its priority increases at rate a≥0.
• A new process becomes an accepted process when its priority reaches that of an accepted process (or when there are no accepted processes).
• Once a process is accepted it remains accepted until it terminates.
• Note that at any time all accepted processes have same priority.
• If b≥a, get FCFS.
• If b=0, get RR.
• If a>b>0, it is interesting.
• If b>a=0, you get RR in "batches".

### Shortest Job First (SPN, SJF, SJF, SJF)

Sort jobs by total execution time needed and run the shortest first.

• Nonpreemptive
• First consider a static situation where all jobs are available in the beginning and we know how long each one takes to run. For simplicity lets consider “run-to-completion”, also called “uniprogrammed” (i.e., we don't even switch to another process on I/O). In this situation, uniprogrammed SJF has the shortest average waiting time.
• Assume you have a schedule with a long job right before a short job.
• Consider swapping the two jobs.
• This decreases the wait for the short by the length of the long job and increases the wait of the long job by the length of the short job.
• This decreases the total waiting time for these two.
• Hence decreases the total waiting for all jobs and hence decreases the average waiting time as well.
• Hence, whenever a long job is right before a short job, we can swap them and decrease the average waiting time.
• Thus the lowest average waiting time occurs when there are no short jobs right before long jobs.
• This is uniprogrammed SJF.
• In the more realistic case of true SJF where the scheduler switches to a new process when the currently running process blocks (say for I/O), we should call the policy shortest next-CPU-burst first.
• The difficulty is predicting the future (i.e., knowing in advance the time required for the job or next-CPU-burst).
• This is an example of priority scheduling.

### Preemptive Shortest Job First (PSPN, SRT, PSJF/SRTF, --)

Preemptive version of above

• 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 in the ready list will require less time than the remaining time for the currently running process. Why?
Ans: When the process joined the ready list it would have started running if the current process had more time remaining. Since that didn't happen the current job had less time remaining and now it has even less.
• Can starve a process that require a long burst.
• This is fixed by the standard technique.
• What is that technique?
Ans: Priority aging.
• Another example of priority scheduling.
• Consider three processes all starting at time 0. One requires 1ms, the second 100ms, the third 10sec (seconds). Compute the total/average waiting time and compare to RR q=1ms, FCFS, and PS.

### Highest Penalty Ratio Next (HPRN, HRN, **, **)

Run the process that has been “hurt” the most.

• For each process, let r = T/t; where T is the wall clock time this process has been in system and t is the running time of the process to date.
• If r=2.5, that means the job has been running 1/2.5 = 40% of the time it has been in the system.
• We call r the penalty ratio and run the process having the highest r value.
• We must worry about a process that just enters the system since t=o and hence the ratio is undefined. Define t to be the max of 1 and the running time to date. Since now t is at least 1, the ratio is always defined.
• HPRN is normally defined to be non-preemptive (i.e., the system only checks r when a burst ends), but there is an preemptive analogue
• When putting process into the run state compute the time at which it will no longer have the highest ratio and set a timer.
• When a process is moved into the ready state, compute its ratio and preempt if needed.
• HRN stands for highest response ratio next and means the same thing.
• This policy is yet another example of priority scheduling