2

下面是重新启动套接字连接服务器的代码,因此当它们受到一些干扰时,它能够重新连接,但在某些时候或情况下它无法重新连接/连接下面代码问题中可能存在的问题

/***   restarting the server by capturing the SIGHUP signal ***/

int isfirst;`enter code here`
char **command_args;

void sig_hangup(int signum)
{
    if ( isfirst )
    {
        isfirst = 0;
        fprintf(stderr, "Parent died\n");
    }`enter code here`
    else                /* Restart! */
    {
        fprintf(stderr, "Restarting...\n");
        /*** Kill all existing child processes ***/
        execv(command_args[0], command_args);
        fprintf(stderr, "Could not restart!!!\n");
        abort();
    }
}

void child(void)
{   struct sigaction act;

    bzero(&act, sizeof(act));
    act.sa_handler = sig_hangup;
    act.sa_flags = SA_RESTART;
/*  if ( sigaction(SIGHUP, &act, 0) != 0 )
        perror("Can't capture SIGHUP");*/
    for (;;)
    {
        fprintf(stderr, "[pid=%d] I'm still here\n", getpid());
        sleep(1);
    }
    exit(0);
}

int main(int count, char *strings[])
{
    isfirst = 1;
    command_args = strings;
    if ( fork() == 0 )
        child();
    sleep(1);
    return 0;
}
4

0 回答 0