Reputation: 41
Because I need to update the job status when a SIGCHLD has arrived, how can I know if sig_atomic_t has changed value? code looks like this...
sig_atomic_t child_status;
sig_atomic_t child_pid; //is this ok?
void sigHandler(int signum){
pid_t pid;
int status;
while((pid = wait(-1, &status, WNOHANG) > 0){
child_status = status;
child_pid = (int)pid;
}
}
Upvotes: 0
Views: 250
Reputation: 754190
You don't know how big a sig_atomic_t
is, so you can't necessarily store a pid or a status in it. That is, the C standard says simply:
§7.14 Signal handling
...
The type defined is
sig_atomic_t
which is the (possibly volatile-qualified) integer type of an object that can be accessed as an atomic entity, even in the presence of asynchronous interrupts.
POSIX does not guarantee anything extra, AFAICS. All that said, there's a decent chance that on an n-bit machine, sig_atomic_t
is a n-bit type (but chips with half-width buses, like an 8088, might be more restricted).
You tell whether it is changed the same way as any other variable, by comparing the current value in the variable with the value you think it had last time:
int old_status = child_status;
int old_pid = child_pid;
...busy code...
if (child_status != old_status || child_pid != old_pid)
...something changed...
Upvotes: 1