Home CS439

CS439: Principles of Computer Systems

Discussion Section 3 Problem Set Solutions

Due in Section on Friday, September 19, 2025

The problem set must be completed before section and brought to section. To ensure that your problem set is turned in correctly and that you receive credit for discussion section, you must follow these guidelines exactly.
  1. If you find yourself in a situation where code submitted by you or your group violates the academic integrity policies of this class what are your options?

  2. Compare and contrast user threads and kernel threads.

  3. True/False: Starvation implies deadlock.
    Explain.

  4. Given the following pseudocode:
    int i = 0;
    
    int main(void)
    { 
       fork();
    
       if(/*CHILD*/)
       {
          i = i - 1;
          return SUCCESS;
       }
       if(/*PARENT*/)
       {
          i = i + 1;
          wait for child to terminate;
    
          printf("i = %d\n", i);
       }
    }
    

    • What is the output? Describe in detail why it is that way.
    • Are there other possible outputs? Why or why not? If so, give another example of what the printed values of "i" might be and explain how to force the program to execute the same way each time.
  5. Now consider this pseudocode involving kernel threads:
    int i = 0;
    
    void thread1(void *)
    {
       i = i - 1;
    }
    
    void thread2(void *)
    {
       i = i + 1;
    }
    
    int main(void)
    {
       createThread(thread1);
       createThread(thread2);
    
       /* wait for thread 1 and thread 2 to end */
    
       printf("i = %d\n", i);
    }
    
    • What is the output? Describe in detail why it is that way.
    • Are there other possible outputs? Why or why not? If so, give another example of what the printed values of "i" might be and explain how to force the program to execute the same way each time.

  6. Some number of neighbors are sharing a bike to train for various sporting events. Since each neighbor will train daily and also must rest for his or her big event, they are hoping for an easy way to share the bike that allows only one rider to be on the bike at a time and allows the riders to rest while waiting for the bike. You are a known expert at synchronization problems involving limited resources, and so they have turned to you to devise a solution.

    Write the following function:

       void borrow_bike();
    
    which may be executed by multiple neighbors (threads) at a time, using:
    1. semaphores, and
    2. monitors.

    Keep in mind that when each neighbor will need the bike is unpredictable, and neighbors should be able to rest from the time they request the bike until they acquire it.