Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6219421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:46:40+00:00 2026-05-24T07:46:40+00:00

From http://pubs.opengroup.org/onlinepubs/009604599/functions/pipe.html : The pipe() function shall create a pipe and place two file

  • 0

From http://pubs.opengroup.org/onlinepubs/009604599/functions/pipe.html:

The pipe() function shall create a pipe and place two file
descriptors, one each into the arguments fildes[0] and fildes[1], that
refer to the open file descriptions for the read and write ends of the
pipe.

There is an example where a parent writes data to its child:

int fildes[2];
const int BSIZE = 100;
char buf[BSIZE];
ssize_t nbytes;
int status;


status = pipe(fildes);
if (status == -1 ) {
    /* an error occurred */
    ...
}


switch (fork()) {
case -1: /* Handle error */
    break;


case 0:  /* Child - reads from pipe */
    close(fildes[1]);                       /* Write end is unused */
    nbytes = read(fildes[0], buf, BSIZE);   /* Get data from pipe */
    /* At this point, a further read would see end of file ... */
    close(fildes[0]);                       /* Finished with pipe */
    exit(EXIT_SUCCESS);


default:  /* Parent - writes to pipe */
    close(fildes[0]);                       /* Read end is unused */
    write(fildes[1], "Hello world\n", 12);  /* Write data on pipe */
    close(fildes[1]);                       /* Child will see EOF */
    exit(EXIT_SUCCESS);
}
  1. I was wondering how a parent using a pipe to communicate with its child can make sure it will not happen that the child runs read() before the parent runs write, and the child
    finish reading before the parent finish writing?

  2. I was wondering if a pipe created in the parent can be used for
    two-way communication between parent and child, or just one way from
    the parent to the child not the other way?

    If the child can send data to parent via the pipe, what will the
    program looks like?

  3. Is a pipe like a real pipe with two ends. fildes[0] and fildes[1]
    are used to represent the two ends respectively?

    If a pipe is two way, what is the meaning of read end and write end
    with respect to,i.e.which one read (write), the parent or the child?

Thanks and regards!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:46:40+00:00Added an answer on May 24, 2026 at 7:46 am
    1. Unless you specify O_NONBLOCK, read will block and wait until data becomes available. So it doesn’t matter; if data isn’t available yet, it will wait for it.
    2. No, pipes are not duplex. If you want bidirectional communication, you should make two pipes (which will give you four file descriptors total). fildes[0] is always the reading (output) end, and fildes[1] is the writing (input) end. So you set up one pipe exactly as you have done it (where the parent gets the writing end and the child gets the reading end, so the parent can send to the child), and you set up the other pipe in the opposite manner (you let the parent keep the reading end and give the child the writing end).
    3. fildes[0] and fildes[1] do indeed represent the ends of the pipe, but they are not interchangeable. One is used for putting data “into” the pipe, and the other is for getting data “out of” the pipe.

    If you need more clarification, feel free to ask; I realize this can be confusing for newcomers.

    Update to answer questions in comment:

    1. Any process can call pipe(), and its subprocesses will inherit the file descriptors it receives (unless you close them). That is, they survive through multiple fork() calls. So you can write a program that will give the file descriptors to grandchildren, great-grandchildren, etc. if you want to.
    2. Sure, two sibling processes can communicate like this. The parent calls pipe() to create the pair (or two pairs if you want bidirectional communication), then forks twice. You can then have one child use fildes[0], and the other child use fildes[1]. The parent should use neither file descriptor. Voilà! Communication between children with a common parent. It’s remarkable how powerful this simple (albeit potentially unintuitive) feature is. This looks something like the example below.
    3. Picture fork() as a cloning machine. Before going into the cloning machine, you use pipe() to make a pair of matched walkie-talkies which use a special secret frequency. One can only transmit; the other can only receive. When you use the cloning machine, two copies of you emerge (the original, i.e. parent, and the clone, i.e. child). Each copy is now carrying an identical pair of matched walkie-talkies. If the original destroys his “receive” device and the clone destroys his “transmit” device, then the original can talk to the clone using the walkie-talkies (but not the other way around).

      But you see, the key idea here is not that pipe() connects two processes (though that’s its primary use), it’s that it’s something that is duplicated during the fork(). You can re-enter the cloning machine many times and get many walkie-talkies, so what defines “who is talking to whom” (which processes are connected by the pipe) is not a particular aspect of the function of the cloning machine or the walkie-talkies.

      Rather, it’s determined by who ends up actually using which endpoints, which is entirely in your control. If you wanted, you could have the child close both endpoints (destroy both walkie-talkies), which would leave the parent to talk to himself (this is silly and probably not useful, but it is possible). I realize this was a bit of a tangent, but I’m trying to communicate how the basic idea works so that you understand the realm of things that are possible with pipes.

    Example of sibling processes talking with a pipe (unidirectional):

    int fildes[2];
    int status;
    
    status = pipe(fildes);
    if (status == -1) { ... }
    
    // first fork
    switch (fork()) {
    case -1:
        // handle error
        break;
    
    case 0:
        // child 1 (with the writing end -- close the writing end)
        close(fildes[0]);
        // your logic for child 1 here, using fildes[1] to write
        return;
    
    default:
        // not child 1: close the writing end
        close(fildes[1]);
        break;
    }
    
    // second fork
    switch (fork()) {
    case -1:
        // handle error
        break;
    
    case 0:
        // child 2 (with the reading end)
        // your logic for child 2 here, using fildes[0] to read
        return;
    }
    
    // still in the parent
    // logic for the common parent here, after forking both children
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From http://www.faqs.org/rfcs/rfc2822.html : CR and LF MUST only occur together as CRLF; they MUST
from http://directwebremoting.org/dwr/reverse-ajax/index.html , it stated it supports polling, comet,piggyback. does that mean when we
Python documentation from http://docs.python.org/library/string.html : string.lstrip(s[, chars]) Return a copy of the string with
From: http://doc.qt.nokia.com/4.7/signalsandslots.html Callbacks have two fundamental flaws: Firstly, they are not type-safe. We can
Hi I'm starting from http://matplotlib.sourceforge.net/users/path_tutorial.html . I'm looking a function to get all bezier
Using this example from http://docs.python.org/tutorial/classes.html#class-objects : class MyClass: A simple example class i =
From http://developer.android.com/guide/topics/fundamentals.html : It's possible to arrange for two applications to share the same
From http://en.wikipedia.org/wiki/AJAX , I get a fairly good grasp of what AJAX is. However,
From http://www.jibbering.com/faq/faq_notes/closures.html : Note: ECMAScript defines an internal [[prototype]] property of the internal Object
From http://projecteuler.net/index.php?section=problems&id=99 Comparing two numbers written in index form like 2 11 and 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.