I have some problem with the SIGALRM. I have a multithreaded program in which each thread contains a object which implements a timed tcpconnect function as shown below:
////////////////////////////////////////////
static void connect_alarm(int signo)
{
cout<<"\nConnect_alarm .... .. Time out connect ...............\n";
return;
}
typedef void (*sigfunc)(int);
int TimedConnect(int sockfd,struct sockaddr *remAddr,socklen_t len,int sec)
{
sigfunc sigf;
int n;
sigf = signal(SIGALRM,connect_alarm);
if(alarm(sec)!=0)
cout<<"\nError setting alarm.\n";
if((n=::connect(sockfd,remAddr,len))<0)
{
close(sockfd);
return -1;
}
// pause(); for testing offline!!
alarm(0);
signal(SIGALRM,sigf);
return;
}
//////////////////////////////////////////
When I run the program it works fine for some time and then there will be no timeout at all.Sometimes it display "Alarm Clock" and exits the program.
Here multiple threads are running simultaneously. I even tried it with a single thread, it didn't work. What is the wrong? This is the main part of my program.Please help!!
////////////////////////////////////////////
static void connect_alarm(int signo)
{
cout<<"\nConnect_alarm .... .. Time out connect ...............\n";
return;
}
typedef void (*sigfunc)(int);
int TimedConnect(int sockfd,struct sockaddr *remAddr,socklen_t len,int sec)
{
sigfunc sigf;
int n;
sigf = signal(SIGALRM,connect_alarm);
if(alarm(sec)!=0)
cout<<"\nError setting alarm.\n";
if((n=::connect(sockfd,remAddr,len))<0)
{
close(sockfd);
return -1;
}
// pause(); for testing offline!!
alarm(0);
signal(SIGALRM,sigf);
return;
}
//////////////////////////////////////////
When I run the program it works fine for some time and then there will be no timeout at all.Sometimes it display "Alarm Clock" and exits the program.
Here multiple threads are running simultaneously. I even tried it with a single thread, it didn't work. What is the wrong? This is the main part of my program.Please help!!