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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'c' unix signal problem

Status
Not open for further replies.

Gafling

Programmer
Nov 7, 2000
44
US
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 ...
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;
        }
    }
        ...
        ...
}
 
Gafling - there's a C Programming forum here at Tek-Tips that might be able to help you, I just think signals are scary :) sorry.. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top