I’ve run into some problems while trying to write a smallshell in c.
The problem is the following: Assume I have written some code for a signal-handler that, in this case, is modified to catch SIGCHLD signals, how could I notify my program that a signal has been caught?
The problem is easy if I were to use a global variable, but that’s not really the way I want to go about it. So any suggestions/hints would be much appreciated!
This is how I solve it right now.
volatile sig_atomic_t exit_status; /* <--global variabel */
void sigchld_handler(int signal) {
switch (signal) {
case SIGCHLD:
exit_status = 1; /* SIGCHLD was caught, notify program.. */
break;
default:
fprintf(stderr, "Some signal catched\n"); /* not a signal of intrest */
break;
}
}
//Thanks
A standard solution is to use the unix self-pipe trick. The benefit is that the read end of the pipe can be used with
select()orepoll()thus integrating nicely with event loops without having to periodically poll the value of an atomic variable.