Accept() 和 SIGUSR1 被意外触发

发布于 2025-01-06 23:35:43 字数 1458 浏览 2 评论 0原文

您好,我正在构建一个使用如下所示的信号处理程序的程序...

struct sigaction pipeIn;
pipeIn.sa_handler = updateServer;
sigemptyset(&pipeIn.sa_mask);
pipeIn.sa_flags = SA_ONESHOT;

if(sigaction(SIGUSR1, &pipeIn, NULL) == -1){

    printf("We have a problem, sigaction is not working.\n");
    perror("\n");
    exit(1);    

}

问题是该处理程序在不应该的情况下被触发。唯一应该发送 SIGUSR1 信号的是我的子进程,它存在于一个无限 while 循环中,用于侦听传入连接。子进程被分叉,如下所示。我重做 pipelineIn 处理程序以运行子进程使用的不同函数,而父进程则不使用该函数。代码如下所示。

while(1){

    newSock = accept(listenSock,(struct sockaddr *)&their_addr,&addr_size);
    printf("A\n");
    if(!fork()){
    // We want to redefine the interrupt
        pid_t th;
        th = getpid();
        printf("child pid: %d\n",th);
        pipeIn.sa_handler = setFlag;
        if(sigaction(SIGUSR1, &pipeIn, NULL) == -1){

            printf("We have a problem, sigaction is not working.\n");
            perror("\n");
            exit(1);    

        }

        close(listenSock);
        kill(getppid(),SIGUSR1);
        waitForP(); 
        }*/
        close(newSock);         
        exit(0);
    }
    close(newSock);
    //waitForP();
    //break;
}

当我运行此代码时,我将从另一台计算机进行调用以连接到您在此处看到的服务器程序。它会accept()来自该计算机的一个请求,但是子进程最终会向父进程发送SIGUSR1。然而,父进程在子进程发送信号之前就收到了 SIGUSR1 信号。处理程序在函数应该之前触发函数...然后子进程最终杀死信号并且处理程序第二次关闭。最后,即使没有产生新的连接并且传入的 IP 地址来自随机的奇怪 ipv6 地址,accept() 函数也会再次关闭。我不知道发生了什么事。任何帮助都会很棒。

Hi I'm building a program that uses a signal handler shown below ...

struct sigaction pipeIn;
pipeIn.sa_handler = updateServer;
sigemptyset(&pipeIn.sa_mask);
pipeIn.sa_flags = SA_ONESHOT;

if(sigaction(SIGUSR1, &pipeIn, NULL) == -1){

    printf("We have a problem, sigaction is not working.\n");
    perror("\n");
    exit(1);    

}

The problem is that this handler is getting tripped when it's not supposed to. The only thing that should send the SIGUSR1 signal is my child process which exists inside an infinite while loop which listens for incoming connections. The child process is forked as you can see below. I redo the pipeIn handler to run a different function that the child process uses which the parent does not. The code is shown below.

while(1){

    newSock = accept(listenSock,(struct sockaddr *)&their_addr,&addr_size);
    printf("A\n");
    if(!fork()){
    // We want to redefine the interrupt
        pid_t th;
        th = getpid();
        printf("child pid: %d\n",th);
        pipeIn.sa_handler = setFlag;
        if(sigaction(SIGUSR1, &pipeIn, NULL) == -1){

            printf("We have a problem, sigaction is not working.\n");
            perror("\n");
            exit(1);    

        }

        close(listenSock);
        kill(getppid(),SIGUSR1);
        waitForP(); 
        }*/
        close(newSock);         
        exit(0);
    }
    close(newSock);
    //waitForP();
    //break;
}

When I run this code, I will make a call from another computer to connect to my server program that you see here. It will accept() the one request from that computer just fine, but then the child process will eventually get around to sending SIGUSR1 to the parent process. However the parent process receives the SIGUSR1 signal before the child process even gets to send the signal. The handler trips the function before it should ... then the child process finally gets to kill the signal and the handler goes off the 2nd time. Lastly the accept() function goes off again even if there are no new connections being produced and the incoming ip address is from a weird ipv6 address that is random. I don't know what's going on. Any help would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2025-01-13 23:35:43

跟我重复一遍:始终检查 返回的错误系统调用accept(2) 是 - 你得到的是 -1 而不是套接字描述符,EINTR errno 和未定义的连接地址)。

Repeat after me: always check for error returned from a system call (which accept(2) is - you are getting -1 instead of a socket descriptor, EINTR in errno and undefined connecting address).

世界等同你 2025-01-13 23:35:43

这似乎是显而易见的,但是您是否在编译代码时打开了完整的警告并确保没有任何警告?神秘的行为通常是由错误引起的,如果你询问的话,这些错误只有 C 编译器才会提到......

This might seem obvious, but have you compiled your code with full warnings on and assured you don't have any? Mysterious behavior is often caused by errors that are only mentioned by the C compiler if you ask it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文