《操作系统》的实验代码。
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

155 řádky
5.6 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=False, 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. joblist = sorted(joblist, key=operator.itemgetter(1))
  49. options.policy = 'FIFO'
  50. if options.policy == 'FIFO':
  51. thetime = 0
  52. print 'Execution trace:'
  53. for job in joblist:
  54. print ' [ time %3d ] Run job %d for %.2f secs ( DONE at %.2f )' % (thetime, job[0], job[1], thetime + job[1])
  55. thetime += job[1]
  56. print '\nFinal statistics:'
  57. t = 0.0
  58. count = 0
  59. turnaroundSum = 0.0
  60. waitSum = 0.0
  61. responseSum = 0.0
  62. for tmp in joblist:
  63. jobnum = tmp[0]
  64. runtime = tmp[1]
  65. response = t
  66. turnaround = t + runtime
  67. wait = t
  68. print ' Job %3d -- Response: %3.2f Turnaround %3.2f Wait %3.2f' % (jobnum, response, turnaround, wait)
  69. responseSum += response
  70. turnaroundSum += turnaround
  71. waitSum += wait
  72. t += runtime
  73. count = count + 1
  74. print '\n Average -- Response: %3.2f Turnaround %3.2f Wait %3.2f\n' % (responseSum/count, turnaroundSum/count, waitSum/count)
  75. if options.policy == 'RR':
  76. print 'Execution trace:'
  77. turnaround = {}
  78. response = {}
  79. lastran = {}
  80. wait = {}
  81. quantum = float(options.quantum)
  82. jobcount = len(joblist)
  83. for i in range(0,jobcount):
  84. lastran[i] = 0.0
  85. wait[i] = 0.0
  86. turnaround[i] = 0.0
  87. response[i] = -1
  88. runlist = []
  89. for e in joblist:
  90. runlist.append(e)
  91. thetime = 0.0
  92. while jobcount > 0:
  93. # print '%d jobs remaining' % jobcount
  94. job = runlist.pop(0)
  95. jobnum = job[0]
  96. runtime = float(job[1])
  97. if response[jobnum] == -1:
  98. response[jobnum] = thetime
  99. currwait = thetime - lastran[jobnum]
  100. wait[jobnum] += currwait
  101. if runtime > quantum:
  102. runtime -= quantum
  103. ranfor = quantum
  104. print ' [ time %3d ] Run job %3d for %.2f secs' % (thetime, jobnum, ranfor)
  105. runlist.append([jobnum, runtime])
  106. else:
  107. ranfor = runtime;
  108. print ' [ time %3d ] Run job %3d for %.2f secs ( DONE at %.2f )' % (thetime, jobnum, ranfor, thetime + ranfor)
  109. turnaround[jobnum] = thetime + ranfor
  110. jobcount -= 1
  111. thetime += ranfor
  112. lastran[jobnum] = thetime
  113. print '\nFinal statistics:'
  114. turnaroundSum = 0.0
  115. waitSum = 0.0
  116. responseSum = 0.0
  117. for i in range(0,len(joblist)):
  118. turnaroundSum += turnaround[i]
  119. responseSum += response[i]
  120. waitSum += wait[i]
  121. print ' Job %3d -- Response: %3.2f Turnaround %3.2f Wait %3.2f' % (i, response[i], turnaround[i], wait[i])
  122. count = len(joblist)
  123. print '\n Average -- Response: %3.2f Turnaround %3.2f Wait %3.2f\n' % (responseSum/count, turnaroundSum/count, waitSum/count)
  124. if options.policy != 'FIFO' and options.policy != 'SJF' and options.policy != 'RR':
  125. print 'Error: Policy', options.policy, 'is not available.'
  126. sys.exit(0)
  127. else:
  128. print 'Compute the turnaround time, response time, and wait time for each job.'
  129. print 'When you are done, run this program again, with the same arguments,'
  130. print 'but with -c, which will thus provide you with the answers. You can use'
  131. print '-s <somenumber> or your own job list (-l 10,15,20 for example)'
  132. print 'to generate different problems for yourself.'
  133. print ''