gpt4 book ai didi

c - 不了解 PIPE 计划

转载 作者:太空宇宙 更新时间:2023-11-04 07:15:55 27 4
gpt4 key购买 nike

我对 PIPE 的了解是它用于单向通信,它有助于在两个相关进程之间进行通信。我从一本书中得到了下面的 PIPE 编程代码示例。我正在尝试使用 printf 理解代码并在代码的每一行之后打印出所有点。但我不明白程序是如何在每一行之后运行的。我的代码如下:

管道:

//using PIPE to communicate with a child process

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

/* Write COUNT copies of MESSAGE to filename, pausing for a second
between each. */

void writer (const char* message, int count, FILE* filename)
{
for (; count > 0 ; -- count) {
printf("point 13\n");
/* Write the message to the filename, and send it off immediately.*/
fprintf (filename, "%s\n", message);
printf("point 14\n");
fflush (filename);
printf("point 15\n");
/* Snooze a while. */
sleep (1);
}
}


/* Read random strings from the filename as long as possible.
*/

void reader (FILE* filename)
{
char buffer[1024];
/* Read until we hit the end of the filename. fgets reads until
either a newline or the end-of-FILE. */
printf("point 16\n");
while (!feof (filename) && !ferror (filename) && fgets (buffer, sizeof (buffer), filename) != NULL)
fputs (buffer, stdout);
printf("point 17\n");
}


int main ()
{
int fds[2];
pid_t pid;
printf("point 1\n");

/* Create a pipe. FILE descriptors for the two ends of the pipe are
placed in fds. */
pipe (fds);
printf("point 2\n");
/* Fork a child process. */
pid = fork ();
printf("point 3\n");

if (pid == (pid_t) 0)
{
FILE* filename;
printf("point 4\n");
/* This is the child process. Close our copy of the write end of
the FILE descriptor. */
close (fds[1]);
printf("point 5\n");
/* Convert the read FILE descriptor to a FILE object, and read
from it. */
filename = fdopen (fds[0], "r");
printf("point 6\n");
reader (filename);
printf("point 7\n");

close (fds[0]);
printf("point 8\n");
}

else
{
/* This is the parent process. */
FILE* filename;
/* Close our copy of the read end of the FILE descriptor. */
close (fds[0]);
printf("point 9\n");
/* Convert the write FILE descriptor to a FILE object, and write
to it. */
filename = fdopen (fds[1], "w");
printf("point 10\n");
writer ("I want to learn c programming!", 5, filename);
printf("point 11\n");
close (fds[1]);
printf("point 12\n");
}
return 0;
}

我真的需要理解代码。如果我运行我的代码,那么我会在 Linux 终端中得到如下所示的输出,但我不确定为什么在第 3 点之后会出现第 9 点。在第 9 点之后,为什么是第 3、4、5、10 点。详细解释对我有帮助。

point 1
point 2
point 3
point 9
point 3
point 4
point 5
point 10
point 13
point 14
point 15
point 6
point 16
I want to learn c programming!
point 13
point 14
point 15
I want to learn c programming!
point 13
point 14
point 15
I want to learn c programming!
point 13
point 14
point 15
I want to learn c programming!
point 13
point 14
point 15
I want to learn c programming!
point 11
point 12
point 17
point 7
point 8

最佳答案

pipe(int[]) 函数返回一个大小为两个整数的数组。您需要了解此处的基本概念。管道可以像水管一样用于单向通信。您需要了解的一件事是管道在内部是一个文件。因此,当我们使用文件描述符从文件读取内容或向文件写入内容时,我们同样需要用于从管道读取/写入管道的描述符。现在在您的程序中,当执行 pipe(fds) 时,会创建一个管道并创建两个描述符用于读取和写入该管道。这些描述符是fds[0](读端)和fds[1](写端)。

现在,您需要了解这里的fork(),该函数通过复制现有进程创建了一个新进程(子进程)。调用 fork() 后,在子进程中立即执行 if,因为 fork 在子进程中返回 0,在父进程中 if 条件失败,因为 fork 返回父进程中子进程的pid,因此执行else。休息你可以观察你的代码。

if 中,您正在关闭 fds[1],这将使您的子进程现在只读取管道的文件描述符,并且您正在关闭 fds [0] 在父级中,这将使您的父级只有管道的写入描述符。现在,您的 child 只能从管道读取,而 parent 只能从管道写入,您可以观察到父子之间的通信(父在写,子在读)。如果您想要反向通信,则需要创建一个新管道。因此,一根管道可以用于一个方向的通信。

现在,您只需了解父进程和子进程在 fork 之后将如何执行,就可以理解程序的其余部分。现在,无论哪个进程获得执行该进程的 CPU 时间,通过这种方式,您都可以交错执行子进程和父进程,从而得到上述输出。

关于c - 不了解 PIPE 计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24645328/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com