Home CS439

CS439: Principles of Computer Systems

Discussion Section Problem Set 2

Due in Section on Friday, February 2, 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. How can fairness and throughput be competing goals for a scheduler? Give an example where a fair scheduler makes bad use of the CPU and an example where a high-throughput scheduler is unfair.

  2. Most round-robin schedulers use a fixed size quantum. Give an argument in favor of and against a small quantum.

  3. When executing system or library calls from multi-threaded code, it is important to know which calls are re-entrant and which are not.
    • What is the meaning of re-entrant?
    • How does a re-entrant call behave differently from a call that is not re-entrant?
    • Why is this an important consideration for multi-threaded code?

  4. In the code below, indicate where each variable is stored (e.g., stack, heap, static data segment), and whether that variable would be shared by the threads in a multi-threaded program. If applicable, indicate where the space that variable points to is allocated.

    int i;     
    char * j;  
    
    void foo(int a){
    
       int b;  
       static float c; 
    
       /*do stuff*/
    }
    
    int main(int argc, char**argv){
    
       int * m;  
       int g;    
       double z; 
    
       j = malloc(MAXBUF*sizeof(char));
    
       createThread(0, foo(), 2);
       createThread(1, foo(), 4);
    
       /*do stuff*/
    }
    
  5. A standardized C language threads programming interface has been developed for UNIX systems, IEEE POSIX 1003.1c. Thread implementations that adhere to this standard are known as POSIX threads, or Pthreads. Using Pthreads, write a C program that creates three new threads and assigns each of them a number. Each thread should print the numbers 1-10, one per line, with its number beginning the line, so that the beginning output from thread 1 would look like the following:
    1: 1
    1: 2
    1: 3
    
    The original thread should wait for the new threads to finish and then print a statement notifying the user that the application is finished.

    Explain the output. Turn in your code, output, and explanation.

    Hint: man 7 pthreads provides an overview of pthreads and should help you get started. Notice that to compile code that uses pthreads you need to include the -pthread flag.