Homework 2

22C:116, Fall 1999

Due Friday Sept 3, 1999, in class

Douglas W. Jones
Several of the problems in this assignment refer to the thread manager given in http://homepage.cs.uiowa.edu/~dwjones/opsys/threads/ Please ignore the code for thread_manager_init() and thread_laungh(). This is some of the hardest code I've ever written! If you must read it, do all your homework first, then go back and read it!

  1. Background Inspect the thread data structure thread and the implementation of the thread_relinquish operation. Compare this with the representation of a process state suggested in the clock interrupt service routine pseudocode given in section 11 of Lecture 3.

    Part A: Disregarding questions of implementation, what is the difference between the abstract operations of a process relinquishing the CPU and a process being preempted? Hint: The definitions of the keywords relinquish and preempt are relevant, and the pseudocode and code referenced above illustrate the difference between these two words!

    Part B: From what you know about context switching, and from the referenced part of some detail, what information must be included in the jump buffer used by longjmp(), and what information that the definition of longjmp() says may not be well defined must actually be saved and restored?

  2. Background: Consider the following two UNIX programs:
    main()
    {
       int i;
       i = 0;
       if (fork()) {
           int j;
           for (j = 0;j < 1000; j++) { i++; }
           wait();
       } else {
           int j;
           for (j = 0;j < 1000; j++) { i--; }
           exit();
       }
       printf( "%d\n", i );
    }
    
    main()
    {
       int i;
       jmp_buf b;
       i = 0;
       if (setjmp(b)) {
           int j;
           for (j = 0;j < 1000; j++) { i++; }
       } else {
           int j;
           for (j = 0;j < 1000; j++) { i--; }
           longjmp(b,1);
       }
       printf( "%d\n", i );
    }
    
    Part A: Both the then and else clauses in both of these programs get executed. Explain why. It happens for different reasons with fork() and the setjmp()!

    Part B: Why is the output of these two programs different?

  3. The Problem When two communicating processes (both software) are in a producer-consumer relationship, the producer signals a data-ready semaphore each time it makes data available to the consumer. When the producer is hardware and the consumer is software (for example, when the producer is an external input device and the consumer is an application program), how does the producer signal the consumer that data is ready.