CSC374 Homework #4
Due Date: Nov 2


  1. Given an input file hello.txt that consists of the string "Hello, world!\n", write a C program that uses mmap to change the contents of hello.txt to "Jello, world!\n".

  2. A free block of size 1024 bytes begins at address 0xC00. The buddy system is used to allocate and free memory from this free block. The smallest size allocated is 64 bytes.

    Example: For a block of size 128 at address 0xE00, its buddy is at 0xE80. In binary:

    	    0xE00 = 1110 0000 0000
                0xE80 = 1110 1000 0000
    	  

    The last 7 bits must be 0 because the size of each of these blocks is 128.

    The buddies differ only in the (7 + 1)-th bit.

    After several allocations, two blocks are to be freed. When a block is freed, its "buddy" is checked to see if it is free and can be coalesced. For each block below, what is the address of its "buddy"?

    1. A block of size 64 at address 0xC40
    2. A block of size 128 at address 0xD00
  3. You are given three groups of statements relating to memory management and garbage collection below. In each group, only one statement is true. Your task is to indicate which statement is true.

      1. In a buddy system, up to 50% of the space can be wasted due to internal fragmentation.

      2. The first-fit memory allocation algorithm is slower than the best-fit algorithm (on average).

      3. Deallocation using boundary tags is fast only when the list of free blocks is ordered according to increasing memory addresses.

      4. The buddy system suffers from internal fragmentation, but not from external fragmentation.

      1. Using the first fit algorithm on a free list that is ordered according to decreasing block sizes results in low performance for allocations, but avoids external fragmentation.

      2. For the best-fit method, the list of free blocks should be ordered according to increasing memory addresses.

      3. The best-fit method chooses the largest free block into which the requested segment fits.

      4. Using the first-fit algorithm on a free list htat is ordered according to increasing block sizes is equivalent to using the best-fit algorithm.

    1. Mark-and-sweep garbage collectors are called conservative if

      1. they coalesce freed memory only when a memory request cannot be statisfied

      2. they treat everything that looks like a pointer as a pointer

      3. they perform garbage collection only when they run out of memory

      4. they do not free memory blocks forming a cyclic list

  4. What is the output of the following problem:

        1	int main()
        2	{
        3	  int fd1, fd2;
        4	
        5	  fd1 = open("foo.txt", O_RDONLY, 0);
        6	  fd2 = open("bar.txt", O_RDONLY, 0);
        7	  close(fd2);
        8	  fd2 = open("baz.txt" O_RDONLY, 0);
        9	  printf("fd2 = %d\n", fd2);
       10	
       11	  return 0;
       12	}