IC221: Systems Programming (SP18)


Home Policy Calendar Units Assignments Resources

HW 8: Process Groups and Pipes

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.)
  • This homework is graded out of 100 points. Point values are associated to each question.

Questions

  1. (5 points) How are process groups and jobs related in the shell?
  2. (7 points) How long with the following shell command run for and why?

    sleep 10 | sleep 20 | sleep 100 | sleep 30 | sleep 1
    
  3. (5 points) Explain the difference between sequential and parallel execution of a command line?
  4. (10 points) For each of the system calls associated with process groupings, provide a brief explanation of each.
    1. setpgrp()
    2. setpgid()
    3. getpgrp()
    4. getpgid()
  5. (10 points) For each of the system calls with arguments, briefly describe the resulting action with respect to the calling process or target process.
    1. getpgid(0)
    2. setpgid(0,0)
    3. setpgid(0,pgid)
    4. setpgid(pid, 0)
  6. (10 points) Consider the following code snippet, what is the output and why? (Hint: why not run it?)

    int main(){
      pid_t cpid;
    
      cpid = fork();
      if(cpid == 0){
    
        setpgrp();
        if( getpid() == getpgrp()){
          printf("C: SAME PGID\n");
        }
        exit(0);
    
      }else if(cpid > 0){
    
        if(getpgid(cpid) == cpid){
          printf("P: SAME PGID\n");
        }else{
          printf("P: NOT SAME PGID\n");
        }
    
        wait();
        exit(0);
      }
    
      exit(1);
    }
    
  7. (12 point) Consider the following code snippet. If we were to run this program in a terminal, will it be properly terminated by Ctrl-c? If so, why? If not, why not?

    int main(){
      pid_t cpid;
    
      cpid = fork();
      if( cpid == 0 ){
        setpgrp();
        while(1);
      }else if( cpid > 0 ){
        wait(NULL);
        exit(0);
      }
      exit(1); //fork failed
    }
    
  8. (12 point) Consider the following code snippet with the open file fight.txt containing the text " Go Navy! Beat Army!" (yes, there are spaces in there). What is the output of this program, and why?

    int main(){
      pid_t cpid;
    
      int fd = open("fight.txt",O_RDONLY);
      char buf[1024];
    
      cpid = fork();
      if( cpid == 0 ){
        read(fd, buf, 10);
        exit(0);
      }else if( cpid > 0 ){
        wait(NULL); /* wait for child*/
    
        read(fd,buf, 10);
        write(1, buf, 10);
        exit(0);
      }
      exit(1); //fork failed
    }
    
  9. (5 points) What does it mean to "widow" a pipe?
  10. (12 points) Consider the following code snippet with the open file fight.txt containing the text " Go Navy! Beat Army!" (yes, there are spaces in there). What is the output of this program, and why?

    int main(){
      int fd_in = open("fight.txt",O_RDONLY);
    
      int fd_out = open("output.txt",O_WRONLY | O_TRUNC | O_CREAT,0755);
      char buf[1024];
    
      close(0);
      dup2(fd_in,0);
    
      close(1);
      dup2(fd_out,1);
    
      while(scanf("%s",buf) != EOF){
        printf("%s\n",buf);
      }
    
      return 0;
    }
    
  11. (12 points) What is the missing code in the program below such that the child's write to stdout will be ready by the parent through its stdin?

    int main(){
      pid_t cpid;
      int pfd[2], n;
      char gonavy[] = "Go Navy!";
      char buffer[1024];
    
      pipe(pfd);
    
      cpid = fork();
      if( cpid == 0 ){
    
        /* What goes here? */
    
        write(1, gonavy,strlen(gonavy));
      }else if( cpid > 0 ){
    
        /* What goes here? */
    
    
        n = read(0, buffer, 1024);
        write(1,buffer,n);
      }
    
      exit(1);
    }