Home CS439

CS439: Principles of Computer Systems

Homework 3, Part 1

Due: 9:45a Thursday, September 17, 2015

Part 1 of the homeworks must be submitted electronically. Please refer to the homework turnin instructions.

  1. Consider a system using a multilevel feedback queue scheduler. Its scheduler is configured to have four queues, which are, in order of highest priority to lowest priority: Q1, Q2, Q3, and Q4. The queues have quantums sized 5s, 10s, 20s, and 40s, respectively.

    For each of the following three processes, determine which queue it is in when it begins its final quantum.

    • Process A, runtime 40s, blocks every 2s.
    • Process B, runtime 32s, never blocks.
    • Process C, runtime 70s, blocks at 7s, 11a, 25s, 42s, 48s, where those times are points in its execution.
  2. 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*/
  3. 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.

  4. What does the function pthread_detach() do? What default behavior of threads does it alter? When might it be useful?
  5. 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?