我正在连续循环发送来自终端的100信号(SIGINT),因为我有这个信号的寄存器信号处理程序,所以任何信号都来自辅助线程或主线程(因为信号处理由所有线程共享) )每次发送信号时都应该输出“catch signal:...”消息作为输出。但我观察到100,一些随机少,没有时间说95,84次是这个消息打印。有人可以解释为什么所有100个信号都没有打印,以及如何使用以下代码打印所有100个信号。
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
// Signal Handler for entire Process (shared by all threads)
void signalHandler(int param)
{
printf("Caught signal: %d ", param);
}
void *childFun(void *arg)
{
// Register signal Handler here
struct sigaction childpsa;
childpsa.sa_handler = signalHandler;
sigaction(SIGTERM, &childpsa, NULL);
sigaction(SIGHUP, &childpsa, NULL);
sigaction(SIGINT, &childpsa, NULL);
sigaction(SIGCONT, &childpsa, NULL);
sigaction(SIGTSTP, &childpsa, NULL);
while (1)
{
// doSomething in while loop
}
}
int main(void)
{
// create a auxiliary thread here
pthread_t child_threadId;
int err = pthread_create(&child_threadId, NULL, &childFun, NULL);
while (1)
{
// main program do something
}
return 1;
}
Note: I can register signal handler in main thread also, behavior is same as
signal disposition is shared by all threads.