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

117 lines
3.8 KiB

пре 9 година
  1. #! /usr/bin/env python
  2. import sys
  3. from optparse import OptionParser
  4. import random
  5. import math
  6. def convert(size):
  7. length = len(size)
  8. lastchar = size[length-1]
  9. if (lastchar == 'k') or (lastchar == 'K'):
  10. m = 1024
  11. nsize = int(size[0:length-1]) * m
  12. elif (lastchar == 'm') or (lastchar == 'M'):
  13. m = 1024*1024
  14. nsize = int(size[0:length-1]) * m
  15. elif (lastchar == 'g') or (lastchar == 'G'):
  16. m = 1024*1024*1024
  17. nsize = int(size[0:length-1]) * m
  18. else:
  19. nsize = int(size)
  20. return nsize
  21. #
  22. # main program
  23. #
  24. parser = OptionParser()
  25. parser.add_option('-s', '--seed', default=0, help='the random seed', action='store', type='int', dest='seed')
  26. parser.add_option('-a', '--asize', default='1k', help='address space size (e.g., 16, 64k, 32m, 1g)', action='store', type='string', dest='asize')
  27. parser.add_option('-p', '--physmem', default='16k', help='physical memory size (e.g., 16, 64k, 32m, 1g)', action='store', type='string', dest='psize')
  28. parser.add_option('-n', '--addresses', default=5, help='number of virtual addresses to generate', action='store', type='int', dest='num')
  29. parser.add_option('-b', '--b', default='-1', help='value of base register', action='store', type='string', dest='base')
  30. parser.add_option('-l', '--l', default='-1', help='value of limit register', action='store', type='string', dest='limit')
  31. parser.add_option('-c', '--compute', default=False, help='compute answers for me', action='store_true', dest='solve')
  32. (options, args) = parser.parse_args()
  33. print ''
  34. print 'ARG seed', options.seed
  35. print 'ARG address space size', options.asize
  36. print 'ARG phys mem size', options.psize
  37. print ''
  38. random.seed(options.seed)
  39. asize = convert(options.asize)
  40. psize = convert(options.psize)
  41. if psize <= 1:
  42. print 'Error: must specify a non-zero physical memory size.'
  43. exit(1)
  44. if asize == 0:
  45. print 'Error: must specify a non-zero address-space size.'
  46. exit(1)
  47. if psize <= asize:
  48. print 'Error: physical memory size must be GREATER than address space size (for this simulation)'
  49. exit(1)
  50. #
  51. # need to generate base, bounds for segment registers
  52. #
  53. limit = convert(options.limit)
  54. base = convert(options.base)
  55. if limit == -1:
  56. limit = int(asize/4.0 + (asize/4.0 * random.random()))
  57. # now have to find room for them
  58. if base == -1:
  59. done = 0
  60. while done == 0:
  61. base = int(psize * random.random())
  62. if (base + limit) < psize:
  63. done = 1
  64. print 'Base-and-Bounds register information:'
  65. print ''
  66. print ' Base : 0x%08x (decimal %d)' % (base, base)
  67. print ' Limit : %d' % (limit)
  68. print ''
  69. if base + limit > psize:
  70. print 'Error: address space does not fit into physical memory with those base/bounds values.'
  71. print 'Base + Limit:', base + limit, ' Psize:', psize
  72. exit(1)
  73. #
  74. # now, need to generate virtual address trace
  75. #
  76. print 'Virtual Address Trace'
  77. for i in range(0,options.num):
  78. vaddr = int(asize * random.random())
  79. if options.solve == False:
  80. print ' VA %2d: 0x%08x (decimal: %4d) --> PA or segmentation violation?' % (i, vaddr, vaddr)
  81. else:
  82. paddr = 0
  83. if (vaddr >= limit):
  84. print ' VA %2d: 0x%08x (decimal: %4d) --> SEGMENTATION VIOLATION' % (i, vaddr, vaddr)
  85. else:
  86. paddr = vaddr + base
  87. print ' VA %2d: 0x%08x (decimal: %4d) --> VALID: 0x%08x (decimal: %4d)' % (i, vaddr, vaddr, paddr, paddr)
  88. print ''
  89. if options.solve == False:
  90. print 'For each virtual address, either write down the physical address it translates to'
  91. print 'OR write down that it is an out-of-bounds address (a segmentation violation). For'
  92. print 'this problem, you should assume a simple virtual address space of a given size.'
  93. print ''