|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+# README Scheduler: Lottery
|
|
|
2
|
+
|
|
|
3
|
+This program, lottery.py, allows you to see how a lottery scheduler works. As
|
|
|
4
|
+always, there are two steps to running the program. First, run without the -c
|
|
|
5
|
+flag: this shows you what problem to solve without revealing the answers.
|
|
|
6
|
+
|
|
|
7
|
+```text
|
|
|
8
|
+prompt> ./lottery.py -j 2 -s 0
|
|
|
9
|
+...
|
|
|
10
|
+Here is the job list, with the run time of each job:
|
|
|
11
|
+ Job 0 ( length = 8, tickets = 75 )
|
|
|
12
|
+ Job 1 ( length = 4, tickets = 25 )
|
|
|
13
|
+
|
|
|
14
|
+Here is the set of random numbers you will need (at most):
|
|
|
15
|
+Random 511275
|
|
|
16
|
+Random 404934
|
|
|
17
|
+Random 783799
|
|
|
18
|
+Random 303313
|
|
|
19
|
+Random 476597
|
|
|
20
|
+Random 583382
|
|
|
21
|
+Random 908113
|
|
|
22
|
+Random 504687
|
|
|
23
|
+Random 281838
|
|
|
24
|
+Random 755804
|
|
|
25
|
+Random 618369
|
|
|
26
|
+Random 250506
|
|
|
27
|
+```
|
|
|
28
|
+
|
|
|
29
|
+When you run the simulator in this manner, it first assigns you some random jobs
|
|
|
30
|
+(here of lengths 8, and 4), each with some number of tickets (here 75 and 25,
|
|
|
31
|
+respectively). The simulator also gives you a list of random numbers, which you
|
|
|
32
|
+will need to determine what the lottery scheduler will do. The random numbers
|
|
|
33
|
+are chosen to be between 0 and a large number; thus, you'll have to use the
|
|
|
34
|
+modulo operator to compute the lottery winner (i.e., winner should equal this
|
|
|
35
|
+random number modulo the total number of tickets in the system).
|
|
|
36
|
+
|
|
|
37
|
+Running with -c shows exactly what you are supposed to calculate:
|
|
|
38
|
+
|
|
|
39
|
+```text
|
|
|
40
|
+prompt> ./lottery.py -j 2 -s 0 -c
|
|
|
41
|
+...
|
|
|
42
|
+** Solutions **
|
|
|
43
|
+Random 511275 -> Winning ticket 75 (of 100) -> Run 1
|
|
|
44
|
+ Jobs: ( job:0 timeleft:8 tix:75 ) (* job:1 timeleft:4 tix:25 )
|
|
|
45
|
+Random 404934 -> Winning ticket 34 (of 100) -> Run 0
|
|
|
46
|
+ Jobs: (* job:0 timeleft:8 tix:75 ) ( job:1 timeleft:3 tix:25 )
|
|
|
47
|
+Random 783799 -> Winning ticket 99 (of 100) -> Run 1
|
|
|
48
|
+ Jobs: ( job:0 timeleft:7 tix:75 ) (* job:1 timeleft:3 tix:25 )
|
|
|
49
|
+Random 303313 -> Winning ticket 13 (of 100) -> Run 0
|
|
|
50
|
+ Jobs: (* job:0 timeleft:7 tix:75 ) ( job:1 timeleft:2 tix:25 )
|
|
|
51
|
+Random 476597 -> Winning ticket 97 (of 100) -> Run 1
|
|
|
52
|
+ Jobs: ( job:0 timeleft:6 tix:75 ) (* job:1 timeleft:2 tix:25 )
|
|
|
53
|
+Random 583382 -> Winning ticket 82 (of 100) -> Run 1
|
|
|
54
|
+ Jobs: ( job:0 timeleft:6 tix:75 ) (* job:1 timeleft:1 tix:25 )
|
|
|
55
|
+--> JOB 1 DONE at time 6
|
|
|
56
|
+Random 908113 -> Winning ticket 13 (of 75) -> Run 0
|
|
|
57
|
+ Jobs: (* job:0 timeleft:6 tix:75 ) ( job:1 timeleft:0 tix:--- )
|
|
|
58
|
+Random 504687 -> Winning ticket 12 (of 75) -> Run 0
|
|
|
59
|
+ Jobs: (* job:0 timeleft:5 tix:75 ) ( job:1 timeleft:0 tix:--- )
|
|
|
60
|
+Random 281838 -> Winning ticket 63 (of 75) -> Run 0
|
|
|
61
|
+ Jobs: (* job:0 timeleft:4 tix:75 ) ( job:1 timeleft:0 tix:--- )
|
|
|
62
|
+Random 755804 -> Winning ticket 29 (of 75) -> Run 0
|
|
|
63
|
+ Jobs: (* job:0 timeleft:3 tix:75 ) ( job:1 timeleft:0 tix:--- )
|
|
|
64
|
+Random 618369 -> Winning ticket 69 (of 75) -> Run 0
|
|
|
65
|
+ Jobs: (* job:0 timeleft:2 tix:75 ) ( job:1 timeleft:0 tix:--- )
|
|
|
66
|
+Random 250506 -> Winning ticket 6 (of 75) -> Run 0
|
|
|
67
|
+ Jobs: (* job:0 timeleft:1 tix:75 ) ( job:1 timeleft:0 tix:--- )
|
|
|
68
|
+--> JOB 0 DONE at time 12
|
|
|
69
|
+```
|
|
|
70
|
+
|
|
|
71
|
+As you can see from this trace, what you are supposed to do is use the random
|
|
|
72
|
+number to figure out which ticket is the winner. Then, given the winning ticket,
|
|
|
73
|
+figure out which job should run. Repeat this until all of the jobs are finished
|
|
|
74
|
+running. It's as simple as that -- you are just emulating what the lottery
|
|
|
75
|
+scheduler does, but by hand!
|
|
|
76
|
+
|
|
|
77
|
+Just to make this absolutely clear, let's look at the first decision made in the
|
|
|
78
|
+example above. At this point, we have two jobs (job 0 which has a runtime of 8
|
|
|
79
|
+and 75 tickets, and job 1 which has a runtime of 4 and 25 tickets). The first
|
|
|
80
|
+random number we are given is 511275. As there are 100 tickets in the system,
|
|
|
81
|
+511275 \% 100 is 75, and thus 75 is our winning ticket.
|
|
|
82
|
+
|
|
|
83
|
+If ticket 75 is the winner, we simply search through the job list until we find
|
|
|
84
|
+it. The first entry, for job 0, has 75 tickets (0 through 74), and thus does not
|
|
|
85
|
+win; the next entry is for job 1, and thus we have found our winner, so we run
|
|
|
86
|
+job 1 for the quantum length (1 in this example). All of this is shown in the
|
|
|
87
|
+print out as follows:
|
|
|
88
|
+
|
|
|
89
|
+```text
|
|
|
90
|
+Random 511275 -> Winning ticket 75 (of 100) -> Run 1
|
|
|
91
|
+ Jobs: ( job:0 timeleft:8 tix:75 ) (* job:1 timeleft:4 tix:25 )
|
|
|
92
|
+```
|
|
|
93
|
+
|
|
|
94
|
+As you can see, the first line summarizes what happens, and the second simply
|
|
|
95
|
+shows the entire job queue, with an * denoting which job was chosen.
|
|
|
96
|
+
|
|
|
97
|
+The simulator has a few other options, most of which should be self-explanatory.
|
|
|
98
|
+Most notably, the -l/--jlist flag can be used to specify an exact set of jobs
|
|
|
99
|
+and their ticket values, instead of always using randomly-generated job lists.
|
|
|
100
|
+
|
|
|
101
|
+```text
|
|
|
102
|
+prompt> ./lottery.py -h
|
|
|
103
|
+Usage: lottery.py [options]
|
|
|
104
|
+
|
|
|
105
|
+Options:
|
|
|
106
|
+ -h, --help
|
|
|
107
|
+ show this help message and exit
|
|
|
108
|
+ -s SEED, --seed=SEED
|
|
|
109
|
+ the random seed
|
|
|
110
|
+ -j JOBS, --jobs=JOBS
|
|
|
111
|
+ number of jobs in the system
|
|
|
112
|
+ -l JLIST, --jlist=JLIST
|
|
|
113
|
+ instead of random jobs, provide a comma-separated list
|
|
|
114
|
+ of run times and ticket values (e.g., 10:100,20:100
|
|
|
115
|
+ would have two jobs with run-times of 10 and 20, each
|
|
|
116
|
+ with 100 tickets)
|
|
|
117
|
+ -m MAXLEN, --maxlen=MAXLEN
|
|
|
118
|
+ max length of job
|
|
|
119
|
+ -T MAXTICKET, --maxtick=MAXTICKET
|
|
|
120
|
+ maximum ticket value, if randomly assigned
|
|
|
121
|
+ -q QUANTUM, --quantum=QUANTUM
|
|
|
122
|
+ length of time slice
|
|
|
123
|
+ -c, --compute
|
|
|
124
|
+ compute answers for me
|
|
|
125
|
+
|
|
|
126
|
+```
|