Home CS439

CS439: Principles of Computer Systems

Homework 4, Part 1

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

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

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

  2. Given the following pseudocode:
    int sharedData;
    void* total(){
    	int i;
            for(i = 0; i < 100; i++)
                    sharedData = sharedData + 1;
    int main(){
            sharedData = 0;
            // start running 2 threads executing total() concurrently and wait for them to finish execution
            pthread_t thread[2];
            int i;
            for(i = 0; i < 2; i++)
                    pthread_create(&thread[i], NULL, total, NULL);
            for(i = 0; i < 2; i++)
                    pthread_join(thread[i], NULL);
            printf("%d\n", sharedData);
            return 0;

    1. On a single processor machine, what are possible outputs of this program?
      Determine both lower bound and upper bound of the output of this program.
      Justify your answer.

    2. Solve the above problem if we run m concurrent threads instead of two threads.

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

  4. Now consider the following program fragment:
    where s1 and s2 are semaphores. All variables are automatic. Now, consider two threads running this fragment of code simultaneously. Can there be a deadlock? Why or why not?

  5. You need two Hydrogen atoms and one Oxygen atom to make water. Using semaphores, write a 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.

  6. Solve the water problem using monitors.