使用 system() 通过外部程序创建的文件与通过 open() 打开该文件之间需要延迟吗?

发布于 2024-12-21 21:10:14 字数 925 浏览 3 评论 0原文

我正在尝试从我的程序创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴者 2024-12-28 21:10:14

您甚至可以直接在您的文件中使用 libtar 来避免运行外部 tar 命令程序。


添加

并且您应该向我们展示您的程序。我非常确定,如果对 system 的调用只是通过 tar 提取了一些文件,那么它在成功 系统后就可用 调用,例如:

 int err = system("/bin/tar xf /tmp/foo.tar bar");
 int fd = -1;
 if (err == 0)
      fd = open("bar", O_RDONLY);
 // fd is available

没有理由在此代码中等待几秒钟。您可能正在做更复杂的事情,或者您忘记测试 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 thru tar, it is available just after a successful system call, e.g. something like:

 int err = system("/bin/tar xf /tmp/foo.tar bar");
 int fd = -1;
 if (err == 0)
      fd = open("bar", O_RDONLY);
 // fd is available

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

悟红尘 2024-12-28 21:10:14

您认为您正在使用“&>”重定向 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.

眼藏柔 2024-12-28 21:10:14

这是我会尝试的:

  1. 自己 fork/exec tar,并让你的父母收集 tar-child。如果系统在文件系统中引入竞争条件,那么控制子进程的创建/获取可能会有所帮助。

  2. touch 一个空文件(fopen 用于写入和 close),然后 tar 进入新文件.

  3. tar--verify选项;文件必须存在才能被验证:)

Here's what I would try:

  1. 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.

  2. touch an empty file (fopen for writing and close) and then tar into into the new file.

  3. Give tar the --verify option; the file has to exist in order to be verified :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文