关于<signal.h>中SIG_ERR、SIG_DEL、SIG_IGN定义的问题

本文详细解析了Linux环境下signal.h中signal函数的使用方式及其SIG_ERR宏的含义,通过实例帮助读者理解函数参数和返回值的转换,并解释SIG_ERR为何不直接作为整数的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux中signal.h中对对signal的定义是:

void (*signal(int signo,void (*func)(int)))(int);

通过typedef可以转换成这样:

typedef void Sigfunc(int);
Sigfunc *signal(int,Sigfunc *);

也就是说,signal有两个参数,一个是int,一个是Sigfunc ,返回值也是Sigfunc ,该指针指向一个参数为int,无返回值的函数,然而,SIG_ERR的定义是这样的:

#define SIG_ERR (void (*)())-1
#define SIG_DEL (void (*)())0
#define SIG_IGN (void (*)())1

为什么不是这样定义的呢??

#define SIG_ERR (void (*)(int))-1
#define SIG_DEL (void (*)(int))0
#define SIG_IGN (void (*)(int))1

在网上搜索之后找到答案,C语言中是可以这样定义的:

void fun(); 
int main()
{
       fun(1,2);
} 
void fun(int i, int j)
{
      printf("%d\n",i+j);
}

只是将-1强制转换为一个指针,通过编译。就像#define NULL (void *)0。可以将SIG_ERR跟其他的信号理解的一样,是一个整数。

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include <sys/socket.h> #include <netinet/in.h> #include <dirent.h> #define PORT 8888 #define WEB_ROOT "./web" #define MAX_THREADS 100 // 日志函数(简化版) void log_request(const char *method, const char *path, int status) { printf("[%s] %s -> %d\n", method, path, status); } // 发送HTTP响应 void send_response(int sock, int status, const char *content_type, const char *body, int body_len) { char header[512]; sprintf(header, "HTTP/1.1 %d OK\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n", status, content_type, body_len); send(sock, header, strlen(header), 0); send(sock, body, body_len, 0); } // 处理HTTP请求 void *handle_request(void *arg) { int sock = *(int *)arg; char buffer[4096]; recv(sock, buffer, sizeof(buffer), 0); // 解析请求方法 char method[16], path[256]; sscanf(buffer, "%s %s", method, path); if (strcmp(method, "GET") == 0) { // 构建文件路径 char filepath[512]; if (strcmp(path, "/") == 0) { strcpy(path, "/Index.html"); } sprintf(filepath, "%s%s", WEB_ROOT, path); // 读取文件 FILE *file = fopen(filepath, "rb"); if (file) { fseek(file, 0, SEEK_END); long len = ftell(file); fseek(file, 0, SEEK_SET); char *content = malloc(len); fread(content, 1, len, file); fclose(file); // 根据扩展名设置Content-Type const char *content_type = "text/html"; if (strstr(path, ".css")) { content_type = "text/css"; } else if (strstr(path, ".js")) { content_type = "application/json"; } else if (strstr(path, ".png")) { content_type = "image/png"; } else if (strstr(path, ".jpg")) { content_type = "image/jpeg"; } send_response(sock, 200, content_type, content, len); free(content); log_request(method, path, 200); } else { const char *not_found = "<h1>404 Not Found</h1>"; send_response(sock, 404, "text/html", not_found, strlen(not_found)); log_request(method, path, 404); } } close(sock); return NULL; } int main() { int server_fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(PORT), .sin_addr.s_addr = htonl(INADDR_ANY)}; bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)); listen(server_fd, 10); printf("Threaded server running on port %d\n", PORT); while (1) { int client_fd = accept(server_fd, NULL, NULL); pthread_t tid; int *sock_ptr = malloc(sizeof(int)); *sock_ptr = client_fd; pthread_create(&tid, NULL, handle_request, sock_ptr); pthread_detach(tid); } return 0; } 运行一段时间后程序会自动退出,怎么分析原因
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值