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

133 lines
6.1 KiB

  1. This homework uses disk.py to familiarize you with how a modern hard
  2. drive works. It has a lot of different options, and unlike most of the other
  3. simulations, has a graphical animator to show you exactly what happens when
  4. the disk is in action.
  5. [Note: there is also an experimental program, 'disk-precise.py', included
  6. in the download. This version of the simulator uses the python Decimal
  7. package for precise floating point computation, thus giving slightly
  8. better answers in some corner cases than 'disk.py'. However, it has
  9. not been very carefully tested, so use at your own caution.]
  10. Let's do a simple example first. To run the simulator and compute some basic
  11. seek, rotation, and transfer times, you first have to give a list of requests
  12. to the simulator. This can either be done by specifying the exact requests, or
  13. by having the simulator generate some randomly.
  14. We'll start by specifying a list of requests ourselves. Let's do a single
  15. request first:
  16. prompt> disk.py -a 10
  17. At this point you'll see:
  18. ...
  19. REQUESTS [br '10']
  20. For the requests above, compute the seek, rotate, and transfer times.
  21. Use -c or the graphical mode (-G) to see the answers.
  22. To be able to compute the seek, rotation, and transfer times for this request,
  23. you'll have to know a little more information about the layout of sectors, the
  24. starting position of the disk head, and so forth. To see much of this
  25. information, run the simulator in graphical mode (-G):
  26. prompt> disk.py -a 10 -G
  27. At this point, a window should appear with our simple disk on it.
  28. The disk head is positioned on the outside track, halfway through sector 6.
  29. As you can see, sector 10 (our example sector) is on the same track, about a
  30. third of the way around. The direction of rotation is counter-clockwise.
  31. To run the simulation, press the "s" key while the simulator window is
  32. highlighted.
  33. When the simulation completes, you should be able to see that the disk spent
  34. 105 time units in rotation and 30 in transfer in order to access sector 10,
  35. with no seek time. Press "q" to close the simulator window.
  36. To calculate this (instead of just running the simulation), you would need to
  37. know a few details about the disk. First, the rotational speed is by default
  38. set to 1 degree per time unit. Thus, to make a complete revolution, it takes
  39. 360 time units. Second, transfer begins and ends at the halfway point between
  40. sectors. Thus, to read sector 10, the transfer begins halfway between 9 and 10,
  41. and ends halfway between 10 and 11. Finally, in the default disk, there are
  42. 12 sectors per track, meaning that each sector takes up 30 degrees of the
  43. rotational space. Thus, to read a sector, it takes 30 time units (given our
  44. default speed of rotation).
  45. With this information in hand, you now should be able to compute the seek,
  46. rotation, and transfer times for accessing sector 10. Because the head starts
  47. on the same track as 10, there is no seek time. Because the disk rotates at
  48. 1 degree / time unit, it takes 105 time units to get to the beginning of sector
  49. 10, halfway between 9 and 10 (note that it is exactly 90 degrees to the middle
  50. of sector 9, and another 15 to the halfway point). Finally, to transfer the
  51. sector takes 30 time units.
  52. Now let's do a slightly more complex example:
  53. prompt> disk.py -a 10,11 -G
  54. In this case, we're transferring two sectors, 10 and 11. How long will it take?
  55. Try guessing before running the simulation!
  56. As you probably guessed, this simulation takes just 30 time units longer, to
  57. transfer the next sector 11. Thus, the seek and rotate times remain the same,
  58. but the transfer time for the requests is doubled. You can in fact see these
  59. sums across the top of the simulator window; they also get printed out to the
  60. console as follows:
  61. ...
  62. Sector: 10 Seek: 0 Rotate:105 Transfer: 30 Total: 135
  63. Sector: 11 Seek: 0 Rotate: 0 Transfer: 30 Total: 30
  64. TOTALS Seek: 0 Rotate:105 Transfer: 60 Total: 165
  65. Now let's do an example with a seek. Try the following set of requests:
  66. prompt> disk.py -a 10,18 -G
  67. To compute how long this will take, you need to know how long a seek will
  68. take. The distance between each track is by default 40 distance units, and the
  69. default rate of seeking is 1 distance unit per unit time. Thus, a seek from
  70. the outer track to the middle track takes 40 time units.
  71. You'd also have to know the scheduling policy. The default is FIFO, though, so
  72. for now you can just compute the request times assuming the processing order
  73. matches the list specified via the "-a" flag.
  74. To compute how long it will take the disk to service these requests, we first
  75. compute how long it takes to access sector 10, which we know from above to be
  76. 135 time units (105 rotating, 30 transferring). Once this request is complete,
  77. the disk begins to seek to the middle track where sector 18 lies, taking 40
  78. time units. Then the disk rotates to sector 18, and transfers it for 30 time
  79. units, thus completing the simulation. But how long does this final rotation
  80. take?
  81. To compute the rotational delay for 18, first figure out how long the disk
  82. would take to rotate from the end of the access to sector 10 to the beginning
  83. of the access to sector 18, assuming a zero-cost seek. As you can see from the
  84. simulator, sector 10 on the outer track is lined up with sector 22 on the middle
  85. track, and there are 7 sectors separating 22 from 18 (23, 12, 13, 14, 15, 16,
  86. and 17, as the disk spins counter-clockwise). Rotating through 7 sectors takes
  87. 210 time units (30 per sector). However, the first part of this rotation is
  88. actually spent seeking to the middle track, for 40 time units. Thus, the
  89. actual rotational delay for accessing sector 18 is 210 minus 40, or 170 time
  90. units. Run the simulator to see this for yourself; note that you can run
  91. without graphics and with the "-c" flag to just see the results without
  92. seeing the graphics.
  93. prompt> ./disk.py -a 10,18 -c
  94. ...
  95. Sector: 10 Seek: 0 Rotate:105 Transfer: 30 Total: 135
  96. Sector: 18 Seek: 40 Rotate:170 Transfer: 30 Total: 240
  97. TOTALS Seek: 40 Rotate:275 Transfer: 60 Total: 375
  98. You should now have a basic idea of how the simulator works. The questions
  99. below will explore some of the different options, to better help you build a
  100. model of how a disk really works.