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 6391613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:41:35+00:00 2026-05-25T03:41:35+00:00

I’ve been messing around in C trying to figure out how to do this.

  • 0

I’ve been messing around in C trying to figure out how to do this. Let’s say I have my main program, the parent process. The parent creates three child processes, each of which will eventually run programs (but that’s not important right now). What I’d like to do is make it so that the first child’s stdout will be received by the second child’s stdin. The second child’s stdout will then be received by the third child’s stdin.
The parent process’s stdin/stdout aren’t messed with at all.

So far, what I’ve got is

pipe(procpipe);

parentPid = getpid();

for(i = 0; i < 3; i++)
{
    if(getpid() == parentPid)
    {
        child[i] = fork();
        if(child[i] == 0)
        {
            mynumber = i+1;
        }
    }
}

But from there I’m kind of stuck as to how to use dup2 to properly assign my pipes, and in which section of my code to insert it. There are lots of examples on Google and this website of how to pipe from a parent to a child, but I’m yet to see one that will tell me exactly how to connect a child’s stdout to another child’s stdin.

Edit:
Forgot to mention: assume all my variables are properly initialised. The int ‘mynumber’ is so a child process knows upon creation which number it is, so I can give it instructions via

if(mynumber == whatever)
  • 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-25T03:41:36+00:00Added an answer on May 25, 2026 at 3:41 am

    So you have a loop that creates several child processes. Each of these child processes will be using two pipes: read from previous and write to the next. To set up a pipe for the reading end you need to close the write end of the pipe, and dup2 the read end into the stdin. Similar for the pipe where the process will be writing.

    void set_read(int* lpipe)
    {
        dup2(lpipe[0], STDIN_FILENO);
        close(lpipe[0]); // we have a copy already, so close it
        close(lpipe[1]); // not using this end
    }
      
    void set_write(int* rpipe)
    {
        dup2(rpipe[1], STDOUT_FILENO);
        close(rpipe[0]); // not using this end
        close(rpipe[1]); // we have a copy already, so close it
    }
    

    When you fork each children you need to attach the pipes to it.

    void fork_and_chain(int* lpipe, int* rpipe)
    {
        if(!fork())
        {
            if(lpipe) // there's a pipe from the previous process
                set_read(lpipe);
            // else you may want to redirect input from somewhere else for the start
            if(rpipe) // there's a pipe to the next process
                set_write(rpipe);
            // else you may want to redirect out to somewhere else for the end
    
            // blah do your stuff
            // and make sure the child process terminates in here
            // so it won't continue running the chaining code
        }
    }
    

    With this in hand you can now write a loop that continuously forks, attaches the pipes, and then reuses the output pipe as the input pipe for the next one. Of course, once both ends of a pipe have been connected to child processes, the parent should not leave it open for itself.

    // This assumes there are at least two processes to be chained :)
    
    // two pipes: one from the previous in the chain, one to the next in the chain
    int lpipe[2], rpipe[2];
    
    // create the first output pipe
    pipe(rpipe);
    
    // first child takes input from somewhere else
    fork_and_chain(NULL, rpipe);
    
    // output pipe becomes input for the next process.
    lpipe[0] = rpipe[0];
    lpipe[1] = rpipe[1];
    
    // chain all but the first and last children
    for(i = 1; i < N - 1; i++)
    {
        pipe(rpipe); // make the next output pipe
        fork_and_chain(lpipe, rpipe);
        close(lpipe[0]); // both ends are attached, close them on parent
        close(lpipe[1]);
        lpipe[0] = rpipe[0]; // output pipe becomes input pipe
        lpipe[1] = rpipe[1];
    }
    
    // fork the last one, its output goes somewhere else      
    fork_and_chain(lpipe, NULL);
    close(lpipe[0]);
    close(lpipe[1]);
    

    The closing bits are very important! When you fork with an open pipe, there will be four open file descriptors: two on the parent process, and two others on the child process. You have to close all of those you won’t be using. That’s why the code above always closes the irrelevant ends of the pipes in the child processes, and both ends on the parent.

    Also note that I am giving special treatment to the first and the last processes, because I don’t know where the input for the chain will come from, and where the output will go to.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.