Home CS439

CS439: Principles of Computer Systems

Discussion Section 3 Problem Set

Due in Section on Friday, Febuary 7, 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. Compare and contrast monitors and semaphores. What is an appropriate use of each?

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

  3. 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*/
    }
    

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

  5. You need two Hydrogen atoms and one Oxygen atom to make water. Using semaphores, write a pseudocode solution that generates water whenever the right resources are available. You must have three functions: one to generate Oxygen, one to generate Hydrogen, and one to generate water. Assume the three methods may be executed by any number of threads concurrently. After creating the semaphore solution, create a solution that uses monitors.

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