使用 system() 通过外部程序创建的文件与通过 open() 打开该文件之间需要延迟吗?
我正在尝试从我的程序创建一个 TAR 存档,然后打开该存档进行进一步处理。我在调用 system()
和 open()
之间有 2 秒的延迟。到目前为止,它工作正常,但我不确定为什么需要 2 秒的延迟,或者这是否是正确的解决方案。
如果没有延迟,我会从 open()
调用中收到错误代码 2(ENOENT“没有这样的文件或目录”)。我的第一个想法是文件系统自身更新速度不够快,并且 open()
找不到该文件。但如果系统真的很忙怎么办?我需要更长的延迟吗?我应该循环直到 open() 成功而不是延迟吗?问题是完全不同的吗?
更新
根文件系统是 EXT2。 /tmp
使用 TMPFS 挂载在 RAM 中。我使用 tar
来创建一个存档,而不是提取其中的内容。本质上,我的程序应该创建一些日志文件的存档并通过网络发送它们(这就是我在创建存档后打开存档的原因)。
int return_value = system("/bin/tar -czf /tmp/logs.tar.gz /var/log/mylogs.log* &> /dev/null");
// error checks on return_value as described here: http://linux.die.net/man/2/wait
if(return_value != 0) {
return return_value;
}
//usleep(2000000);
return_value = open("/tmp/logs.tar.gz", O_RDONLY | O_LARGEFILE, 0);
// success or failure depending on whether there's a delay or not
I'm trying to create a TAR archive from my program and then opening the archive for further processing. I have a 2 second delay between calling system()
and open()
. So far, it works fine, but I'm not sure why the 2 second delay is necessary or if it's the correct solution.
Without the delay, I get error code 2 (ENOENT "No such file or directory") from the open()
call. My first thought was the filesystem wasn't updating itself fast enough and open()
couldn't find the file. But what if the system is really busy? Do I need a longer delay? Should I loop until open()
succeeds instead of the delay? Is the problem something completely different?
UPDATE
The root filesystem is EXT2. /tmp
is mounted in RAM using TMPFS. I'm using tar
to create an archive, not extract contents of one. Essentially, my program is supposed to create an archive of some log files and send them over the network (that's why I open the archive after creating it).
int return_value = system("/bin/tar -czf /tmp/logs.tar.gz /var/log/mylogs.log* &> /dev/null");
// error checks on return_value as described here: http://linux.die.net/man/2/wait
if(return_value != 0) {
return return_value;
}
//usleep(2000000);
return_value = open("/tmp/logs.tar.gz", O_RDONLY | O_LARGEFILE, 0);
// success or failure depending on whether there's a delay or not
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您甚至可以直接在您的文件中使用 libtar 来避免运行外部
tar
命令程序。添加
并且您应该向我们展示您的程序。我非常确定,如果对
system
的调用只是通过tar
提取了一些文件,那么它在成功系统后就可用
调用,例如:没有理由在此代码中等待几秒钟。您可能正在做更复杂的事情,或者您忘记测试
system
的结果You could even avoid running an external
tar
command by using libtar directly in your program.ADDED
And you should show us your program. I'm pretty sure that if the call to
system
just extracted some file thrutar
, it is available just after a successfulsystem
call, e.g. something like:there is no reason to wait a few seconds in this code. You are probably doing more complex things, or you forgot to test the result of
system
您认为您正在使用“&>”重定向 tar 的输出,但实际上您正在后台运行它,因为 system() 碰巧调用了不支持 &> 的 shell。因此将其解释为“&”随后是“>”。延迟会导致您的程序等待足够长的时间,直到 tar 完成。
修复方法是修改命令以使用 shell 支持的语法。在任何情况下,从 tar 中抛出错误输出都可能是一个错误。
You think you are redirecting tar's output with "&>", but actually you are running it in the background, because system() happens to invoke a shell that doesn't support &> and so interprets it as "&" followed by ">". The delay causes your program to wait long enough that tar completes.
The fix is to modify your command to use syntax that your shell supports. Throwing the error output from tar is probably a mistake in any case.
这是我会尝试的:
自己 fork/exec tar,并让你的父母收集 tar-child。如果系统在文件系统中引入竞争条件,那么控制子进程的创建/获取可能会有所帮助。
touch
一个空文件(fopen
用于写入和close
),然后tar
进入新文件.给
tar
--verify
选项;文件必须存在才能被验证:)Here's what I would try:
fork/exec tar yourself, and have your parent collect the tar-child. If system is introducing a race condition with the file system, taking control of the child process creating/reaping may help.
touch
an empty file (fopen
for writing andclose
) and thentar
into into the new file.Give
tar
the--verify
option; the file has to exist in order to be verified :)