Sample questions for the midterm

  1. Consider the following code:
       if (0==fork()) {
          printf("Child\n");
       }
       printf("Parent\n");
    
    How many times is "Parent" printed? Why?
  2. Why should the TSL instruction be atomic to ensure mutual exclusion?
  3. Consider the following program.
        semaphore mutex=1; /* binary semaphore */
        down(mutex);
        down(mutex);
    
    By the normal definition of binary semaphores, this results in deadlock. But in this case, we want to allow multiple downs by the same process. Define the operations down and up permit multiple locks bt the same process without deadlock.
  4. Describe how message passing can be used to implement semaphores.
  5. What is a monitor? What are the advantages of using monitors?
  6. Is the following code thread-safe? Why?
    // return unique id
    int returnId(void) {
       static int id=-1;
       id++;
       return id;
    }
    
  7. Assume the freelist configuration shown below:
      10K, 4K, 20K, 18K, 7K, 9K, 12K, 15K
    
    For the sequence of requests
       12K, 10K, 9K
    
    What is the allocation under first fit, best fit, worst fit, and next fit?