《操作系统》的实验代码。

119 行
3.9 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', action='store', type='int', dest='seed')
  7. parser.add_option('-j', '--jobs', default=3, help='number of jobs in the system', action='store', type='int', dest='jobs')
  8. parser.add_option('-l', '--jlist', default='', help='instead of random jobs, provide a comma-separated list of run times and ticket values (e.g., 10:100,20:100 would have two jobs with run-times of 10 and 20, each with 100 tickets)', action='store', type='string', dest='jlist')
  9. parser.add_option('-m', '--maxlen', default=10, help='max length of job', action='store', type='int', dest='maxlen')
  10. parser.add_option('-T', '--maxticket', default=100, help='maximum ticket value, if randomly assigned', action='store', type='int', dest='maxticket')
  11. parser.add_option('-q', '--quantum', default=1, help='length of time slice', action='store', type='int', dest='quantum')
  12. parser.add_option('-c', '--compute', help='compute answers for me', action='store_true', default=False, dest='solve')
  13. (options, args) = parser.parse_args()
  14. random.seed(options.seed)
  15. print 'ARG jlist', options.jlist
  16. print 'ARG jobs', options.jobs
  17. print 'ARG maxlen', options.maxlen
  18. print 'ARG maxticket', options.maxticket
  19. print 'ARG quantum', options.quantum
  20. print 'ARG seed', options.seed
  21. print ''
  22. print 'Here is the job list, with the run time of each job: '
  23. import operator
  24. tickTotal = 0
  25. runTotal = 0
  26. joblist = []
  27. if options.jlist == '':
  28. for jobnum in range(0,options.jobs):
  29. runtime = int(options.maxlen * random.random())
  30. tickets = int(options.maxticket * random.random())
  31. runTotal += runtime
  32. tickTotal += tickets
  33. joblist.append([jobnum, runtime, tickets])
  34. print ' Job %d ( length = %d, tickets = %d )' % (jobnum, runtime, tickets)
  35. else:
  36. jobnum = 0
  37. for entry in options.jlist.split(','):
  38. (runtime, tickets) = entry.split(':')
  39. joblist.append([jobnum, int(runtime), int(tickets)])
  40. runTotal += int(runtime)
  41. tickTotal += int(tickets)
  42. jobnum += 1
  43. for job in joblist:
  44. print ' Job %d ( length = %d, tickets = %d )' % (job[0], job[1], job[2])
  45. print '\n'
  46. if options.solve == False:
  47. print 'Here is the set of random numbers you will need (at most):'
  48. for i in range(runTotal):
  49. r = int(random.random() * 1000001)
  50. print 'Random', r
  51. if options.solve == True:
  52. print '** Solutions **\n'
  53. jobs = len(joblist)
  54. clock = 0
  55. for i in range(runTotal):
  56. r = int(random.random() * 1000001)
  57. winner = int(r % tickTotal)
  58. current = 0
  59. for (job, runtime, tickets) in joblist:
  60. current += tickets
  61. if current > winner:
  62. (wjob, wrun, wtix) = (job, runtime, tickets)
  63. break
  64. print 'Random', r, '-> Winning ticket %d (of %d) -> Run %d' % (winner, tickTotal, wjob)
  65. # print 'Winning ticket %d (of %d) -> Run %d' % (winner, tickTotal, wjob)
  66. print ' Jobs:',
  67. for (job, runtime, tickets) in joblist:
  68. if wjob == job:
  69. wstr = '*'
  70. else:
  71. wstr = ' '
  72. if runtime > 0:
  73. tstr = tickets
  74. else:
  75. tstr = '---'
  76. print ' (%s job:%d timeleft:%d tix:%s ) ' % (wstr, job, runtime, tstr),
  77. print ''
  78. # now do the accounting
  79. if wrun >= options.quantum:
  80. wrun -= options.quantum
  81. else:
  82. wrun = 0
  83. clock += options.quantum
  84. # job completed!
  85. if wrun == 0:
  86. print '--> JOB %d DONE at time %d' % (wjob, clock)
  87. tickTotal -= wtix
  88. wtix = 0
  89. jobs -= 1
  90. # update job list
  91. joblist[wjob] = (wjob, wrun, wtix)
  92. if jobs == 0:
  93. print ''
  94. break