CS 111 Fall 2005

Scribe Notes for 10/18/05

by Eduardo Duque, Rafayel Ambartsumyan, Amit Halani

Scheduling and a Bit of Synchronization!


Scheduling

Say you had the following 4 tasks with corresponding time units, priority, and quantum number.

Quantum = 3.

Task
Time Units
Priority
A
5
2
B
2
2
C
9
0
D
4
1

Remember that a numerically lower priority value takes precedence.

Strict Priority:

  • Strict Priority: Run the process with the highest priority. If there is a choice, run those round-robin.

The scheduler running strict priority would provide the following Gantt Chart:

Process Isolation & Robustness

The question to ask is "Does priority setting require privilege?"

  • Lowering your own priority (giving yourself a numerically greater value) does not require privilege.

Background Task/Process

  • This is a task with no real deadline
  • It is safe to run a background task at low priority
  • It takes up resources only when nothing else needs them.

Effective Priority

Say you set the time units for task C to be 3,000,000.

Question: What would happen?

Answer: Doing this will starve everyone but task C. Remember that our goal is Priority with no starvation.

  • Effective Priority: This is round robin with priority except the round robin uses an effective priority that is calculated from the real priority.

Aging

Aging works like this: Penalize a process that has run for too long by doing the following:

  • Increment value by 1 if a process ran >= 40% of the time in the last 10 quanta.
  • Increment by 2 if a process ran >= 40% of the time in the last 20 quanta.
  • Furthermore, the effective priority is the real priority unless one of the previous conditions are met.

Example: Suppose you had a quantum value of 1. Tasks A, B, C, and D all have time units (tau) equal to infinity.

Task
Time Units
Priority
A
Infinity
2
B
Infinity
2
C
Infinity
0
D
Infinity
1

The scheduler running this scheme will produce the following Gantt Chart:

A problem with aging: You can still starve! How?

Converse of Aging: Favor processes that haven't run in a long time. This is used by systems like BSD and Linux.

  • Every second, recalculate the effective priority based on:
    1. Estimated recent CPU usage
    2. Real Priority ("nice" level)
  • In these systems there are 0-31 different effective priorities, 0-7 which belong to the kernel.

Priority Inversion

Say you had the same tasks as before with the same priorities (A,B = 2, C = 0, and D = 1). Also note the following:

  • A is C's child
  • A is about to exit
  • C is waitpid()'ing for A to exit

Question: Which task runs?

Answer: D runs! Is this OK?

Priority Inversion: This is when a high priority process is waiting for a lower priority process so the high priority is effectively lowered.

Solution to priority inversion: When process P is blocked, waiting for process Q, we set Eprio(Q) to be the following: min(Eprio(P), Eprio(Q)).

CPU Usage Discounting

Say you had the following two processes, A and B, executing the corresponding code:

Process A
Process B

for(i=0, i<100, i++)

;

yield();

while(1);

 
while(1);

Question: Does this code portray fairness?

Answer: Because A voluntarily gave up CPU access, it has been penalized and receives less than fair share.

CPU Usage Discount: If you don't use much CPU, you run more often.

Proportional Share

In proportional share every process gets a priority class share of the CPU that is "inversely proportional" to the priority class. Therefore the higher the priority, the less the share.

The following table shows tasks A, B, C, and D with their corresponding priorities and proportional shares:

Task
Priority

Proportional Shares

A
2
1/8
B
2
1/8
C
0
1/2
D
1
1/4

What happens if you had a process E with a priority of 2? You get the following:

Task
Priority

Proportional Shares (approximately)

A
2
1/9
B
2
1/9
C
0
4/9
D
1
2/9
E
2
1/9

Pros of proportional share:

  • No starvation!
  • Simple to understand!

Cons of proportional share:

  • Hard to write the script that will implement the math!
  • The complexity is Order(log n) as opposed to Order(1) for something like Robin Robin Scheduling

Real Time Scheduling ("Hard Real Time")

This scheduling provides a guarantee of when a process will run. It also brings forth a new set of scheduling preferences: Deadlines and Run Schedules.

Reservation

Reservation occurs when the scheduler reserves a time where a task will be allowed to run. For examples consider the following for tasks A, B, C:

  • Task A - gets 1 quantum to run every 10 quanta
  • Task B - gets 4 quanta to run every 5 quanta
  • Task C - gets 2 quanta every 10 quanta

Note: There is some admission control. That is, the OS rejects scheduling requests if they cannot be fulfilled. Admission control is good for things like embedded systems for things like elevators and airplanes where it is important to ensure that certain things happen at certain times.

Question: Can all the previous tasks get what they need and therefore given their requests? Let's take a look.

After the scheduler reserves task A's request, the gantt chart looks something like the following:

A
-
-
-
-
-
-
-
-
-
A
-
-
-
-

After the scheduler reserves task B's request, the gantt chart looks something like the following:

A
B
B
B
B
-
B
B
B
B
A
B
B
B
B

Now notices that when the scheduler gets to reserving C's request there is not enough room to give C 2 quanta every 10 quanta, therefore the OS rejects scheduling task C.

Best Effort: The OS will accept every scheduling request and will do its best to full those request.

Deadline

Deadline fulfills such request from a process: "I need to run for X time units by time t, where we will refer to time t as being the deadline.

Example: Consider the following tasks and their corresponding time units and deadlines.

Task
time units

t(deadline)

A
5
10
B
1
9
C
4
15
D
2
3

Question: In what order can we run these tasks?

EDF - Earliest Deadline First: As the name suggest, let the task with the earliest deadline run first. Notice that EDF will always give you a schedule that meets a deadline if such a deadline exists!

The gant chart for EDF on tasks A, B, C, and D is as follows:

 


Synchronization

Synchronization is to ensure the safe sharing of resources.

Resources include: memory objects, files, and the network.

Problem statement: Read 3 files, output their lines in any order.

Note: Please download the code for the following problem statement from the course website!

As of now, the code will open the files in sequential order, doing the following:

That is, the files have to take CPU time and block while nothing else is going on, each file takes control at their own individual time. We want to make it so that while things are blocked, all three files are being read and opened at the same time, therefore taking up less of the CPU since there now isn't multiple blocks. In a graphical sense, we want the following to happen:

So for our program that reads 3 files and outputs their lines in any order, we should generate 3 threads, 1 for each file.