'how to reap some child processes (but not all) with waitpid function
I am working on the task of writing myshell on systemprogramming class. I have a question because I am having a hard time writing Sigchld_handler that handles multiple processes through pipes(eg. ls | grep .c)
This question is based on my understanding as follows.
- Signal is not queued.
- However, the list of terminated children is queued to the parent process.
At first, I wrote sigchld handler of myshell as follows.
reaped_id[reap_count] = Waitpid(pid[reap_count], NULL, 0);
The problem with the initial Sigchld_handler is that because the Sigchld signal is not queued, when there are multiple terminated child processes, they fail to reap the second process and a zombie process is created.
So, the textbook says to write the program as below when reaping the child processes with sigchld_handler.
while((pid=waitpid(-1, NULL,0)) > 0){
Sigprocmask(SIG_BLOCK, &mask_all, &prev_all);
deletejob(pid);
Sigprocmask(SIG_BLOCK, &prev_all, NULL);
}
But I think the problem with this code is that the parents are blocked until all the child processes are terminated. (Because waitpid returns -1 when the child process no longer exists)
My myshell's Sigchld_handler wants all terminated child processes(not all child processes) to be reaped, but both the running child process and the parent process(myshell) should be executed concurrently. I wish Parent process doesn't wait until the whole child process is over.
How should I write sigchld_handler?
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
