子级执行 exec 调用后,在父级和子级之间共享文件描述符 (UNIX/C)
我的操作系统课上有一个作业。我应该编写三个程序,一个程序从文件读取并写入管道,一个程序从管道读取并写入文件,第三个程序是创建管道并执行两个分叉的父程序生成子进程,该子进程立即执行 exec() 调用。我目前正在做的利用管道的操作是将文件描述符作为 argv[] 的一部分传递到 exec() 调用中,它可以工作,但似乎是错误的解决方案。
我想知道是否有更好的方法来做到这一点。通过参数传递文件描述符似乎很尴尬。
I have an assignment in my OS class. I am supposed to write three programs, one program reads from a file and writes to a pipe, one program reads from a pipe and writes to a file, and the third program is the parent which creates the pipe and does two forks in order to spawn the child processes, which immediately do exec() calls. What I'm currently doing to make use of the pipe, is to pass the file descriptor in the exec() call as part of argv[], it works but it seems like the wrong solution.
What I want to know is if there is a better way to do this. It seems awkward to be passing file descriptors through arguments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fork后的子进程继承父进程打开的文件描述符。请参阅
man 2 fork
。好的。我给您的第一个建议是使用标志
-Wall -Wextra -pedantic
进行编译以获取所有警告。现在我对代码进行了更改:和 test2.c
总结一下:分叉后,您将关闭标准输入描述符并重复 pfd[0],这将成为新的标准输入。然后调用 execve(),它使用 pfd[0] 作为标准输入。有关更多信息,请参阅函数
fork、execve、pipe
的man 2
。Child process after fork inherits parent's opened file descriptors. See
man 2 fork
.Ok. My first advice for you is to compile with flags
-Wall -Wextra -pedantic
to get all warnings. Now my changes in your code:and test2.c
So to sum up: after forking you're closing standard input descriptor and duplicates pfd[0], which will become new standard input. Then call execve(), which uses pfd[0] as it's standard input. See more in
man 2
of functionsfork, execve, pipe
.