Hi,
I have a program in which I'm spawning of 3 child processes. The main associates a signal handler (Sigterm and Sigint) and than spawns 3 threads. After this main calls pthread_join to wait for 3 child processes to get over. In the signal handler method, in case the signal is received by one of the threads, I'm re-sending the signal to the main thread. In case it is received by the main thread, I'm not doing anything.
This program works fine on Solaris and program terminates gracefully after sending maximum of 4 signals. But when I run it on Linux, the signal is always received by the main thread. So the program never terminates, even if I send the signal any number of time. How can I rectify the problem? Here is the program.
--------------------------
----------
----------
----------
----------
--
#include<iostream.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#define NTHREADS 4
pthread_t worker[NTHREADS];
pthread_t pid;
void smServerSignalHandler(int sig_num) {
cout<<"Got signal "<<sig_num<<" in the handler"<<endl;
struct sigaction arg;
arg.sa_handler=smServerSig
nalHandler
;
sigaction(SIGTERM, &arg, NULL);
if (pthread_self() == pid)
{
cout<<" pthread_self() == pid xxxxxxxxxxxx Current id"<<endl;
}
else
{
pthread_kill(SIGTERM,pid);
cout<<"pthread_self() != pid xxxxxxxxxxxxx NOT Current id"<<endl;
}
}
//The function hello is being called on the creation of all the 3 threads.
void *hello(void *arg) {
int myid=*(int*)arg;
int *status1;
pause();
return (void*)myid;
}
int main(int argc, char *argv[]) {
int counter[NTHREADS];
int *status;
pid=pthread_self();
struct sigaction arg;
arg.sa_handler=smServerSig
nalHandler
;
sigaction(SIGTERM, &arg, NULL);
if (pthread_self() == pid)
for(int k=0;k<NTHREADS;k++) {
int errorcode;
if ((errorcode=pthread_create
(&worker[k
],NULL,hel
lo,(void*)
&k)) > 0)
printf("The error code is %d \n",errorcode);
}
for(int l=0;l<NTHREADS;l++) {
//Now the main thread is going to join all the threads.
pthread_join(worker[l],(vo
id**)&stat
us);
}
}
--------------------------
----------
----------
----------
----------
-----
Regards,
Pankaj
Start Free Trial