signal can be received in any threads or main program itself. i have created one auxiliary thread from main program. so there is two thread in my program 1. main thread (process itself) 2. Auxiliary Thread. I just want that whenever signal arrived in my auxiliary thread, that should send signal to my main thread (program). i am using pthread_kill(main_threadid, sig) to send signal from signal handler register inside auxiliary thread. but. i observe each time signal send to main thread received to auxiliary child itself and signal handler falls in loop of receiving sending signal.
#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;
}
say I am sending SIGINT to process from terminal using its process id.