0

Question edited after the comment "Oh sry it worked :D". Its embarrassing but I should say pthread_sigmask version of the code too didnt work. Here is the pthread_sigmask version of the code and its output in which SIGCHLD is received before its unblocked!

void signalHandler(int signal_number) {
    if ((debug & 1) == 1) {
        cout << "\n" << getTime() << " signal handler: process " << getpid() << " received signal " << signal_number << "\n";
    }
}

void * test(void*){
     sigset_t chldmask;
    if ((sigemptyset(&chldmask) == -1) || (sigaddset(&chldmask, SIGCHLD) == -1)) {
        perror("Failed to initialize the signal mask");
        return NULL;
    }
    if (pthread_sigmask(SIG_BLOCK, &chldmask, NULL) == -1) {
        return NULL;
    }
    fprintf(stderr, "SIGCHLD signal blocked\n");
    int child = fork();
    if (child == 0) {
        cout << "\nI'm child " << getpid() << " exiting now.\n";
        fflush(stdout);
        exit();
    }
    cout << "\nI'm parent " << getpid() << " not yet exiting.\n";
    cout << "\ngonna sleep for 10 secs\n";
    sleep(10);
    cout << "\nunblocking SIGCHLD after 10 secs\n";
    if (pthread_sigmask(SIG_UNBLOCK, &chldmask, NULL) == -1) {
        return NULL;
    }
    cout << "\nExiting the thread!\n";
    fflush(stdout);
}

int main(){
      struct sigaction signalaction_struct;
      memset(&signalaction_struct, 0, sizeof (signalaction_struct));
      signalaction_struct.sa_handler = &signalHandler;
      sigaction(SIGCHLD, &signalaction_struct, NULL);
      sigaction(SIGUSR1, &signalaction_struct, NULL);
      sigaction(SIGUSR2, &signalaction_struct, NULL);          
      pthread_t test_thread;
      pthread_create(&test_thread,NULL,&test,NULL);
      pthread_join(test_thread,NULL);
      return 0;
}

Output:

SIGCHLD signal blocked

I'm parent 13177 not yet exiting.

gonna sleep for 10 secs

I'm child 13182 exiting now.

2013-11-06 19:47:36 signal handler: process 13177 received signal 17

unblocking SIGCHLD after 10 secs

Exiting the thread!

How can I block the signal from the thread? I got to protect a piece of code in a thread from from SIGCHLD. It seems like Dealing With Asynchronous Signals In Multi Threaded Program answers my question but I didn't get how to arrange things. I'm looking for some suggestions and explaination. Thank you.

Community
  • 1
  • 1
Necktwi
  • 2,483
  • 7
  • 39
  • 62
  • 3
    You shouldn't use `sigprocmask` in a multithreaded program. Use `pthread_sigmask` instead. – Kerrek SB Nov 06 '13 at 11:37
  • oh thanq... I will look into it. – Necktwi Nov 06 '13 at 11:43
  • @KerrekSB In the above code I replaced `sigprocmask` with `pthread_sigmask`, It gave same result; It didn't block the signal. – Necktwi Nov 06 '13 at 12:08
  • @KerrekSB Oh sry it worked :D the signal handler triggered once thread exited. I thought signal handle should never trigger. Thanq. – Necktwi Nov 06 '13 at 12:19
  • @KerrekSB actually `sigprocmask` didn't work. Now I produced the unexpected output after your suggestion. I edited the question appropriately. – Necktwi Nov 06 '13 at 14:39

0 Answers0