Find the answer to your Linux question:
Results 1 to 2 of 2
Hi All, I have created a pthread, and installed a signal handler inside that, same way as we do in main( ) function. The thread's signal handler is a separate ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    34

    Question [SOLVED] Signal handling in pthread

    Hi All,
    I have created a pthread, and installed a signal handler inside that, same way as we do in main( ) function. The thread's signal handler is a separate function. Surprisingly, it is not working, that is the thread's signal handler is not able to catch signals. Here is the code:
    Code:
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdio.h>
    #include <signal.h>
    
    typedef struct data
    {
     char name[10];
     int age;
    }data;
    
    void sig_func(int sig)
    {
     printf("Caught signal: %d\n",sig);
     signal(SIGSEGV,sig_func);
    }
    
    void func(data *p)
    {
     printf("This is from thread function\n");
     signal(SIGSEGV,sig_func); // Register signal handler inside thread
     strcpy(p->name,"Mr. Linux");
     p->age=30;
     sleep(2); // Sleep to catch the signal
    }
    
    int main()
    {
     pthread_t tid;
     pthread_attr_t attr;
     data *ptr;
    
     pthread_attr_init(&attr);
     pthread_create(&tid,&attr,(void*)func,ptr);
     pthread_kill(tid,SIGSEGV);
    
     pthread_join(tid,NULL);
     printf("Name:%s\n",ptr->name);
     printf("Age:%d\n",ptr->age);
    }
    Output:
    Segmentation fault (which means the signal is not caught by handler)

  2. #2
    Just Joined!
    Join Date
    Dec 2010
    Posts
    34

    Thumbs up

    The problem is sorted:
    1) ptr is not initialised, so all the ptr-> parts will crash the program
    2) you are calling pthread_kill() immediately, very likely before the signal handler has been installed, and in a thread (which has unspecified behaviour)
    3) you call printf() from a signal handler, which is not guaranteed to work (see man 7 signal for a list of safe functions)
    Here is the modified code: Thanks to Sam Hocevar, who helped me solve this (User Sam Hocevar - Stack Overflow)
    Code:
    #include <pthread.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdio.h>
    #include <signal.h>
    
    typedef struct data
    {
     char name[10];
     int age;
    }data;
    void sig_func(int sig)
    {
     printf("Caught signal:%d\n",sig);
     signal(SIGSEGV,sig_func);
    }
    
    void func(data *p)
    {
     fprintf(stderr,"This is from thread function\n");
     signal(SIGSEGV,sig_func);
     strcpy(p->name,"Raj");
     p->age=30;
     sleep(2);
    }
    
    int main()
    {
    pthread_t tid;
     pthread_attr_t attr;
     data *ptr,d;
     ptr=&d;
    
     pthread_attr_init(&attr);
     pthread_create(&tid,&attr,(void*)func,ptr);
     sleep(2);
    
     pthread_kill(tid,SIGSEGV);
     pthread_join(tid,NULL);
     printf("Name:%s\n",ptr->name);
     printf("Age:%d\n",ptr->age);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...