不了解PIPE计划

时间:2014-07-09 04:17:44

标签: c linux pipe

我对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

1 个答案:

答案 0 :(得分:3)

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

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

if内,您正在关闭fds[1],现在这将使您的子进程只能拥有管道的读取文件描述符,并且您将在父级中关闭fds[0],这将启用您的父级只有管​​道的写描述符。现在,您的孩子只能从管道读取,父母只能从管道写入,您可以观察到从父母到孩子的沟通(父母正在写作,孩子正在阅读)。如果您想要反向通信而不是创建新管道。因此,一个管道可用于一个方向的通信。

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