This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Different behaviour of program
- From: "Ajay Bansal" <Ajay_Bansal at infosys dot com>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Tue, 1 Apr 2003 10:20:14 +0530
- Subject: Different behaviour of program
Hi All
Following program behaves differently on Solaris and Linux if we pass
SIGTERM (kill -15) or ^C (kill -2) to running program. I am using Sun
Workshop compiler on Solaris and gcc on Linux.
But I am not able to understand why!!!!!!!!!!!!!!!!!
TIA
Ajay
-------------------------------------------------------
/* This program illustrates pthread_kill( ). It can be used to send any
signal
to any thread from any other thread.If not caught, it will cause that
thread as well
as whole process to terminate. */
#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;
if (pthread_self() == pid)
{
cout<<" pthread_self() == pid xxxxxxxxxxxx Current
id"<<endl;
}
else
{
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 sigact;
memset(&sigact, 0, sizeof(struct sigaction));
//Block SIGTERM when in the signal handler
sigemptyset(&sigact.sa_mask);
sigaddset (&sigact.sa_mask,SIGTERM);
//Block SIGINT when in the signal handler
sigaddset (&sigact.sa_mask,SIGINT);
//Set the handler
sigact.sa_handler = smServerSignalHandler;
//Transparently restart the slow system calls
sigact.sa_flags |= SA_RESTART;
//Install the handler for SIGTERM
sigaction(SIGTERM, &sigact, NULL);
//Install the handler for SIGINT
sigaction(SIGINT, &sigact, NULL);
for(int k=0;k<NTHREADS;k++) {
int errorcode;
if
((errorcode=pthread_create(&worker[k],NULL,hello,(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],(void**)&status);
}
}