【发布时间】:2012-08-04 08:50:56
【问题描述】:
我一直在阅读write(2) 等关于EINTR 的内容,并试图确定是否需要在我的程序中检查它。作为健全性检查,我尝试编写一个会运行它的程序。程序永远循环,重复写入文件。
然后,在一个单独的 shell 中,我运行:
while true; do pkill -HUP test; done
但是,我从 test.c 看到的唯一输出是来自信号处理程序的 .s。为什么SIGHUP 不会导致write(2) 失败?
test.c:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
void hup_handler(int sig)
{
printf(".");
fflush(stdout);
}
int main()
{
struct sigaction act;
act.sa_handler = hup_handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGHUP, &act, NULL);
int fd = open("testfile", O_WRONLY);
char* buf = malloc(1024*1024*128);
for (;;)
{
if (lseek(fd, 0, SEEK_SET) == -1)
{
printf("lseek failed: %s\n", strerror(errno));
}
if (write(fd, buf, sizeof(buf)) != sizeof(buf))
{
printf("write failed: %s\n", strerror(errno));
}
}
}
【问题讨论】:
-
由于这个
write没有I/O,所以不可能中断I/O。您只是在修改缓存中的页面。尝试写入套接字、写入 NFS 服务器或使用大于 RAM 的写入大小从另一个mmaped 文件写入文件。