子进程和父进程之间的管道通信使用 pipe()
Pipeline Communication between Child Process and Parent Process using pipe()
我正在尝试设置管道以使用 pipe() 在子进程和父进程之间进行通信。我在 Whosebug 上阅读了一些帖子,有些帖子使用了 dup() 和 dup2() 函数。有人可以解释一下在这种情况下这些功能的用途吗?
您可以使用 dup2
分别重定向子进程和父进程的 stdin 和 stdout,以通过与指令 pipe
创建的文件描述符一起使用的管道发送消息。为了以更具体的方式说明其功能,这里有一个详细的示例说明如何操作。
#include <unistd.h>
#include <stdio.h>
#define READ 0
#define WRITE 1
int main (int argc, char * argv [ ] )
{
int fd[2];
pipe(fd); // creating an unnamed pipe
if (fork() !=0)
{
close(fd[READ]); // Parent close the reading descriptor
dup2(fd[WRITE], 1); // copy fd[WRITE]] in the descriptor 1 (stdout)
close (fd[WRITE]); // closing the writing descriptor, not needed anymore because of stdout
if(execlp(argv[1], argv[1], NULL) ==-1) // execute the program writer passed as an argument to myprog
perror("error in execlp");
}
else // child process (reader)
{
// closing unused writing descriptor
close(fd[WRITE]);
// copy fd[READ] in descriptor 0 (stdin)
dup2(fd[READ],0);
close (fd[READ]); // closing reading descriptor, not needed anymore because of stdin
// execute reading command
if(execlp(argv[2], argv[2], NULL) == -1) // Execute the reader passed as an argument to myprog
perror("connect");
}
return 0 ;
}
这样,父进程通过标准输出发送的每条消息都将重定向到子进程的标准输入。例如,当执行命令 myprog who wc
时(使用上面显示的代码),它的行为就像在终端中执行 who | wc
一样。你可以看到我的父进程 who
将通过标准输出向 wc
发送消息。
因为dup
和dup2
的区别。你可以看看这个 link.
pipe()
创建新的文件描述符,这意味着您可以像对文件、标准输入等一样写入和读取它们
dup2
和 dup
是重命名文件描述符,例如将标准输出替换为程序
要与子进程和父进程通信,您实际上并不需要 dup
或 dup2
您可以使用 pipe()
给您的新文件描述符,并保持标准 input/output 打开
这是父子进程的简单通信
int main()
{
int fd[2];
int pid;
if (pipe(fd) == -1)
return (1);
pid = fork();
if (pid == 0)
{
/* child process
reading parent process */
char rdbuff[10];
close(fd[1]);
read(fd[0], rdbuff, 10);
printf("got: %s\n", rdbuff);
}
else if (pid > 0)
{
/* parent
sending string to child process */
close(fd[0]);
write(fd[1], "sup!", 5);
}
else
{
/* error */
return (1);
}
}
我正在尝试设置管道以使用 pipe() 在子进程和父进程之间进行通信。我在 Whosebug 上阅读了一些帖子,有些帖子使用了 dup() 和 dup2() 函数。有人可以解释一下在这种情况下这些功能的用途吗?
您可以使用 dup2
分别重定向子进程和父进程的 stdin 和 stdout,以通过与指令 pipe
创建的文件描述符一起使用的管道发送消息。为了以更具体的方式说明其功能,这里有一个详细的示例说明如何操作。
#include <unistd.h>
#include <stdio.h>
#define READ 0
#define WRITE 1
int main (int argc, char * argv [ ] )
{
int fd[2];
pipe(fd); // creating an unnamed pipe
if (fork() !=0)
{
close(fd[READ]); // Parent close the reading descriptor
dup2(fd[WRITE], 1); // copy fd[WRITE]] in the descriptor 1 (stdout)
close (fd[WRITE]); // closing the writing descriptor, not needed anymore because of stdout
if(execlp(argv[1], argv[1], NULL) ==-1) // execute the program writer passed as an argument to myprog
perror("error in execlp");
}
else // child process (reader)
{
// closing unused writing descriptor
close(fd[WRITE]);
// copy fd[READ] in descriptor 0 (stdin)
dup2(fd[READ],0);
close (fd[READ]); // closing reading descriptor, not needed anymore because of stdin
// execute reading command
if(execlp(argv[2], argv[2], NULL) == -1) // Execute the reader passed as an argument to myprog
perror("connect");
}
return 0 ;
}
这样,父进程通过标准输出发送的每条消息都将重定向到子进程的标准输入。例如,当执行命令 myprog who wc
时(使用上面显示的代码),它的行为就像在终端中执行 who | wc
一样。你可以看到我的父进程 who
将通过标准输出向 wc
发送消息。
因为dup
和dup2
的区别。你可以看看这个 link.
pipe()
创建新的文件描述符,这意味着您可以像对文件、标准输入等一样写入和读取它们
dup2
和 dup
是重命名文件描述符,例如将标准输出替换为程序
要与子进程和父进程通信,您实际上并不需要 dup
或 dup2
您可以使用 pipe()
给您的新文件描述符,并保持标准 input/output 打开
这是父子进程的简单通信
int main()
{
int fd[2];
int pid;
if (pipe(fd) == -1)
return (1);
pid = fork();
if (pid == 0)
{
/* child process
reading parent process */
char rdbuff[10];
close(fd[1]);
read(fd[0], rdbuff, 10);
printf("got: %s\n", rdbuff);
}
else if (pid > 0)
{
/* parent
sending string to child process */
close(fd[0]);
write(fd[1], "sup!", 5);
}
else
{
/* error */
return (1);
}
}