exec - Multi pipe in C (childs dont stop reading) -


i trying implement multi pipe in c, run multiple commands shell. have made linked list (called t_launch in code) if type "ls | grep src | wc" :

wc -- pipe -- grep src -- pipe -- ls

every pipe node contain int tab[2] pipe() function (of course, there have been 1 pipe() call each pipe node)

now trying execute these commands :

int     execute_launch_list(t_shell *shell, t_launch *launchs) {    pid_t pid;    int   status;    int   firstpid;     firstpid = 0;    while (launchs != null)    {       if ((pid = fork()) == -1)          return (my_error("unable fork\n"));       if (pid == 0)       {          if (launchs->prev != null)          {             close(1);             dup2(launchs->prev->pipefd[1], 1);             close(launchs->prev->pipefd[0]);          }          if (launchs->next != null)          {             close(0);             dup2(launchs->next->pipefd[0], 0);             close(launchs->next->pipefd[1]);          }         execve(launchs->cmdpath, launchs->words, shell->environ);       }       else if (firstpid == 0)         firstpid = pid;       launchs = launchs->next == null ? launchs->next : launchs->next->next;    }    waitpid(firstpid, &status, 0);    return (success); } 

but doesn't work : looks commands dont stop reading. example if type "ls | grep src, "src" print grep command, grep continue reading , never stop. if type "ls | grep src | wc", nothing printed. what's wrong code ? thank you.

if understand code correctly, first call pipe in shell process every pipe. proceed fork each process.

while close unused end of each of child's pipes in child process, procedure suffers 2 problems:

  1. every child has every pipe, , doesn't close ones don't belong it

  2. the parent (shell) process has pipes open.

consequently, pipes open, , children don't eofs.

by way, need wait() children, not last one. consider case first child long computation after closing stdout, remember any computation or side-effect after stdout closed, short one, sequenced after sink process terminates since multiprocessing non-deterministic.


Comments