//thread block sighup
sigemptyset(&newmask);
sigaddset(&newmask, SIGHUP);
if(pthread_sigmask(SIG_BLOCK, &newmask, &oldmask) < 0){
perror("sigprocmask error");
}
fprintf(stdout, "thread|first while. catch sigusr1 or sigusr2 can break\n");
while(1){
if(flag_sigusr1 || flag_sigusr2){
fprintf(stdout, "thread|break\n");
break;
}
sleep(10);
}
flag_sigusr1 = 0;
//thread block SIGUSR1
sigaddset(&newmask, SIGUSR1);
if(pthread_sigmask(SIG_BLOCK, &newmask, &oldmask) < 0){
perror("sigprocmask error");
}
fprintf(stdout, "thread|first while. catch sigusr2 can break\n");
while(1){
if(flag_sigusr1 || flag_sigusr2){
fprintf(stdout, "break\n");
break;
}
sleep(10);
}
fprintf(stdout, "thread|thread exit\n");
return (void *)0;
}
int main(void){
sigset_t newmask;
pthread_t tid;
int signo;
//signal action
signal(SIGUSR1, sig_usr1);
signal(SIGUSR2, sig_usr2);
signal(SIGHUP , sig_hup);
if(pthread_create(&tid, NULL, thread_control_signal, NULL) < 0){
perror("create pthread failed");
return -1;
}
//main thread block sigusr1
sigemptyset(&newmask);
sigaddset(&newmask, SIGUSR1);
if(pthread_sigmask(SIG_BLOCK, &newmask, NULL) < 0){
perror("sigprocmask error");
}
//main thread wait sighup
sigemptyset(&newmask);
sigaddset(&newmask, SIGHUP);
if(sigwait(&newmask, &signo) < 0){
perror("sigwait failed");
return -1;
}
fprintf(stdout, "main|get SIGHUP\n");
pthread_kill(tid, SIGUSR2);
pthread_kill(tid, SIGUSR2);
pthread_join(tid, NULL);
fprintf(stdout, "main|exit\n");
return 0;
}