For the advanced 'c' programmer ... a signal problem ...
I am trying to use the unix alarm & signal functions to control an client
application. The code is essentially documented in the snippet below.
The intent of the program is to send a message and wait 10 seconds for a
reply. If the 'alarm(10)' expires, the program is to try to re-establish
the link before sending any more requests to the server.
However there is a problem. When server is offline or very slow in
responding and the 'alarm(10)' expires on the second time around, the
'sig_alarm' function is not invoked; the program appears to hang on the
read command. Re-stating the above: the program functions correctly when
the alarm(10) expires the first time but appears to hang when the
alarm(10) expires the second time.
Any guidance would be appreciated ...
Code example ...
I am trying to use the unix alarm & signal functions to control an client
application. The code is essentially documented in the snippet below.
The intent of the program is to send a message and wait 10 seconds for a
reply. If the 'alarm(10)' expires, the program is to try to re-establish
the link before sending any more requests to the server.
However there is a problem. When server is offline or very slow in
responding and the 'alarm(10)' expires on the second time around, the
'sig_alarm' function is not invoked; the program appears to hang on the
read command. Re-stating the above: the program functions correctly when
the alarm(10) expires the first time but appears to hang when the
alarm(10) expires the second time.
Any guidance would be appreciated ...
Code example ...
Code:
/* Function to handle alarm signal */
void sig_alarm(int sig) {
signal(SIGALRM, SIG_IGN);
alarm_signal = 1;
return;
}
/* Function read socket data */
int read_data(char *TEXT) {
...
alarm_signal = 0;
alarm(10);
MesgCount = read(clisocket, &IN, ReadCount);
alarm(0);
if (alarm_signal == 1) return(8);
...
return(0);
}
int startup(void) {
/* Allow for clean up if we get killed or we time out */
signal(SIGALRM, sig_alarm);
...
...
/* Go read some data from the socket ... */
i = read_data(&msg.mtext[0]);
if ( i ) {
switch (i) {
case 1: ...
case 2: ...
case 8: ...
signal(SIGALRM, sig_alarm);
break;
}
}
...
...
}