WEBVTT

00:00.000 --> 00:05.706
Midterm 1, Section 02, Problem 3: Scheduling Policies as Traits.

00:06.306 --> 00:11.744
We'll work it out on the exam's own pages, as the answer key's handwriting appears.

00:12.344 --> 00:16.236
A script reveals it, and this explainer was built with Claude.

00:16.760 --> 00:23.575
Problem 3 draws on 07r_traits, from week 4, on Thursday, September 17.

00:24.175 --> 00:27.694
One of the pieces you wrote there was pick_next for RoundRobin:

00:28.294 --> 00:33.925
scan forward from a cursor, take the first Runnable slot, and move the cursor past it.

00:34.440 --> 00:39.206
The exercise gave you the trait Scheduler, with its default method run_for,

00:39.806 --> 00:41.438
and the Priority policy.

00:42.038 --> 00:46.688
And its README set out the two kinds of dispatch, static and dynamic.

00:47.800 --> 00:51.810
Problem 3 opens with the code of exercise 07r.

00:52.810 --> 00:56.490
It separates the scheduling policy from the loop that uses it.

00:57.090 --> 01:00.652
Each process slot is Unused, Runnable or Sleeping.

01:01.160 --> 01:04.786
The trait Scheduler names what every policy must provide.

01:05.386 --> 01:10.254
Its one required method is pick_next: the slot to run next, or None.

01:10.854 --> 01:15.082
run_for is a default method, written once, in the trait itself.

01:15.600 --> 01:18.350
Each turn, run_for calls pick_next once.

01:18.950 --> 01:21.433
It pushes each slot it is given onto picked,

01:22.033 --> 01:25.860
and the first None ends the loop early. Then it returns picked.

01:26.360 --> 01:29.548
RoundRobin is the policy whose pick_next you wrote.

01:30.148 --> 01:34.511
It keeps one cursor, next, and RoundRobin::new() starts it at 0.

01:35.111 --> 01:36.903
pick_next scans forward from it:

01:37.503 --> 01:42.003
slot is next plus off, mod n, which wraps the scan around the end.

01:42.520 --> 01:45.955
At the first Runnable slot, the cursor moves one past it,

01:46.555 --> 01:50.031
wrapping the same way, and pick_next returns Some(slot).

01:50.631 --> 01:54.714
If no slot is Runnable, the loop runs out, and it returns None.

01:56.240 --> 02:02.550
Part (a). This code runs RoundRobin for 5 turns, on these states, one for each slot.

02:03.150 --> 02:06.051
Fill in the table for the five calls to pick_next.

02:06.651 --> 02:08.762
Calls 1 and 2 are done for you.

02:09.280 --> 02:12.447
Call 1 starts with rr.next at 0.

02:13.047 --> 02:18.512
Slot 0 is Unused, so the scan moves on to slot 1, which is Runnable.

02:19.112 --> 02:23.279
It returns Some(1), and the cursor moves one past it, to 2.

02:23.800 --> 02:25.452
Call 2 starts at 2.

02:26.052 --> 02:31.186
Slot 2 is Sleeping, so the scan moves on to slot 3, which is Runnable.

02:31.786 --> 02:35.689
So it returns Some(3), and rr.next is 4.

02:37.200 --> 02:40.684
Call 3 starts where call 2 left the cursor, at 4.

02:41.684 --> 02:45.021
Slot 4 is Runnable, so the scan stops right there,

02:46.021 --> 02:47.791
and it returns Some(4).

02:48.791 --> 02:53.128
The cursor moves one past it, which wraps around the end, to 0.

02:54.640 --> 02:58.437
Call 4 starts where call 3 left the cursor, at 0.

02:59.437 --> 03:04.902
Slot 0 is Unused, so the scan moves on to slot 1, which is Runnable.

03:05.902 --> 03:07.287
It returns Some(1),

03:08.287 --> 03:12.084
and the cursor moves to 2. Call 4 repeats call 1.

03:13.600 --> 03:15.326
Call 5 starts at 2.

03:16.326 --> 03:21.460
Slot 2 is Sleeping, so the scan moves on to slot 3, which is Runnable.

03:22.460 --> 03:23.921
It returns Some(3),

03:24.921 --> 03:28.824
and the cursor moves to 4. Call 5 repeats call 2.

03:29.920 --> 03:33.108
No call returned None, so all 5 turns ran.

03:34.608 --> 03:36.643
picked is [1, 3, 4, 1, 3]:

03:37.243 --> 03:41.925
run_for pushes each slot that pick_next returns, in the order returned.

03:43.040 --> 03:48.211
Slots 1, 3 and 4 can run, and each ran once before any ran twice.

03:48.811 --> 03:53.705
The rotation always comes back around: round robin starves no slot.

03:55.440 --> 03:56.496
Part (b).

03:57.496 --> 04:01.217
The 07r file has a second policy, Priority,

04:01.817 --> 04:07.555
which runs the most urgent runnable slot. And these two functions run any policy.

04:08.080 --> 04:09.625
run_static is generic:

04:10.225 --> 04:15.045
S stands for any one type that implements Scheduler, chosen at each call.

04:15.645 --> 04:18.523
Its body runs that policy for 3 turns.

04:19.040 --> 04:20.884
run_dyn is not generic.

04:21.484 --> 04:23.572
It takes a reference to dyn Scheduler:

04:24.172 --> 04:30.207
a value of any type that implements Scheduler, though run_dyn itself is not told which.

04:31.320 --> 04:39.681
A program calls run_static four times: with a Priority, a RoundRobin, another Priority, and another RoundRobin.

04:40.281 --> 04:43.213
It calls run_dyn twice: once with each.

04:43.720 --> 04:47.804
(i): how many copies of run_static does the compiler generate?

04:49.304 --> 04:50.450
(B), 2.

04:51.450 --> 04:52.484
One per type:

04:53.084 --> 04:58.428
one copy for each distinct type it is called with, Priority and RoundRobin.

04:59.560 --> 05:07.303
The calls with another Priority and another RoundRobin reuse those two copies: (D), 4, would be one per call.

05:07.903 --> 05:11.025
This is monomorphization, or static dispatch:

05:11.625 --> 05:16.520
each copy knows its type, so its calls are resolved at compile time.

05:17.040 --> 05:19.403
(ii): how many copies of run_dyn?

05:20.903 --> 05:21.841
(A), 1.

05:22.441 --> 05:28.017
run_dyn is not generic, so there is exactly one copy, shared by both calls.

05:28.520 --> 05:32.432
(iii): then how does that one copy call the right type's methods?

05:33.932 --> 05:35.265
(A), at run time.

05:35.865 --> 05:40.653
The reference carries a pointer to a table of that type's methods, a vtable,

05:41.253 --> 05:43.502
and each call looks up its method there.

05:44.600 --> 05:46.423
That is dynamic dispatch.

05:47.023 --> 05:53.856
(B) is static dispatch, the way run_static works: a copy per type, chosen at compile time.

05:54.456 --> 05:59.160
run_dyn has only its one copy, so the choice waits for run time.

06:00.880 --> 06:04.775
Part (c). Backward is a policy that rotates the other way.

06:05.375 --> 06:08.157
Each call looks at the slots going down from next,

06:08.757 --> 06:13.697
wrapping from 0 around to the last slot, and takes the first Runnable one.

06:14.200 --> 06:17.665
Each time it looks at a slot, it steps the cursor down one,

06:18.265 --> 06:20.419
wrapping from 0 to the last slot.

06:21.019 --> 06:23.631
And Backward::new() starts with next at 0.

06:24.160 --> 06:26.953
In the code, the slot it looks at is next itself,

06:27.553 --> 06:31.094
and the cursor steps down first, before the slot is tested.

06:31.694 --> 06:33.069
Two boxes are blank:

06:33.669 --> 06:38.077
box 1, for when next is 0, and box 2, for any other next.

06:38.600 --> 06:41.056
(i): which option goes in each box?

06:41.656 --> 06:46.359
Box 1 runs when next is 0: the cursor wraps to the last slot.

06:47.859 --> 06:49.831
That is (A), n - 1.

06:50.431 --> 06:54.575
(C), n, is out of bounds: the last slot is n - 1.

06:55.080 --> 06:57.885
Box 2 runs whenever next is not 0.

06:59.385 --> 07:02.200
That is (B), self.next - 1:

07:02.800 --> 07:07.599
the cursor steps down one. (D) would jump to the last slot every time.

07:08.120 --> 07:14.168
(ii): why test whether next is 0, instead of always using self.next - 1?

07:15.668 --> 07:19.100
(C). When next is 0, subtracting 1 underflows:

07:19.700 --> 07:24.316
a usize has no value below 0, and a debug build panics.

07:25.440 --> 07:29.406
(D) is not the reason: subtraction works on a usize.

07:30.006 --> 07:35.083
Only taking 1 from 0 fails, and the test sends that case to box 1.

07:35.683 --> 07:41.877
Nor is (B): without the test, the cursor would still step down, by self.next - 1.

07:43.600 --> 07:47.555
(iii): run Backward for 4 turns, on the states from part (a),

07:48.155 --> 07:52.665
and show the work: for each call, the slots it looks at and the slot it takes.

07:54.160 --> 07:57.551
Call 1 starts at 0, and looks at slot 0.

07:58.151 --> 08:02.752
Since next is 0, box 1 wraps the cursor to the last slot, 4.

08:03.352 --> 08:08.732
Slot 0 is Unused, so the scan goes on to slot 4, which is Runnable.

08:10.740 --> 08:13.103
It takes 4, and next is now 3:

08:13.703 --> 08:17.647
box 2 stepped the cursor down as the call looked at slot 4.

08:18.247 --> 08:21.680
Call 2 starts at 3, and slot 3 is Runnable.

08:23.200 --> 08:25.660
It takes 3 at once, and next is 2.

08:26.660 --> 08:33.475
Call 3 starts at 2. Slot 2 is Sleeping, so the scan goes on to slot 1, which is Runnable.

08:34.975 --> 08:38.754
It takes 1, and next is 0, where call 4 begins.

08:39.280 --> 08:46.680
Call 4 starts at 0. Slot 0 is Unused, and box 1 wraps the cursor around to slot 4,

08:47.280 --> 08:48.559
which is Runnable.

08:49.559 --> 08:54.070
It takes 4, and next is 3 again: call 4 repeats call 1.

08:56.100 --> 09:00.992
So run_for returns [4, 3, 1, 4]: the slots it took, in order.

09:01.592 --> 09:06.818
The same Runnable slots that RoundRobin took, visited the other way around.

09:08.520 --> 09:09.991
That's Problem 3.

09:10.591 --> 09:14.439
(a): RoundRobin moves its cursor one past the slot it takes.

09:15.039 --> 09:20.827
(b): generic code gets a copy per type used; dyn, one copy and a vtable.

09:21.427 --> 09:25.520
(c): on a usize, test for 0 before you subtract 1.
