信号处理程序在我的C http服务器上不工作
问题:我希望我的单个处理程序按预期工作,并在按下ctrl C时打印“很好地退出”。这是一个赋值,我们必须使用信号处理程序。正如你所看到的,我也用sigaction做了实验,但结果是一样的 当前行为:“work”正在打印出来,表明信号处理程序正在工作,但它一定在某个地方卡住了,因为它没有取消程序。尽管如此,如果我按ctrl-c,然后向服务器发送一个http请求,例如curl http:/localhost:port/file.name,它将优雅地退出并打印出所需的消息。然而,我希望它这样做,而不必我发送请求 通过进一步研究进行编辑。我在接听电话前后打印了一张照片。print before将打印一次,然后accept将保持它直到收到连接。那么问题出在哪里,我们如何解决呢信号处理程序在我的C http服务器上不工作,c,linux,signals,httpserver,C,Linux,Signals,Httpserver,问题:我希望我的单个处理程序按预期工作,并在按下ctrl C时打印“很好地退出”。这是一个赋值,我们必须使用信号处理程序。正如你所看到的,我也用sigaction做了实验,但结果是一样的 当前行为:“work”正在打印出来,表明信号处理程序正在工作,但它一定在某个地方卡住了,因为它没有取消程序。尽管如此,如果我按ctrl-c,然后向服务器发送一个http请求,例如curl http:/localhost:port/file.name,它将优雅地退出并打印出所需的消息。然而,我希望它这样做,而不必
#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, '\0', 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", "\033[92m", PORT, "\033[0m", "\033[92m", ROOT, "\033[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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“http_common.h”
#定义CONNMAX 1000
#定义字节1024
字符*根;
int冗长;
接收到的int信号=1;
int listenfd,客户端[CONNMAX];
无效错误(字符*);
静态无效清除(内部参数)
{
如果(arg==SIGINT){
printf(“工作”);
接收到的信号=0;
//信号(SIGINT,清洁);
}
else if(arg==SIGHUP){
接收到的信号=0;
}
}
int main(int argc,char*argv[])
{
//结构动作法;
//memset(&act,'\0',sizeof(act));
//act.sa_handler=清洁;
//sigemptyset(和act.sa_面具);
//act.sa_flags=sa_重启;
信号(SIGINT,清洁);
//信号机(信号机,清洁);
//if(sigaction(SIGINT,&act,NULL)=-1)
//printf(“正在做某事”);
clientaddr中的结构sockaddr_;
索克伦·阿德伦;
字符c;
字符端口[6];
ROOT=getenv(“PWD”);
strcpy(端口号“8888”);
int槽;
while((c=getopt(argc,argv,“p:v”)!=-1)
开关(c){
案例“v”:
详细=1;
打破
案例“p”:
strcpy(港口,optarg);
打破
案例“?”:
fprintf(stderr,“给出的参数错误”);
出口(1);
违约:
出口(1);
}
printf(“侦听端口%s%s%s,根目录是%s%s%s\n”,“\033[92m”,端口“\033[0m”,“\033[92m”,根目录“\033[0m”);
int i=0;
对于(i=0;i
您必须使用sigaction()
注册信号,并且不使用SA_RESTART
标志
使用signal()
注册信号处理程序时,它将设置SA_RESTART
标志。请参阅:
在GNU C库中,建立带有信号的处理程序会将所有标志设置为零,但SA_RESTART除外,其值取决于您使用siginterrupt进行的设置。有关详细信息,请参阅Interrupted Primitives
设置SA_RESTART
时,信号不会中断(大多数)系统调用,而是会重新启动它们。请参阅:
如果在系统调用或库函数调用被阻止时调用信号处理程序,则:
- 信号处理程序返回后,调用将自动重新启动;或
- 调用失败,错误为EINTR