open()在第一次尝试时失败(open() fails at first attempt)

编程入门 行业动态 更新时间:2024-10-19 08:52:31
open()在第一次尝试时失败(open() fails at first attempt)

open()在首次尝试时无法使用ENOENT(没有此类文件或目录),但在后续尝试中可以正常工作。 我的程序分叉子进程,并等待子进程使用waitpid() 。 子进程使用execl()创建从特定目录中的用户接收的文件路径的副本。 子进程退出后,父进程使用open()打开这个新创建的副本。 但是在第一次尝试时它与ENOENT(没有这样的文件或目录)失败。 我可以看到子进程在指定的目录中创建了一个文件。 如果我通过提供相同的文件名再次运行此程序,那么它工作正常。 我的问题是:为什么不在第一次尝试时打开文件? 我需要刷新目录还是什么?

我在redhat上

这是一个快速的N代码代码

my_function() { char *src = "TEST.txt"; char *dest = "./Output/"; char *fp = "/Output/TEST.txt"; int fd; struct fstat file_stat; pid_t PID = fork(); if(PID == -1) exit(1); if(PID == 0) { execl("/bin/cp", "/bin/cp", src, dest); exit(1); } if(PID > 0) { int chldstat; pid_t ws = waitpid(PID,&chldstat,WNOHANG); } if(stat(fp,&file_stat) == -1) { perror("stat"); exit(1); } if((fd = open(dest,O_RDWR)) == -1) { perror("open"); exit(1); } if((fp=mmap(0,file_stat.st_size,PROT_READ | PROT_WRITE,fd,0)) == -1) { perror("mmap"); exit(1); } //OTHER ROUTINES ............. ............ ............ }

open() fails with ENOENT (no such file or directory) on first attempt but works fine in subsequent attempts. My program forks a child process and and waits for the child to finish using waitpid(). The child process creates a copy of a file path received from user in specific directory using execl(). Once the child exits, the parent process opens this newly created copy using open(). However it fails with ENOENT (no such file or directory) on the first attempt. I can see that child process creates a file in the specified directory. If I run this program again by supplying the same file name, then it works fine. My question is: Why isn't it opening the file on the first attempt? Do I need to refresh directory or what is it?

I am on redhat

HERE IS A QUICK N DIRTY CODE SNIPPETS

my_function() { char *src = "TEST.txt"; char *dest = "./Output/"; char *fp = "/Output/TEST.txt"; int fd; struct fstat file_stat; pid_t PID = fork(); if(PID == -1) exit(1); if(PID == 0) { execl("/bin/cp", "/bin/cp", src, dest); exit(1); } if(PID > 0) { int chldstat; pid_t ws = waitpid(PID,&chldstat,WNOHANG); } if(stat(fp,&file_stat) == -1) { perror("stat"); exit(1); } if((fd = open(dest,O_RDWR)) == -1) { perror("open"); exit(1); } if((fp=mmap(0,file_stat.st_size,PROT_READ | PROT_WRITE,fd,0)) == -1) { perror("mmap"); exit(1); } //OTHER ROUTINES ............. ............ ............ }

最满意答案

正如其他人所指出的,没有源代码就很难回答这样的问题。 但:

你似乎患有竞争条件。 该文件已创建,但比您的第一次打开尝试晚了一点。 在你的第二次尝试中,你更幸运,文件已经创建。 再次运行工作正常的事实支持这一理论 - 该文件甚至在程序启动之前就存在,因此随时打开它成功。

你怎么会有竞争条件? 如果孩子创造它,并且父亲只有在确认孩子结束后才试图打开它,那么应该没有问题。 很难推测出现了什么问题。 也许你等待错误的过程。 也许孩子创建了另一个创建文件的进程,而父进程只等待第一个子进程。 还有一百万其他的maybes。

As other noted, it's hard to answer such a question without the source code. But:

You seem to suffer from a race condition. The file is created, but a bit later than your first open attempt. On your second attempt you're luckier, and the file was already created. The fact that running again works fine supports this theory - the file existed even before the program started, so opening it succeeds at any time.

How come you have the race condition? If the child creates it, and the father tries to open it only after it has verified that the child ended, then there should be no problem. It's hard to speculate what went wrong. Maybe you wait for the wrong process. Maybe the child creates another process, which creates the file, and the parent waits only for the first child. And a million other maybes.

更多推荐

本文发布于:2023-08-07 02:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:open   fails   attempt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!