CS 367 HW #5
Due April 29 at classtime

  1. In linking, what is symbol resolution? What is symbol relocation?
    see text
  2. Consider the following C program. How many times will each string ("One", "Two", "Three", "Four", "Five") be output?

    fun() {

      printf("One");

      fork();

      printf("Two");

      if (fork() == 0) {

         printf("Three");

         fork();

         printf("Four");

      }

      printf("Five");

    }


                                   |---Four---Five---
                                   |
                       |---Three---|---Four---Five---
                       |
             |---Two---|---Five---
             |                     |---Four---Five---
             |                     |
             |         |---Three---|---Four---Five---
             |         |
    ---One---|---Two---|---Five---
    
  3. Consider the following C program. What are all of the possible outputs of the program?

    #define N 3

    int val = 10;

    void handler(sig) {

       val += 5;

       return;

    }

    int main() {

      pid_t pid;

      int i;

      signal(SIGCHLD,handler);

      for (i=0;i<N;i++) {

        if ((pid ==fork()) == 0) {

            val -= 3;

            exit(0);

        }

      }

      for (i=0;i<N;i++) {

        waitpid(-1,NULL,0);

      }

      printf("val = %d\n",val);

    }


    the handler will be called 1,2 or 3 times so val will equal 15, 20 or 25.

  4. In your text, 8.8 (p. 639)
    fork() - called once, returns twice
    execve() - called once, never returns
    setjmp() - called once, returns many times
    longjmp() - called once, never returns

  5. In your text, 8.14 (p. 641-2)
    A,C,E are all possible outputs