《操作系统》的实验代码。
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.

116 lines
4.3 KiB

  1. This program, scheduler.py, allows you to see how different schedulers perform
  2. under scheduling metrics such as response time, turnaround time, and total
  3. wait time. Three schedulers are "implemented": FIFO, SJF, and RR.
  4. There are two steps to running the program.
  5. First, run without the -c flag: this shows you what problem to solve without
  6. revealing the answers. For example, if you want to compute response,
  7. turnaround, and wait for three jobs using the FIFO policy, run this:
  8. ./scheduler.py -p FIFO -j 3 -s 100
  9. If that doesn't work, try this:
  10. python ./scheduler.py -p FIFO -j 3 -s 100
  11. This specifies the FIFO policy with three jobs, and, importantly, a specific
  12. random seed of 100. If you want to see the solution for this exact problem,
  13. you have to specify this exact same random seed again. Let's run it and see
  14. what happens. This is what you should see:
  15. prompt> ./scheduler.py -p FIFO -j 3 -s 100
  16. ARG policy FIFO
  17. ARG jobs 3
  18. ARG maxlen 10
  19. ARG seed 100
  20. Here is the job list, with the run time of each job:
  21. Job 0 (length = 1)
  22. Job 1 (length = 4)
  23. Job 2 (length = 7)
  24. Compute the turnaround time, response time, and wait time for each job. When
  25. you are done, run this program again, with the same arguments, but with -c,
  26. which will thus provide you with the answers. You can use -s <somenumber> or
  27. your own job list (-l 10,15,20 for example) to generate different problems for
  28. yourself.
  29. As you can see from this example, three jobs are generated: job 0 of length 1,
  30. job 1 of length 4, and job 2 of length 7. As the program states, you can now
  31. use this to compute some statistics and see if you have a grip on the basic
  32. concepts.
  33. Once you are done, you can use the same program to "solve" the problem and see
  34. if you did your work correctly. To do so, use the "-c" flag. The output:
  35. prompt> ./scheduler.py -p FIFO -j 3 -s 100 -c
  36. ARG policy FIFO
  37. ARG jobs 3
  38. ARG maxlen 10
  39. ARG seed 100
  40. Here is the job list, with the run time of each job:
  41. Job 0 (length = 1)
  42. Job 1 (length = 4)
  43. Job 2 (length = 7)
  44. ** Solutions **
  45. Execution trace:
  46. [time 0] Run job 0 for 1.00 secs (DONE)
  47. [time 1] Run job 1 for 4.00 secs (DONE)
  48. [time 5] Run job 2 for 7.00 secs (DONE)
  49. Final statistics:
  50. Job 0 -- Response: 0.00 Turnaround 1.00 Wait 0.00
  51. Job 1 -- Response: 1.00 Turnaround 5.00 Wait 1.00
  52. Job 2 -- Response: 5.00 Turnaround 12.00 Wait 5.00
  53. Average -- Response: 2.00 Turnaround 6.00 Wait 2.00
  54. As you can see from the figure, the -c flag shows you what happened. Job 0 ran
  55. first for 1 second, Job 1 ran second for 4, and then Job 2 ran for 7
  56. seconds. Not too hard; it is FIFO, after all! The execution trace shows these
  57. results.
  58. The final statistics are useful too: they compute the "response time" (the
  59. time a job spends waiting after arrival before first running), the "turnaround
  60. time" (the time it took to complete the job since first arrival), and the
  61. total "wait time" (any time spent ready but not running). The stats are shown
  62. per job and then as an average across all jobs. Of course, you should have
  63. computed these things all before running with the "-c" flag!
  64. If you want to try the same type of problem but with different inputs, try
  65. changing the number of jobs or the random seed or both. Different random seeds
  66. basically give you a way to generate an infinite number of different problems
  67. for yourself, and the "-c" flag lets you check your own work. Keep doing this
  68. until you feel like you really understand the concepts.
  69. One other useful flag is "-l" (that's a lower-case L), which lets you specify
  70. the exact jobs you wish to see scheduled. For example, if you want to find out
  71. how SJF would perform with three jobs of lengths 5, 10, and 15, you can run:
  72. prompt> ./scheduler.py -p SJF -l 5,10,15
  73. ARG policy SJF
  74. ARG jlist 5,10,15
  75. Here is the job list, with the run time of each job:
  76. Job 0 (length = 5.0)
  77. Job 1 (length = 10.0)
  78. Job 2 (length = 15.0)
  79. ...
  80. And then you can use -c to solve it again. Note that when you specify the
  81. exact jobs, there is no need to specify a random seed or the number of jobs:
  82. the jobs lengths are taken from your comma-separated list.
  83. Of course, more interesting things happen when you use SJF (shortest-job
  84. first) or even RR (round robin) schedulers. Try them and see!
  85. And you can always run
  86. ./scheduler.py -h
  87. to get a complete list of flags and options (including options such as setting
  88. the time quantum for the RR scheduler).