Home CS439

CS439: Principles of Computer Systems

Homework 4, Part 1

Due in Section on Friday, February 19, 2016

Part 1 of the homeworks must be completed before section and brought to section. Please refer to the homework turnin instructions.

  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. 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. Describe the priority inversion problem and give an example situation where it may occur.

  4. Consider the following program fragment:
    if(a > 0)
    if(b < 0 && a <= 0)
    else if(b >= 0 && a > 0)

    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.