0

i have program which sends signal SIGCHLD to all other processes in the system TWICE. problem is, its sent only once.

program sending the signal:

 int main(){
 kill(-1, SIGCHLD);   
 sleep(8);
 kill(-1, SIGCHLD);
 return 0;}

program getting the signals:

int main(){
struct sigaction sigchldHandler;
sigchldHandler.sa_flags = 0;
sigset_t BlockMask;
sigfillset(&BlockMask);
sigchldHandler.sa_mask = BlockMask;
sigchldHandler.sa_handler = sigchldFunction;
if( sigaction( SIGCHLD, &sigchldHandler, NULL ) != 0 ){
      perror( "sigaction() error\n" );}
    while(1){
       pause()}
return 0;
}

the function is:

void sigchldFunction(int signal){
write(1, "gets here only once\n", strlen("gets here only once\n"));
startHandling();
}

why doesn't it get the second signal, byt only the first one? ...HELP??

Zohar Argov
  • 143
  • 1
  • 1
  • 11
  • 1
    First I suggest you to learn how to format your code properly. Readability of the code is half of it's success. – Eugene Sh. Jun 08 '16 at 21:53
  • its as short and clear as possible.. – Zohar Argov Jun 08 '16 at 21:57
  • 1
    `short != clear`. Don't save on spaces and newlines. Haven't you seen the other questions with code around here? – Eugene Sh. Jun 08 '16 at 21:57
  • u r right.. net time ill try. anyways, do you see anything here? – Zohar Argov Jun 08 '16 at 22:05
  • 3
    You don't show us the code for `startHandling()` so we can't be sure, but you are presumably inside `startHandling()` with SIGCHLD _blocked_ from your signal handler because [you didn't specify SA_NODEFER](http://stackoverflow.com/a/36069374/132382). Thus the second signal is effectively ignored until you leave the signal handler. – pilcrow Jun 08 '16 at 22:05
  • so if I add the blocks, it should work ? like I editted? – Zohar Argov Jun 08 '16 at 22:07
  • 2
    With signal handlers, you do as little as possible in the signal handler before returning — call as few functions as possible, do as little work as possible, get back as soon as possible. Your process will receive no signals at all until it returns from `sigchldFunction()`. That's what blocking all the signals means. – Jonathan Leffler Jun 08 '16 at 22:57

0 Answers0