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
- (5 points) How are process groups and jobs related in the shell?
(7 points) How long with the following shell command run for and why?
sleep 10 | sleep 20 | sleep 100 | sleep 30 | sleep 1
- (5 points) Explain the difference between sequential and parallel execution of a command line?
- (10 points) For each of the system calls associated with process groupings,
provide a brief explanation of each.
setpgrp()
setpgid()
getpgrp()
getpgid()
- (10 points) For each of the system calls with arguments, briefly describe the
resulting action with respect to the calling process or target
process.
getpgid(0)
setpgid(0,0)
setpgid(0,pgid)
setpgid(pid, 0)
(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); }
(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 }
(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 }
- (5 points) What does it mean to "widow" a pipe?
(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; }
(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 itsstdin
?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); }