Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with SIGALRM

Status
Not open for further replies.

goodtalka

Programmer
Jun 11, 2002
12
IN
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<<&quot;\nConnect_alarm .... .. Time out connect ...............\n&quot;;
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<<&quot;\nError setting alarm.\n&quot;;

if((n=::connect(sockfd,remAddr,len))<0)
{
close(sockfd);
return -1;
}
// pause(); for testing offline!!
alarm(0);
signal(SIGALRM,sigf);

return(n);
}

//////////////////////////////////////////


When I run the program it works fine for some time and then there will be no timeout at all.Sometimes it display &quot;Alarm Clock&quot; 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!!
 
alarm() is used to generate the SIGALRM signal after specified seconds, if the connect call succeeds then the alarm is reset, else it generates the alarm and returns.
 
Sorry, I didn't notice the initial call to alarm on first reading, so I was wondering why you were only calling alarm(0) after the connect.

I think you need to reset alarm regardless of whether or not connect fails. In other words, your connect call may not have been interrupted by alarm when it returns, resulting in the alarm still being active.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top