c++ - Child process receives parent's SIGINT -
i have 1 simple program that's using qt framework. uses qprocess execute rar , compress files. in program catching sigint , doing in code when occures:
signal(sigint, &unix_handler);
when sigint occures, check if rar process done, , if isn't wait ... problem (i think) rar process get's sigint meant program , quits before has compressed files.
is there way run rar process doesn't receive sigint when program receives ?
thanks
if generating sigint
ctrl-c on unix system, signal being sent entire process group.
you need use setpgid or setsid put child process different process group not receive signals generated controlling terminal.
[edit]
be sure read rationale section of setpgid
page carefully. little tricky plug of potential race conditions here.
to guarantee 100% no sigint
delivered child process, need this:
#define check(x) if(!(x)) { perror(#x " failed"); abort(); /* or whatever */ } /* block sigint. */ sigset_t mask, omask; sigemptyset(&mask); sigaddset(&mask, sigint); check(sigprocmask(sig_block, &mask, &omask) == 0); /* spawn child. */ pid_t child_pid = fork(); check(child_pid >= 0); if (child_pid == 0) { /* child */ check(setpgid(0, 0) == 0); execl(...); abort(); } /* parent */ if (setpgid(child_pid, child_pid) < 0 && errno != eacces) abort(); /* or whatever */ /* unblock sigint */ check(sigprocmask(sig_setmask, &omask, null) == 0);
strictly speaking, every 1 of these steps necessary. have block signal in case user hits ctrl-c right after call fork
. have call setpgid
in child in case execl
happens before parent has time anything. have call setpgid
in parent in case parent runs , hits ctrl-c before child has time anything.
the sequence above clumsy, handle 100% of race conditions.
Comments
Post a Comment