IC221: Systems Programming (SP18)


Home Policy Calendar Units Assignments Resources

HW 7: Process Exit and Creation

Instructions

  • You must turn in a sheet of paper that is neatly typed or written answering the questions below. (You are strongly encouraged to type your homework.)

    You can use the following tex file to get started with preparing your homework.

  • This homework is graded out of 100 points. Point values are associated to each question.

Questions

  1. (10 points) What is the difference between _exit() and exit() and _Exit()?
  2. (5 points) When a process successfully returns from main(), which of the three different exit calls is actually used? What exit value is typically used for the process when it returns from main() and why?
  3. (5 points) What is the difference between unbuffered, line buffered, and fully buffered with respect to output streams?
  4. (20 points) Consider the following program snippets. What are the outputs of each? Explain your answer!
    1. int main(){
        fprintf(stdout, "Hello World!");
        return 0;
      }
      
    2. int main(){
        fprintf(stdout, "Hello World!");
        exit(0);
      }
      
    3. int main(){
        fprintf(stdout, "Hello World!");
        _Exit(0);
      }
      
    4. int main(){
        fprintf(stderr, "Hello World!");
        _exit(0);
      }
      
  5. (10 point) Why does the following code snippet properly check for a failed call to execv()?

    int main(){
      char * ls_args[2] = { "/bin/ls", NULL} ;
    
      execv( ls_args[0], ls_args);
      perror("execve failed");
    
      exit(1); //failure
    }
    
  6. (10 points) Consider setting up an argv array to be passed to execv() for the execution of following command:

    ls –l –a /bin /usr/bin Fill in
    

    Complete the argv deceleration in code

    char * argv[] = { /* what goes here? */ } ;
    
  7. (5 points) The fork() system call is the only function that returns twice. Explain why this is?
  8. (5 points) If you were to compile and run the following program in the shell, which process'es pid would print to the screen? Explain

    int main(){
      printf("Parent pid: %d\n", getppid());
    }
    
  9. (5 points) The wait() system call will return when a child's status change of a child. What is the most typical status change that would make the system call return?
  10. (15 points) Using the manual page, provide a brief description of each of the status macros below:
    1. WIFEXITED()
    2. WIFEXITSTATUS()
    3. WIFSIGNALED()
  11. (10 points) Assume you were writing a program that checked if a file existed by using ls. (This is a silly way to do this, but just for the sake of argument)

    Recall that ls returns an exit status of 2 when the file does not exist and it cannot list it, and ls returns an exit status of 0 when the file does exist and can be listed. Complete the wait() portion of the program below. The output should be EXISTS! if the file specified in argv[1] exists and DOES NOT EXIST! If the file specified in argv[1] does not exist.

    ( hint: actually try and complete the program on your computer )

    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/wait.h>
    #include <sys/types.h>
    
    int main(int argc, char * argv[]){
      pid_t cid;
      char * ls_args[] = {"ls", NULL, NULL};
      if(argc == 2){
        ls_args[1] = argv[1];
      }
      cid = fork();
      if( cid == 0 ){ /*child*/
        execvp(ls_args[0],ls_args);
        exit(1); /*error*/
      }
    
      /*parent*/
      int status;
      wait(&status);
    
      /* FINISH THIS PROGRA */
    
    }