#include "apue.h"
#include <fcntl.h>
int
main(void)
{
int fd;
char buf[20] = {0};
if ((fd =open("tempfile", O_RDWR)) < 0)
err_sys("open error");
if (unlink("tempfile") < 0)
err_sys("unlink error");
printf("file unlinked/n");
read(fd, buf, sizeof(buf));//you could still read this after unlink
printf("%s/n", buf);
sleep(15);
printf("done/n");
exit(0);
}
This property of unlink is often used by a program to ensure that a temporary file it creates won't be left around in case the program crashes. The process creates a file using either open or creat and then immediately calls unlink . The file is not deleted, however, because it is still open. Only when the process either closes the file or terminates, which causes the kernel to close all its open files, is the file deleted.
很高级,很好