《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.9 KiB

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