CS 537 Notes, Section #11: Scheduling and CPU Scheduling


OSTEP, Chapters 7-10

Scheduling

Until now we have talked about processes, from now on we will talk about resources, the things operated upon by processes. Resources range from cpu time to disk space to channel I/O time.

Resources fall into two classes:

OS makes two related kinds of decisions about resources:

Resource #1: the processor.


CPU Scheduling

Processes may be in any one of three general scheduling states:

There are two parts to CPU scheduling:

This is an example of policy/mechanism separation.

Goals for Scheduling Disciplines

Measures of Performance

There are several important ways to measure performance:


FCFS (also called FIFO)

Run until finished.

FCFS Queue

Solution: limit maximum amount of time that a process can run without a context switch. This time is called a time slice.


Round Robin

Run process for one time slice, then move to back of queue. Each process gets equal share of the CPU. Most systems use some variant of this. What happens if the time slice is not chosen carefully?

Round Robin

Originally, Unix had 1 sec. time slices. Too long. Most timesharing systems today use time slices of 10,000 - 100,000 instructions.

Implementation of priorities: run highest priority processes first, use round-robin among processes of equal priority. Re-insert process in run queue behind all processes of greater or equal priority.

Even round-robin can produce bad results occasionally. Go through example of ten processes each requiring 100 time slices.

What is the best we can do?


STCF

Shortest time to completion first with preemption. This minimizes the average response time.

STCF Queue

As an example, show two processes, one doing 1 ms computation followed by 10 ms I/O, one doing all computation. Suppose we use 100 ms time slice: I/O process only runs at 1/10th speed, effective I/O time is 100 ms. Suppose we use 1 ms time slice: then compute-bound process gets interrupted 9 times unnecessarily for each valid interrupt. STCF works quite nicely.

Unfortunately, STCF requires knowledge of the future. Instead, we can use past performance to predict future performance.


Exponential Queue

(Also called "multi-level feedback queues".) Attacks both efficiency and response time problems.

Exponential Queues

Linux's new scheduler (version 2.6) makes this all very complex:


Summary:


Priority Inversion Problem

There are some curious interactions between scheduling and synchronization. A classic problem caused by this interaction was first observed in 1979 by Butler Lampson and David Redell at Xerox.

Suppose that you have three processes:
P1: Highest priority
P2: Medium priority
P3: Lowest priority

And suppose that you have the following critical section, S:

   S:  P(mutex)
       . . .
       . . .
       V(mutex)

The three processes execute as follows:

  1. P3 enters S, locking the critical section.
  2. P3 is preempted by the scheduler and P2 starts running.
  3. P2 is preempted by the scheduler and P1 starts running.
  4. P1 tries to enter S and is blocked at the P operation.
  5. P2 starts running again, preventing P1 from running.

So, what's going wrong here?
To really understand this situation, you should try to work out the example for yourself, before continuing to read.

As a result, P2 running (at medium priority) is blocking P1 (at highest priority) from running. This example is not an academic one. Many designers of real-time systems, where priority can be crucial, have stumbled over issue. You can read the original paper by Lampson and Redell to see their suggestion for handling the situation. Also, do a Web search for priority inversion.

Copyright © 2013, 2018 Barton P. Miller
Non-University of Wisconsin students and teachers are welcome to print these notes their personal use. Further reproduction requires permission of the author.