信号处理程序在我的 C http 服务器上不工作

Signal Handler Not working On my C http Server

问题:我希望我的单个处理程序按预期工作并在我按下 ctrl C 时打印出 "EXITED NICELY"。这是一项任务,我们必须使用信号处理程序。如您所见,我也尝试了 sigaction,但结果相同。

当前行为:"work" 正在打印表明信号处理程序正在工作,但它一定是卡在某个地方,因为它不会取消程序。虽然如果我按 ctrl c 然后向服务器发送一个 http 请求,例如 curl http:/localhost:port/file.name 它会优雅地退出并打印出我想要的消息。但是,我希望它无需发送请求即可执行此操作。

通过进一步研究编辑。我在接听电话前后打印了一张。 print before 将打印一次,然后 accept 将保留它直到它收到连接。那么问题出在哪里,我们该如何解决呢?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<signal.h>
#include<fcntl.h>
#include "http_common.h"
#define CONNMAX 1000
#define BYTES 1024

char *ROOT;
int verbose;
int signalReceived = 1;
int listenfd, clients[CONNMAX];
void error(char *);

static void clean(int arg)
{
        if(arg == SIGINT) {
                printf("work\n");
                signalReceived = 0;
                //signal(SIGINT, clean);
        }
        else if(arg == SIGHUP) {
                signalReceived = 0;
        }
}

int main(int argc, char *argv[])
{
        //struct sigaction act;
        //memset (&act, '[=10=]', sizeof(act));
        //act.sa_handler = clean;
        //sigemptyset(&act.sa_mask);
        //act.sa_flags = SA_RESTART;
        signal(SIGINT, clean);
        //signal(SIGHUP, clean);
        //if (sigaction(SIGINT, &act, NULL) == -1)
        //      printf("doing something\n");
        struct sockaddr_in clientaddr;
        socklen_t addrlen;
        char c;
        char PORT[6];
        ROOT = getenv("PWD");
        strcpy(PORT, "8888");
        int slot;
        while ((c = getopt (argc, argv, "p:v")) != -1)

                switch (c) {
                case'v':
                        verbose = 1;
                        break;
                case'p':
                        strcpy(PORT, optarg);
                        break;
                case'?':
                        fprintf(stderr, "Wrong arguments given\n");
                        exit(1);
                default:
                        exit(1);
                }
        printf("Listening on port %s%s%s, root is %s%s%s\n", "3[92m", PORT, "3[0m", "3[92m", ROOT, "3[0m");
        int i = 0;
        for (i = 0; i < CONNMAX; i++)
                clients[i] = -1;
        startServer(PORT, &listenfd);
        while (signalReceived == 1) {
                addrlen = sizeof(clientaddr);
                clients[slot] = accept (listenfd, (struct sockaddr *) &clientaddr, &addrlen);
                if (clients[slot] < 0)
                        exit(0);
                else {
                        if (fork() == 0) {
                                respond(slot, verbose, ROOT, clients);
                             exit(0);
                        }
                }
                while (clients[slot] != -1)
                        slot = (slot + 1) % CONNMAX;

        }
        printf("EXITED NICLEY\n");//ients[slot] = accept (listenfd, (struct sockaddr *) &clientaddr, &addrlen);
        return 0;
}

您必须使用 sigaction() 并且不使用 SA_RESTART 标志来注册信号。

当您使用 signal() 注册信号处理程序时,它将设置 SA_RESTART 标志。见 the glibc manual:

In the GNU C Library, establishing a handler with signal sets all the flags to zero except for SA_RESTART, whose value depends on the settings you have made with siginterrupt. See Interrupted Primitives, to see what this is about.

设置 SA_RESTART 时,信号将 不会 中断(大多数)系统调用,而是会重新启动它们。见 the signal man page:

If a signal handler is invoked while a system call or library function call is blocked, then either:

  • the call is automatically restarted after the signal handler returns; or
  • the call fails with the error EINTR.

Which of these two behaviors occurs depends on the interface and whether or not the signal handler was established using the SA_RESTART flag (see sigaction(2)).