C:打开后立即删除的临时文件

时间:2020-12-07 09:25:52

I'm trying to make a temp file, to which I want write a bunch of stuff, and then print out upon receiving a signal. However, after some diagnostics with lsof it looks like the temp file is deleted immediately after opening it. Take the following snippet:

我试着做一个临时文件,我想写一堆东西,然后在收到信号后打印出来。但是,在使用lsof进行一些诊断之后,该临时文件在打开后立即被删除。把以下代码片段:

FILE *tmp;

int main(int argc, char *argv[]) {

    if ((tmp = tmpfile()) == NULL)
        err_sys("tmpfile error");

    sleep(60);

Now if I go do a ps aux, get the pid of my process, and then do a lsof -p <pid>, I see the following:

现在如果我做一个ps aux,得到我的进程的pid,然后做lsof -p ,我看到

10.06   1159 daniel    3u   REG    0,1     0 10696049115128289 /tmp/tmpfCrM7Jn (deleted)

This is a bit of a head-scratcher for me. Considering that it's really only a single built in function call, which is not causing an error when being called, I'm not sure what the problem is.

这对我来说有点让人挠头。考虑到它实际上只是一个内置函数调用,在被调用时不会导致错误,我不确定问题是什么。

1 个解决方案

#1


3  

From the man page:

从手册页:

The created file is unlinked before tmpfile() returns, causing the file to be automatically deleted when the last reference to it is closed.

创建的文件在返回tmpfile()之前被取消链接,导致在关闭对该文件的最后一个引用时自动删除该文件。

The output from lsof simply indicates that the path pointing to the inode was removed. However, the current file handle FILE *tmp should still be valid, until the file is closed, or the program exits.

lsof的输出仅仅表示删除指向inode的路径。但是,当前的文件句柄file *tmp应该仍然有效,直到文件关闭,或者程序退出。

#1


3  

From the man page:

从手册页:

The created file is unlinked before tmpfile() returns, causing the file to be automatically deleted when the last reference to it is closed.

创建的文件在返回tmpfile()之前被取消链接,导致在关闭对该文件的最后一个引用时自动删除该文件。

The output from lsof simply indicates that the path pointing to the inode was removed. However, the current file handle FILE *tmp should still be valid, until the file is closed, or the program exits.

lsof的输出仅仅表示删除指向inode的路径。但是,当前的文件句柄file *tmp应该仍然有效,直到文件关闭,或者程序退出。