Home CS439

CS439: Principles of Computer Systems

Discussion Section Problem Set 4

Due in Section on Friday, February 16, 2018

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. What are the three methods of switching from user mode to supervisor mode? When might each occur?

  2. Given the following segment of code:
    char a[100];
    
    thread_func(int r, char ** g)
    {
       int d;
       static double b;
       char * s = "boo";
       char * p;
    
       p = malloc(300);
       return 0;
    }
    
    Identify the memory space in which each variable resides and indicate if the variable is private to a thread or shared amongst threads.

  3. Compare and contrast user threads and kernel threads.

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

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