Home CS439

CS439: Principles of Computer Systems

Discussion Section 3 Problem Set

Due in Section on Friday, September 20, 2024

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. Given the following piece of code:
    main(int argc, char**argv)
    {
       int child=fork();
       int c=2;
       
       if(child==0)
       {
          c+=2;
       }
       else
       {
          child=fork();
          c+=4;
          if(child==0)
             c+=2;
       }
    }
    
    How many different copies of the variable c are there? What are their values?

  2. Consider a uniprocessor kernel that user programs can trap into using system calls. The kernel receives and handles interrupts from I/O devices. Would there be any need for critical sections within that kernel?

  3. Describe the priority inversion problem and give an example situation where it may occur.

  4. Compare and contrast monitors and semaphores. What is an appropriate use of each?

  5. Deadlock can exist if and only if several conditions hold simultaneously. What are those conditions within a process? Name and describe all of them.

  6. Consider the following program fragment:
       if(a > 0)
          sema_down(s1);
       else
          sema_down(s2);
       
       b++;
       
       sema_down(s3);
       
       if(b < 0 && a <= 0)
          sema_down(s1);
       else if(b >= 0 && a > 0)
          sema_down(s2);
       else
          sema_down(s4);
       
       a++;
       
       sema_up(s4);
       sema_up(s3);
       sema_up(s2);
       sema_up(s1);
       
       

    s1, s2, s3 and s4 are semaphores initialized to 1. All variables are automatic (on the stack). Now, consider two threads running this fragment of code simultaneously, can there be a deadlock? Why, or why not?

  7. 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.