C Minishell - Need to Kill Zombies for Pipeline Implementation
c, parent-child, shell, unix, wait
Solution
Change this
while(waitpid(-1, NULL, WNOHANG) > 0);
To:
while(wait(NULL) > 0);
/* which is equivalent to */
while(waitpid(-1, NULL, 0) > 0);
This will cause the parent process to wait for all child processes to finish, if you don't wish to block the parent process then catch `SIGCHLD` and call `wait()` in the signal handler instead.
Problem
So I'm building a minishell in C(for unix). I just figured out how to get pipelines to work, however I'm having a Zombie problem. Let's say I have: ``` echo a | echo b | echo c ``` This doesn't output anything, when it should be outputting "c". However, if I tell my shell to execute each sub-command, and then wait before moving on to the next command, it works fine. However this isn't a real solution, as I want that natural coordination between pipes that you get if you don't wait. I'm having trouble devising an efficient way to wait on all the zombies once the last command is executed. I tried doing this after the last execution, but before the shell exits: ``` while(waitpid(-1, NULL, WNOHANG) > 0); ``` However, no luck. So far the only thing that works is by telling my shell to execute each sub-command, and then wait before starting the next command. Here's the entire main shell file: http://pastebin.com/YV96mFy7 The main function that processes input(processline()) starts at line 105. Thanks for the help, if you guys need anything more just ask.