信號可以在任何線程中接收到的主線程(主程序)或主要程序本身。 我從主程序創建了一個輔助線程。所以在我的程序中有兩個線程1.主線程(進程本身)2.輔助線程。我只是希望每當信號到達我的輔助線程時,就應該發送信號給我的主線程(程序)。我正在使用pthread_kill(main_threadid,sig)從輔助線程內的信號處理程序寄存器發送信號。但。我觀察到每個時間信號發送到主線程接收到的輔助子本身和信號處理器落在接收發送信號的環路中。pthread_kill()不發送信號到
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
// global variable
pthread_t main_threadId;
struct sigaction childpsa;
// Signal Handler for Auxiliary Thread
void signalHandler_Child(int param)
{
printf("Caught signal: %d in auxiliary Thread", param);
pthread_kill(main_threadId, param);
}
void *childFun(void *arg)
{
childpsa.sa_handler = signalHandler_Child;
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)
{
main_threadId = pthread_self();
fprintf(stderr, "pid to signal %d\n", getpid());
// 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;
}
說我發送SIGINT從終端使用其進程ID處理。
我不太確定你想要做什麼,但使用全局信號,線程間通信可能是不正確的方法。 – thrig