《操作系统》的实验代码。
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

153 строки
5.4 KiB

  1. #! /usr/bin/env python
  2. import sys
  3. from optparse import OptionParser
  4. import random
  5. parser = OptionParser()
  6. parser.add_option("-s", "--seed", default=0, help="the random seed",
  7. action="store", type="int", dest="seed")
  8. parser.add_option("-j", "--jobs", default=3, help="number of jobs in the system",
  9. action="store", type="int", dest="jobs")
  10. parser.add_option("-l", "--jlist", default="", help="instead of random jobs, provide a comma-separated list of run times",
  11. action="store", type="string", dest="jlist")
  12. parser.add_option("-m", "--maxlen", default=10, help="max length of job",
  13. action="store", type="int", dest="maxlen")
  14. parser.add_option("-p", "--policy", default="FIFO", help="sched policy to use: SJF, FIFO, RR",
  15. action="store", type="string", dest="policy")
  16. parser.add_option("-q", "--quantum", help="length of time slice for RR policy", default=1,
  17. action="store", type="int", dest="quantum")
  18. parser.add_option("-c", help="compute answers for me", action="store_true", default=True, dest="solve")
  19. (options, args) = parser.parse_args()
  20. random.seed(options.seed)
  21. print 'ARG policy', options.policy
  22. if options.jlist == '':
  23. print 'ARG jobs', options.jobs
  24. print 'ARG maxlen', options.maxlen
  25. print 'ARG seed', options.seed
  26. else:
  27. print 'ARG jlist', options.jlist
  28. print ''
  29. print 'Here is the job list, with the run time of each job: '
  30. import operator
  31. joblist = []
  32. if options.jlist == '':
  33. for jobnum in range(0,options.jobs):
  34. runtime = int(options.maxlen * random.random()) + 1
  35. joblist.append([jobnum, runtime])
  36. print ' Job', jobnum, '( length = ' + str(runtime) + ' )'
  37. else:
  38. jobnum = 0
  39. for runtime in options.jlist.split(','):
  40. joblist.append([jobnum, float(runtime)])
  41. jobnum += 1
  42. for job in joblist:
  43. print ' Job', job[0], '( length = ' + str(job[1]) + ' )'
  44. print '\n'
  45. if options.solve == True:
  46. print '** Solutions **\n'
  47. if options.policy == 'SJF':
  48. #YOUR CODE
  49. pass
  50. if options.policy == 'FIFO':
  51. thetime = 0
  52. print 'Execution trace:'
  53. #YOUR CODE
  54. print '\nFinal statistics:'
  55. t = 0.0
  56. count = 0
  57. turnaroundSum = 0.0
  58. waitSum = 0.0
  59. responseSum = 0.0
  60. for tmp in joblist:
  61. jobnum = tmp[0]
  62. runtime = tmp[1]
  63. response = t
  64. turnaround = t + runtime
  65. wait = t
  66. print ' Job %3d -- Response: %3.2f Turnaround %3.2f Wait %3.2f' % (jobnum, response, turnaround, wait)
  67. responseSum += response
  68. turnaroundSum += turnaround
  69. waitSum += wait
  70. t += runtime
  71. count = count + 1
  72. print '\n Average -- Response: %3.2f Turnaround %3.2f Wait %3.2f\n' % (responseSum/count, turnaroundSum/count, waitSum/count)
  73. if options.policy == 'RR':
  74. print 'Execution trace:'
  75. turnaround = {}
  76. response = {}
  77. lastran = {}
  78. wait = {}
  79. quantum = float(options.quantum)
  80. jobcount = len(joblist)
  81. for i in range(0,jobcount):
  82. lastran[i] = 0.0
  83. wait[i] = 0.0
  84. turnaround[i] = 0.0
  85. response[i] = -1
  86. runlist = []
  87. for e in joblist:
  88. runlist.append(e)
  89. thetime = 0.0
  90. while jobcount > 0:
  91. # print '%d jobs remaining' % jobcount
  92. job = runlist.pop(0)
  93. jobnum = job[0]
  94. runtime = float(job[1])
  95. if response[jobnum] == -1:
  96. response[jobnum] = thetime
  97. currwait = thetime - lastran[jobnum]
  98. wait[jobnum] += currwait
  99. ranfor = 0
  100. if runtime > quantum:
  101. #YOUR CODE
  102. print ' [ time %3d ] Run job %3d for %.2f secs' % (thetime, jobnum, ranfor)
  103. runlist.append([jobnum, runtime])
  104. else:
  105. #YOUR CODE
  106. print ' [ time %3d ] Run job %3d for %.2f secs ( DONE at %.2f )' % (thetime, jobnum, ranfor, thetime + ranfor)
  107. turnaround[jobnum] = thetime + ranfor
  108. jobcount -= 1
  109. thetime += ranfor
  110. lastran[jobnum] = thetime
  111. print '\nFinal statistics:'
  112. turnaroundSum = 0.0
  113. waitSum = 0.0
  114. responseSum = 0.0
  115. for i in range(0,len(joblist)):
  116. turnaroundSum += turnaround[i]
  117. responseSum += response[i]
  118. waitSum += wait[i]
  119. print ' Job %3d -- Response: %3.2f Turnaround %3.2f Wait %3.2f' % (i, response[i], turnaround[i], wait[i])
  120. count = len(joblist)
  121. print '\n Average -- Response: %3.2f Turnaround %3.2f Wait %3.2f\n' % (responseSum/count, turnaroundSum/count, waitSum/count)
  122. if options.policy != 'FIFO' and options.policy != 'SJF' and options.policy != 'RR':
  123. print 'Error: Policy', options.policy, 'is not available.'
  124. sys.exit(0)
  125. else:
  126. print 'Compute the turnaround time, response time, and wait time for each job.'
  127. print 'When you are done, run this program again, with the same arguments,'
  128. print 'but with -c, which will thus provide you with the answers. You can use'
  129. print '-s <somenumber> or your own job list (-l 10,15,20 for example)'
  130. print 'to generate different problems for yourself.'
  131. print ''