In my program, I expected thread A to periodically send SIGUSR1
to thread B. In thread B, it will block at sigwait
. As for what to do when SIGUSR1
is received, I am not defining it yet. Below is my code. However, the program terminates at once, and the output is User defined signal 1
.
void usr1_handler();
int main(int argc, char const *argv[])
{
pthread_t tid;
pthread_attr_t attr_obj;
void *thread(void *);
pthread_attr_init(&attr_obj);
pthread_attr_setdetachstate(&attr_obj, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr_obj, thread, (void *)NULL);
while(1)
{
int ret = pthread_kill(tid, SIGUSR1);
sleep(5);
}
return 0;
}
void *thread(void *dummy)
{
int sig;
sigset_t sigmask;
struct sigaction action;
/* set up signal mask to block all in main thread */
sigfillset(&sigmask);
pthread_sigmask(SIG_BLOCK, &sigmask, (sigset_t *)0);
for (;;)
{
int err = sigwait(&sigmask, &sig);
/* define what to do with sig here */
printf("sig is %d\n", sig);
}
pthread_exit((void *)NULL);
}