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

162 rivejä
5.8 KiB

9 vuotta sitten
  1. This program, malloc.py, allows you to see how a simple memory allocator
  2. works. Here are the options that you have at your disposal:
  3. -h, --help show this help message and exit
  4. -s SEED, --seed=SEED the random seed
  5. -S HEAPSIZE, --size=HEAPSIZE
  6. size of the heap
  7. -b BASEADDR, --baseAddr=BASEADDR
  8. base address of heap
  9. -H HEADERSIZE, --headerSize=HEADERSIZE
  10. size of the header
  11. -a ALIGNMENT, --alignment=ALIGNMENT
  12. align allocated units to size; -1->no align
  13. -p POLICY, --policy=POLICY
  14. list search (BEST, WORST, FIRST)
  15. -l ORDER, --listOrder=ORDER
  16. list order (ADDRSORT, SIZESORT+, SIZESORT-, INSERT-FRONT, INSERT-BACK)
  17. -C, --coalesce coalesce the free list?
  18. -n OPSNUM, --numOps=OPSNUM
  19. number of random ops to generate
  20. -r OPSRANGE, --range=OPSRANGE
  21. max alloc size
  22. -P OPSPALLOC, --percentAlloc=OPSPALLOC
  23. percent of ops that are allocs
  24. -A OPSLIST, --allocList=OPSLIST
  25. instead of random, list of ops (+10,-0,etc)
  26. -c, --compute compute answers for me
  27. One way to use it is to have the program generate some random allocation/free
  28. operations and for you to see if you can figure out what the free list would
  29. look like, as well as the success or failure of each operation.
  30. Here is a simple example:
  31. prompt> ./malloc.py -S 100 -b 1000 -H 4 -a 4 -l ADDRSORT -p BEST -n 5
  32. ptr[0] = Alloc(3) returned ?
  33. List?
  34. Free(ptr[0]) returned ?
  35. List?
  36. ptr[1] = Alloc(5) returned ?
  37. List?
  38. Free(ptr[1]) returned ?
  39. List?
  40. ptr[2] = Alloc(8) returned ?
  41. List?
  42. In this example, we specify a heap of size 100 bytes (-S 100), starting at
  43. address 1000 (-b 1000). We specify an additional 4 bytes of header per
  44. allocated block (-H 4), and make sure each allocated space rounds up to the
  45. nearest 4-byte free chunk in size (-a 4). We specify that the free list be
  46. kept ordered by address (increasing). Finally, we specify a "best fit"
  47. free-list searching policy (-p BEST), and ask for 5 random operations to be
  48. generated (-n 5). The results of running this are above; your job is to figure
  49. out what each allocation/free operation returns, as well as the state of the
  50. free list after each operation.
  51. Here we look at the results by using the -c option.
  52. prompt> ./malloc.py -S 100 -b 1000 -H 4 -a 4 -l ADDRSORT -p BEST -n 5 -c
  53. ptr[0] = Alloc(3) returned 1004 (searched 1 elements)
  54. Free List [ Size 1 ]: [ addr:1008 sz:92 ]
  55. Free(ptr[0]) returned 0
  56. Free List [ Size 2 ]: [ addr:1000 sz:8 ] [ addr:1008 sz:92 ]
  57. ptr[1] = Alloc(5) returned 1012 (searched 2 elements)
  58. Free List [ Size 2 ]: [ addr:1000 sz:8 ] [ addr:1020 sz:80 ]
  59. Free(ptr[1]) returned 0
  60. Free List [ Size 3 ]: [ addr:1000 sz:8 ] [ addr:1008 sz:12 ] [ addr:1020 sz:80 ]
  61. ptr[2] = Alloc(8) returned 1012 (searched 3 elements)
  62. Free List [ Size 2 ]: [ addr:1000 sz:8 ] [ addr:1020 sz:80 ]
  63. As you can see, the first allocation operation (an allocation) returns the
  64. following information:
  65. ptr[0] = Alloc(3) returned 1004 (searched 1 elements)
  66. Free List [ Size 1 ]: [ addr:1008 sz:92 ]
  67. Because the initial state of the free list is just one large element, it is
  68. easy to guess that the Alloc(3) request will succeed. Further, it will just
  69. return the first chunk of memory and make the remainder into a free list. The
  70. pointer returned will be just beyond the header (address:1004), and the
  71. allocated space is rounded up to 4 bytes, leaving the free list with 92 bytes
  72. starting at 1008.
  73. The next operation is a Free, of "ptr[0]" which is what stores the results of
  74. the previous allocation request. As you can expect, this free will succeed
  75. (thus returning "0"), and the free list now looks a little more complicated:
  76. Free(ptr[0]) returned 0
  77. Free List [ Size 2 ]: [ addr:1000 sz:8 ] [ addr:1008 sz:92 ]
  78. Indeed, because we are NOT coalescing the free list, we now have two elements
  79. on it, the first being 8 bytes large and holding the just-returned space, and
  80. the second being the 92-byte chunk.
  81. We can indeed turn on coalescing via the -C flag, and the result is:
  82. prompt> ./malloc.py -S 100 -b 1000 -H 4 -a 4 -l ADDRSORT -p BEST -n 5 -c -C
  83. ptr[0] = Alloc(3) returned 1004 (searched 1 elements)
  84. Free List [ Size 1 ]: [ addr:1008 sz:92 ]
  85. Free(ptr[0]) returned 0
  86. Free List [ Size 1 ]: [ addr:1000 sz:100 ]
  87. ptr[1] = Alloc(5) returned 1004 (searched 1 elements)
  88. Free List [ Size 1 ]: [ addr:1012 sz:88 ]
  89. Free(ptr[1]) returned 0
  90. Free List [ Size 1 ]: [ addr:1000 sz:100 ]
  91. ptr[2] = Alloc(8) returned 1004 (searched 1 elements)
  92. Free List [ Size 1 ]: [ addr:1012 sz:88 ]
  93. You can see that when the Free operations take place, the free list is
  94. coalesced as expected.
  95. There are some other interesting options to explore:
  96. * -p (BEST, WORST, FIRST)
  97. This option lets you use these three different strategies to look for a
  98. chunk of memory to use during an allocation request
  99. * -l (ADDRSORT, SIZESORT+, SIZESORT-, INSERT-FRONT, INSERT-BACK)
  100. This option lets you keep the free list in a particular order,
  101. say sorted by address of the free chunk, size of free chunk (either
  102. increasing with a + or decreasing with a -), or simply returning free
  103. chunks to the front (INSERT-FRONT) or back (INSERT-BACK) of the free list.
  104. * -A (list of ops)
  105. This option lets you specify an exact series of requests instead
  106. of randomly-generated ones.
  107. For example, running with the flag "-A +10,+10,+10,-0,-2" will allocate
  108. three chunks of size 10 bytes (plus header), and then free the first one
  109. ("-0") and then free the third one ("-2"). What will the free list look
  110. like then?
  111. Those are the basics. Use the questions from the book chapter to explore more,
  112. or create new and interesting questions yourself to better understand how
  113. allocators function.