我一直在閱讀EINTR
關於write(2)
等,並試圖確定我是否需要在我的程序中檢查它。作爲一個完整的檢查,我試圖編寫一個可以運行的程序。該程序永遠循環,重複寫入文件。爲什麼不寫(2)返回EINTR?
然後,在一個單獨的外殼,我運行:
while true; do pkill -HUP test; done
然而,我從test.c的看到的唯一輸出是從信號處理程序中的.
秒。爲什麼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更大的寫入大小寫入另一個'mmap'文件的文件。 – 2012-08-07 11:04:54