In the print dialog, choose "Save as PDF" as the destination.
Operating Systems 1, Lab 7
One set of processes, six schedulers: build every Gantt chart by hand, then let a C# program check you.
Part 1
One chart, three formulas, a few rules
The lecture's FCFS example: P3 arrives at 0, P1 at 4, P5 at 5, P2 and P4 at 6
| P3 | idle | P1 | P5 | P2 | P4 |
0 3 4 9 13 17 193 to 4)| Symbol | Meaning |
|---|---|
S | start of the process's last box in the chart |
R | time the process ran before that box |
Σ | sum of a column, so ΣTAT / n is the average TAT of n processes |
| Process | AT | BT | Priority |
|---|---|---|---|
| P1 | 0 | 8 | 4 |
| P2 | 2 | 3 | 5 |
| P3 | 3 | 4 | 2 |
| P4 | 4 | 2 | 1 |
| P5 | 6 | 7 | 3 |
Part 2
Non-preemptive: once a process has the CPU, it keeps it until it finishes
Three processes arrive at 0 with bursts 18, 3, 3. Predict the average WT for each order.
| Order | Waiting times | Average WT |
|---|---|---|
| P1, P2, P3 | 0, 18, 21 | 13 |
| P2, P3, P1 | 0, 3, 6 | 3 |
Serve strictly by arrival: P1 at 0, P2 at 2, P3 at 3, P4 at 4, P5 at 6
| P1 | P2 | P3 | P4 | P5 |
0 8 11 15 17 24| Process | CT | TAT = CT - AT | WT = TAT - BT |
|---|---|---|---|
| P1 | 8 | 8 - 0 = 8 | 8 - 8 = 0 |
| P2 | 11 | 11 - 2 = 9 | 9 - 3 = 6 |
| P3 | 15 | 15 - 3 = 12 | 12 - 4 = 8 |
| P4 | 17 | 17 - 4 = 13 | 13 - 2 = 11 |
| P5 | 24 | 24 - 6 = 18 | 18 - 7 = 11 |
Same FCFS order, but every dispatch first costs 1 unit (del)
| del | P1 | del | P2 | del | P3 | del | P4 | del | P5 |
0 1 9 10 13 14 18 19 21 22 29P1 runs 0 to 8 untouched. Who is waiting at 8, and in what order?
| Process | BT | Runs |
|---|---|---|
| P4 | 2 | 1st |
| P2 | 3 | 2nd |
| P3 | 4 | 3rd |
| P5 | 7 | 4th |
| P1 | P4 | P2 | P3 | P5 |
0 8 10 13 17 24| Process | CT | TAT = CT - AT | WT = TAT - BT |
|---|---|---|---|
| P1 | 8 | 8 - 0 = 8 | 8 - 8 = 0 |
| P2 | 13 | 13 - 2 = 11 | 11 - 3 = 8 |
| P3 | 17 | 17 - 3 = 14 | 14 - 4 = 10 |
| P4 | 10 | 10 - 4 = 6 | 6 - 2 = 4 |
| P5 | 24 | 24 - 6 = 18 | 18 - 7 = 11 |
Preemptive SJF: at every arrival, compare the newcomer's burst with the remaining time of the running process
| Time | Event | Decision |
|---|---|---|
| 0 | P1 arrives (8) | P1 runs |
| 2 | P2 arrives (3); P1 has 6 left | 3 < 6: P2 preempts P1 |
| 3 | P3 arrives (4); P2 has 2 left | P2 keeps the CPU |
| 4 | P4 arrives (2); P2 has 1 left | 2 is not less than 1: P2 keeps it |
| 5 | P2 finishes | left: P1 6, P3 4, P4 2: P4 runs |
| Time | Event | Decision |
|---|---|---|
| 6 | P5 arrives (7); P4 has 1 left | P4 keeps the CPU |
| 7 | P4 finishes | P1 6, P3 4, P5 7: P3 runs |
| 11 | P3 finishes | P1 6, P5 7: P1 runs |
| 17 | P1 finishes | P5 runs to 24 |
| P1 | P2 | P4 | P3 | P1 | P5 |
0 2 5 7 11 17 24Read each WT straight off the chart: last start - time run before - AT
| Process | Last start - run before - AT | WT |
|---|---|---|
| P1 | 11 - 2 - 0 | 9 |
| P2 | 2 - 0 - 2 | 0 |
| P3 | 7 - 0 - 3 | 4 |
| P4 | 5 - 0 - 4 | 1 |
| P5 | 17 - 0 - 6 | 11 |
Part 3
Who matters more, and who has waited long enough
P1 (priority 4) runs 0 to 8; P3 (2) and P4 (1) arrive meanwhile and must wait
| P1 | P4 | P3 | P5 | P2 |
0 8 10 14 21 24| Time | Event | Decision |
|---|---|---|
| 2 | P2 arrives (priority 5) | worse than P1 (4): P1 continues |
| 3 | P3 arrives (2) | better than P1 (4): P3 preempts P1 |
| 4 | P4 arrives (1) | better than P3 (2): P4 preempts P3 |
| 6 | P4 finishes; P5 arrives (3) | best waiting: P3 (2) |
| 9 | P3 finishes | then P5 (3), P1 (4), P2 (5) |
Predict the chart from the decisions, then the WT of each process
| P1 | P3 | P4 | P3 | P5 | P1 | P2 |
0 3 4 6 9 16 21 24P2 has the worst priority: it waits 19 of the 24 units in both priority chartsqQueue shown head first, after the decision
| Time | Event | CPU and queue |
|---|---|---|
| 0 | P1 arrives | P1 runs; queue empty |
| 2 | P2 arrives | P1 runs; queue P2 |
| 3 | P3 arrives and P1's quantum ends | P2 runs; queue P3, P1 |
| 4 | P4 arrives | P2 runs; queue P3, P1, P4 |
| 6 | P5 arrives; P2 finishes (needed 3) | P3 runs; queue P1, P4, P5 |
| Time | Event | CPU and queue |
|---|---|---|
| 9 | P3's quantum ends (1 left) | P1 runs; queue P4, P5, P3 |
| 12 | P1's quantum ends (2 left) | P4 runs; queue P5, P3, P1 |
| 14 | P4 finishes (needed 2) | P5 runs; queue P3, P1 |
| 17 | P5's quantum ends (4 left) | P3 runs; queue P1, P5 |
| 18 | P3 finishes | P1 runs 18 to 20, then P5 alone to 24 |
Two P5 boxes at the end: its quantum ends at 23, the queue is empty, it gets the CPU straight back
| P1 | P2 | P3 | P1 | P4 | P5 | P3 | P1 | P5 | P5 |
0 3 6 9 12 14 17 18 20 23 24Set A again, only q changes
| Quantum | Average WT | Process switches |
|---|---|---|
| 1 | 9.2 | 20 |
| 3 | 8.6 | 8 |
| 4 | 6.6 | 5 |
| 8 | 7.2 | 4 |
Part 4
Six schedulers on one input, and schedulers built from queues
System processes
highest priority
Interactive processes
Interactive editing processes
Batch processes
Student processes
lowest priority
Q0
quantum 8
Q1
quantum 16
Q2
FCFS
30 ms burst: 8 in Q0, 16 in Q1, the last 6 in Q2Four exam-style questions from the lecture problems
n tasks with known run times on one CPU: which algorithm gives the maximum throughput: RR, SJF, highest response ratio next, or FCFS?T units; all arrive at 0, no I/O. Which algorithm is this equivalent to?Part 5
Let a program draw the same charts
public class Process
{
public string Id { get; }
public int Arrival { get; }
public int Burst { get; }
public int Priority { get; }
public int Remaining { get; set; }
public int Completion { get; set; }
// constructor: Remaining = burst
}
public record Slice(string Id, int Start, int End);Remaining drops by 1 for every time unit the process runsCompletion (CT) is set when Remaining reaches 0Slice is one box of the Gantt chartwhile (done < ps.Count)
{
var ready = ps.Where(p => p.Arrival <= time && p.Remaining > 0 && p != running)
.OrderBy(rank).ThenBy(p => p.Arrival).ThenBy(p => p.Id).ToList();
if (running == null)
running = ready.FirstOrDefault();
else if (preemptive && ready.Count > 0 && rank(ready[0]) < rank(running))
running = ready[0];
AddTick(gantt, running?.Id ?? "idle", time, false);
if (running != null && --running.Remaining == 0)
{
running.Completion = time + 1; running = null; done++;
}
time++;
}foreach (var p in ps.Where(p => p.Arrival == time).OrderBy(p => p.Id))
queue.Enqueue(p); // 1. new arrivals join first
if (running != null && used == quantum)
{
queue.Enqueue(running); // 2. then the preempted process
running = null;
}bool dispatched = false;
if (running == null && queue.Count > 0)
{
running = queue.Dequeue(); // 3. the head gets the CPU
used = 0;
dispatched = true;
}var ps = SetA();
Report.Print("FCFS", ps, Scheduler.Run(ps, p => p.Arrival, false));
ps = SetA();
Report.Print("SJF (non-preemptive)", ps, Scheduler.Run(ps, p => p.Burst, false));
ps = SetA();
Report.Print("SRTF (preemptive SJF)", ps, Scheduler.Run(ps, p => p.Remaining, true));The SRTF block of the program's output. Predict it from our chart first.
=== SRTF (preemptive SJF)
P1 0-2 | P2 2-5 | P4 5-7 | P3 7-11 | P1 11-17 | P5 17-24
P1 AT= 0 BT= 8 CT=17 TAT=17 WT= 9
P2 AT= 2 BT= 3 CT= 5 TAT= 3 WT= 0
P3 AT= 3 BT= 4 CT=11 TAT= 8 WT= 4
P4 AT= 4 BT= 2 CT= 7 TAT= 3 WT= 1
P5 AT= 6 BT= 7 CT=24 TAT=18 WT=11
Average WT = 5.00 Average TAT = 9.80Part 6
About 30 minutes: solve on paper, then check with the widget or the C# program
About 6 minutes
| Process | AT | BT |
|---|---|---|
| P1 | 1 | 4 |
| P2 | 0 | 2 |
| P3 | 3 | 3 |
| P4 | 3 | 1 |
| P5 | 13 | 2 |
| P2 | P1 | P3 | P4 | idle | P5 |
0 2 6 9 10 13 15About 8 minutes
| Process | AT | BT |
|---|---|---|
| P1 | 0 | 8 |
| P2 | 2 | 2 |
| P3 | 6 | 5 |
| P4 | 7 | 1 |
| P1 | P2 | P1 | P4 | P1 | P3 |
0 2 4 7 8 11 16About 8 minutes
| Process | AT | BT | Priority |
|---|---|---|---|
| P1 | 0 | 6 | 3 |
| P2 | 2 | 2 | 5 |
| P3 | 3 | 3 | 2 |
| P4 | 4 | 4 | 1 |
| P5 | 7 | 3 | 4 |
| P1 | P2 | P1 | P5 | P1 | P3 | P4 |
0 2 4 7 10 11 14 18About 8 minutes
| Process | AT | BT |
|---|---|---|
| P1 | 0 | 3 |
| P2 | 1 | 2 |
| P3 | 4 | 5 |
| Process | AT | BT |
|---|---|---|
| P4 | 6 | 3 |
| P5 | 7 | 4 |
| P6 | 8 | 2 |
| P1 | P2 | P1 | P3 | P4 | P5 | P3 | P6 | P4 | P5 | P3 |
0 2 4 5 7 9 11 13 15 16 18 19TAT = CT - AT and WT = TAT - BT; for preemptive charts, WT = S - R - ATMahmoud Abas|CPU Scheduling: Practice and a C# Simulation