子级执行 exec 调用后,在父级和子级之间共享文件描述符 (UNIX/C)

发布于 2024-12-03 02:24:08 字数 220 浏览 0 评论 0原文

我的操作系统课上有一个作业。我应该编写三个程序,一个程序从文件读取并写入管道,一个程序从管道读取并写入文件,第三个程序是创建管道并执行两个分叉的父程序生成子进程,该子进程立即执行 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 技术交流群。

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

发布评论

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

评论(1

记忆里有你的影子 2024-12-10 02:24:08

fork后的子进程继承父进程打开的文件描述符。请参阅man 2 fork

好的。我给您的第一个建议是使用标志 -Wall -Wextra -pedantic 进行编译以获取所有警告。现在我对代码进行了更改:

/*removed char pipe_readend[12];*/
char * arg[2] = {"test2", NULL}; /*first argument of program should be always it's name*/
char message[3] = {'h', 'i', '\0'}; /*c-string should be always terminated by '\0'*/
/*...*/
 case 0:
        /* child actions */
            close(pfd[1]); /* close write end */
            close(STDIN_FILENO); /*close standard input descriptor*/
            dup(pfd[0]); /*now standard input will be pfd[0]*/
            execve("test2", arg, NULL); /* start new process */
            break;

和 test2.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (void){
    char msg[3];
    read(STDIN_FILENO, msg, 3); /*read from standard input*/
    printf("test2: %s\n", msg);
    return 0;
}

总结一下:分叉后,您将关闭标准输入描述符并重复 pfd[0],这将成为新的标准输入。然后调用 execve(),它使用 pfd[0] 作为标准输入。有关更多信息,请参阅函数 fork、execve、pipeman 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:

/*removed char pipe_readend[12];*/
char * arg[2] = {"test2", NULL}; /*first argument of program should be always it's name*/
char message[3] = {'h', 'i', '\0'}; /*c-string should be always terminated by '\0'*/
/*...*/
 case 0:
        /* child actions */
            close(pfd[1]); /* close write end */
            close(STDIN_FILENO); /*close standard input descriptor*/
            dup(pfd[0]); /*now standard input will be pfd[0]*/
            execve("test2", arg, NULL); /* start new process */
            break;

and test2.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (void){
    char msg[3];
    read(STDIN_FILENO, msg, 3); /*read from standard input*/
    printf("test2: %s\n", msg);
    return 0;
}

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 functions fork, execve, pipe.

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