public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Different behaviour of program
@ 2003-04-01  4:48 Ajay Bansal
  0 siblings, 0 replies; only message in thread
From: Ajay Bansal @ 2003-04-01  4:48 UTC (permalink / raw)
  To: gcc-help

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);
	}	
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-04-01  4:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-01  4:48 Different behaviour of program Ajay Bansal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).