只是这个简短的问题:如果我们收到 SIGTERM 并退出,我们如何关闭 FIFO 文件。如果我的文件是普通文件,它可以工作。但是如果是FIFO,就不行了。有任何想法吗?谢谢 :)
/**
with the help of these guys,
it is not a good approach to write it,
though it works. close(fd) is pointless here.
**/
FILE * myfile;
int fd;
void sig_handler(int signum) {
if (signum == SIGTERM) {
*close(fd);*
fclose(myfile);
printf("caught SIGTERM\n");
exit(EXIT_SUCCESS);
}
}
int main(void) {
myfile = fopen("file", "w+");
fd = open("myfifo", O_RDONLY);
{
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = sig_handler;
sigaction(SIGTERM, &act, 0);
}
for(;;)
pause();
return 0;
}