C - 如何查找先前由 tmpfile() 创建的临时文件?
我正在开发一个多进程程序,该程序基本上对 RVB 文件的每一层执行模糊化。 (1个过程->1层)。每个子进程都使用以下函数传递临时文件:tmpfile()
。每个子进程完成其工作后,主进程必须读取创建的每个临时文件并组装数据。问题是我不知道如何读取主进程内的每个临时文件,因为我无法访问子进程内存,所以我不知道指向创建的临时文件的临时指针是什么!
有什么想法吗?
如果需要,请随时要求澄清。
I'm working on a multi-process program which basically perform fuzzification on each layer of a RVB file. (1 process -> 1 layer). Each child process is delivering a temp file by using the function: tmpfile()
. After each child process finishes its job, the main process has to read each temp file created and assemble the data. The problem is that I don't know how to read each temp file inside the main process since I can't access to child's process memory so I can't know what's the temporary pointer to the temp file created!
Any idea?
Don't hesitate to ask for clarifications if needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
tmpfile()
函数返回一个FILE
指针,指向一个没有确定名称的文件 - 事实上,即使子进程也无法轻易确定该文件的名称,更不用说父文件(在许多 Unix 系统上,该文件没有名称;在tmpfile()
返回给调用者之前它已取消链接)。因此,如果您必须传递文件名,则您使用了错误的临时文件创建原语。
您有多种选择:
mkstemp()
很好,如果您需要 FILE 指针而不是文件描述符,则可以使用fdopen()
来创建一。您仍然面临着从子级到父级获取文件名的问题;同样,父级可以打开文件,或者您可以为每个子级使用一个管道,或者一些共享内存,或者......选择 IPC 机制。其中,假设并行执行至关重要,我可能会使用管道从子级获取文件名(选项 2);它的协调问题最少。但为了简单起见,我会选择“主程序完成这一切”(选项 5)。
The
tmpfile()
function returns you aFILE
pointer to a file with no determinate name - indeed, even the child process cannot readily determine a name for the file, let alone the parent (and on many Unix systems, the file has no name; it has been unlinked beforetmpfile()
returns to the caller).So, you are using the wrong temporary file creation primitive if you must convey file names around.
You have a number of options:
tmpfile()
so that both the parent and children share the files. There are some minor coordination issues to handle - the parent will need to seek back to the start before reading what the children wrote, and it should only do that after the child has exited.mkstemp()
is good, and if you need a FILE pointer instead of a file descriptor, you can usefdopen()
to create one. You are still faced with the problem of getting file names from children to parent; again, the parent could open the files, or you can use a pipe for each child, or some shared memory, or ... take your pick of IPC mechanisms.Of these, assuming parallel execution is of paramount importance, I'd probably use pipes to get the file names from the children (option 2); it has the fewest coordination issues. But for simplicity, I'd go with 'main program does it all' (option 5).
如果您在父进程中调用 tmpfile() ,子进程将继承所有打开的描述符,并且能够写入文件,并且父进程也可以访问打开的文件。
If you call
tmpfile()
in parent process, child will inherit all open descriptors and will be able to write to the file, and opened file will be accessible for parent as well.您可以在父进程中创建一个临时文件,然后分叉,然后让子进程使用它。
You could create a tempfile in the parent process and then fork, then have the child process use that.
子进程可以将文件描述符发送回父进程。
编辑: APUE 站点中的示例代码(src.tar.gz/apue.2e/lib,recvfd.c 、sendfd.c)
The child process can send back the filedescriptor to the parent process.
EDIT: example code in APUE site (src.tar.gz/apue.2e/lib, recvfd.c, sendfd.c)
使用线程而不是子进程?将临时文件的名称放在另一个文件中?不要对临时文件使用随机名称,而是(例如)基于父进程的 pid 的名称(以允许程序的多个实例同时运行)加上序列号?
Use threads instead of subprocesses? Put the names of the temporary files in another file? Don't use random names for the temp files, but (for example) names based on the pid of the parent process (to allow several instances of your program to run simultaneously) plus a sequential number?