c - Is brother-brother pipe safer than father-child? -


in case of establishing pipe between 2 processes, if 2 have brother brother relationship rather father-child, more error prone ?

my question arose when investigated code example below:

#include <stdlib.h> #include <stdio.h> #include <sys/wait.h>  void runpipe();  int main(int argc, char **argv) {     int pid, status;     int fd[2];      pipe(fd);      switch (pid = fork())     {      case 0: /* child */         runpipe(fd);         exit(0);      default: /* parent */         while ((pid = wait(&status)) != -1) {             fprintf(stderr, "process %d exits %d\n", pid, wexitstatus(status));             exit(0);         }      case -1:         perror("fork");         exit(1);     }     exit(0); }  char *cmd1[] = { "ls", "-al", "/", 0 }; char *cmd2[] = { "tr", "a-z", "a-z", 0 };  void runpipe(int pfd[]) {     int pid;      switch (pid = fork())     {      case 0: /* child */         dup2(pfd[0], 0);         close(pfd[1]);  /* child not need end of pipe */         execvp(cmd2[0], cmd2);         perror(cmd2[0]);      default: /* parent */         dup2(pfd[1], 1);         close(pfd[0]);  /* parent not need end of pipe */         execvp(cmd1[0], cmd1);         perror(cmd1[0]);       case -1:         perror("fork");         exit(1);     } } 

in example above, parent(grandpa) forks child(parent), forks child(grandchild). grandpa waits dad dad not wait grandson because both execute execvp. happens if child finishes earlier dad (zombie) or dad finishes earlier child (orphan) ? on other hand if had 2 brothers connected pipe , 1 father , waiting them (total 3 processes), if both brothers executed execvp, ones exit not harm other.

in case of establishing pipe between 2 processes, if 2 have brother brother relationship rather father-child, more error prone ?

as far pipe concerned, depends on i/o operations each performs. if process @ read end tries read data process @ other end not prepared write, block until writer writes or exits. in latter case, read either report error or return short data.

what happens if child finishes earlier dad (zombie) or dad finishes earlier child (orphan) ?

if father calls exec() function after forking child , before collecting via wait() or waitpid(), in example code, unlikely ever wait on child.

regardless, child , dad each become zombies when terminate. true of child whether or not orphaned first. if dad never collects child (as won't in example), once dad terminates, child (whether live or zombie) inherited process 0 (init), can relied upon clean zombie children. similarly, if grandpa never collects dad init do.

under circumstances possible zombies build uncollected. form of resource leak, cleaned when zombies inherited init. exacerbated grandpa -> parent -> child topology you've set up, wouldn't characterize "error prone."


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -