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

  • SEARCH
  • Home
  • 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 8425467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:16:44+00:00 2026-06-10T04:16:44+00:00

One implementation of a pipe is: #define STD_INPUT 0 #define STD_OUTPUT 1 pipeline(char *process1,

  • 0

One implementation of a pipe is:

#define STD_INPUT 0
#define STD_OUTPUT 1
pipeline(char *process1, char *process2)
{
    int fd[2];

    pipe(&fd[0]);
    if (fork() != 0) {
        /* The parent process executes these statements. */
        close(fd[0]);
        close(STD_OUTPUT);
        dup(fd[1]);
        close(fd[1]);    /* this file descriptor not needed anymore */
        execl(process1, process1, 0);
    }
    else {
        /* The child process executes these statements. */
        close(fd[1]);
        close(STD_INPUT);
        dup(fd[0]);
        close(fd[0]);   /* this file descriptor not needed anymore */
        execl(process2, process2, 0);
   }
}

I am confused by the use of the two statements which follow each dup call, respectively.

close(fd[1]);    /* this file descriptor not needed anymore */

and

close(fd[0]);   /* this file descriptor not needed anymore */

I am told the descriptors are no longer needed, but to me those descriptors represent each end of the pipe, so why are they no longer needed?

  • 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-06-10T04:16:45+00:00Added an answer on June 10, 2026 at 4:16 am

    The pipe call returns both the read descriptor and the write descriptor for unidirectional communication. However, the writer does not need the read descriptor (fd[0]). And, the reader does not need the write descriptor (fd[1]). So, each process after the fork call closes the descriptor it does not need, and uses the descriptor that it does need.

    So, the parent is the writer in your example. It closes the fd[0] first, and then closes STD_OUTPUT. It then duplicates fd[1], which will now be in STD_OUTPUT since it is available. Since the output descriptor of the pipe is now duplicated, it is no longer needed either, so it is closed. Now, when the writer writes something to STD_OUTPUT, it will be writing to the output descriptor of the pipe.

    The child, which is the reader, performs similar logic, but on the other descriptor. It first closes fd[1], and then closes STD_INPUT. It then duplicates fd[0], which results in the descriptor being in STD_INPUT. After being duplicated, the input descriptor of the the pipe is no longer needed, so it is closed. Now, when the reader reads something from STD_INPUT, it will be reading from the input descriptor of the pipe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got one implementation of read/write locks which is below. Notice that in the
Can a Objective c interface have more than one implementation? Example I could have
One more question about most elegant and simple implementation of element combinations in F#.
Does any one know if there is kinda implementation/addon for VS 2008 SP1 for
What's the best implementation for more than one background service in an ASP.NET application?
I found Swenson's C implementation of Timsort: https://github.com/swenson/sort mentioned in one of the older
We are just upgrading our ASP.Net shop implementation (from simple one) to structure that
In relation to this question ( Efficient hashCode() implementation ) I have one more
Anybody knows proper python implementation of TEA (Tiny Encryption Algorithm)? I tried the one
I am using Mojarra 2.0.4 implementation of JSF with Tomcat server for one of

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.