I am doing my project to develop a simple shell. I have a problem with handling signals that I hope you guys can help me.
I am using signal() function to catch the signal sending from the forked children to the shell, which is:
However, this is really confusing. When the user input a command into my shell, normally the shell parse it and call the corresponding programs. However, when I have a pipe like:
ls | cat | cat | cat | cat > a.txt
Then there are multiple files running at once, and the signal handler does not seem to be able to catch multiple signal at one time...
I tried to use signal mask in my handler (in the code snippet).
But this does not seem help... I also applied the race running idea with signal suspend function to delay the moment the child return the signal but also, it does not work because I think at some moment, two child sends two SIGCHLDs at once, and that is really hard to control...
Right now, what I am doing is ask for the children to wait 1 second before sending any signals to the parent, and this works actually. But I don't think this is a smart way to handle signal...
I hope you can help me. Thank you!
sigset_t nmask, omask;
sigprocmask(SIG_BLOCK, &nmask, &omask); //Block all SIGCHLD signals
/* My main code goes here */
sigprocmask(SIG_SETMASK, &omask, NULL); //Unblock SIGCHLD