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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:18:49+00:00 2026-06-13T07:18:49+00:00

I’m running into problem about redirect stdout of multi process. Assuming I have process

  • 0

I’m running into problem about redirect stdout of multi process.

Assuming I have process A, I use fork() in A and then I get process A and B. And I use fork() in B, finally I get process A, B and C. Both B and C are implementing other program by exec().

Now, I try to redirect the stdout of A and B to stdin of C with two pipes.

#include<unistd.h>
#include<stdio.h>
#include<sty/types.h>
int main()
{
    int AtoC [2];
    pipe(AtoC);

    int fd1,fd2;
    fd1=fork();
    if(fd1>0)
    {
        /***In process A, I do the following steps: ***/
        close(AtoC[0]);
        dup2(AtoC[1], STDOUT_FILENO);
        /* program running in process A */
    }
    else
    {
        int BtoC [2];
        pipe(BtoC);
        fd2=fork();
        if(fd2>0)
        {
            /***In process B, I do the following steps: ***/
            close(AtoC[1]);
            close(BtoC[0]);
            dup2(BtoC[1], STDOUT_FILENO);
            /*** execute another program in process B using execl(); ***/
        }
        else
        {
            /*** In process C, I do the following steps: ***/
            close(AtoC[1]);
            close(BtoC[1]);
            dup2(AtoC[0],STDIN_FILENO);
            dup2(BtoC[0],STDIN_FILENO);
            /*** execute another different program in process C using execl(); ***/
        }
    }
}

Now, after these two statements :

dup2(AtoC[0],STDIN_FILENO);
dup2(BtoC[0],STDIN_FILENO);

the stdin of process C is finally redirect to BtoC[0] ,which is the stdout of process B. And the stdout of process A is not passed into process C’s stdin.

My question is whether there is any solution can let me redirect both process A and B’s stdout into process C’s stdin at the same time.

Another question is if I also want to print the stdout of process A in screen, what should I do? I know the command tee in command line. I try to use the corresponding function tee(int fd_in, int fd_out, size_t len, unsigned int flags)in process A, but I failed to print anything of stdout of process A.

Any suggestion is appreciated, thanks.

  • 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-13T07:18:50+00:00Added an answer on June 13, 2026 at 7:18 am

    I’m not sure that it’s a good idea, but it is perfectly feasible. The key observation is that you only need one pipe. Several processes (with a common parent) can write to a single pipe. However, only one file descriptor can be used as the standard input. In the original code, only one of the pipes was connected to C as its standard input (the others were still connected, but mainly because you hadn’t closed enough descriptors).

    • Rule of thumb: if you connect one end of a pipe to standard input or standard output via dup2() (or dup()), you should close both of the file descriptors returned by pipe().

    Try this code for size. I’ve reduced the bushiness of the tree, removed unused variables (nothing uses the process IDs returned by fork()), renamed the pipe, made sure it is closed properly in each process, and provided some writing activity in processes A and B and some reading activity in process C in lieu of running commands. I’m assuming that usleep() (micro-sleep, sleep time expressed in microseconds) is available; if not, try nanosleep(), but it has a more complex interface).

    #include <unistd.h>
    
    int main(void)
    {
        int ABtoC[2];
        pipe(ABtoC);
    
        if (fork() > 0)
        {
            // Process A
            close(ABtoC[0]);
            dup2(ABtoC[1], STDOUT_FILENO);
            close(ABtoC[1]);     // Close this too!
            // Process A writing to C
            for (int i = 0; i < 100; i++)
            {
                write(STDOUT_FILENO, "Hi\n", sizeof("Hi\n")-1);
                usleep(5000);
            }
        }
        else if (fork() > 0)
        {
            // Process B
            close(ABtoC[0]);
            dup2(ABtoC[1], STDOUT_FILENO);
            close(ABtoC[1]);
            // Process B writing to C
            for (int i = 0; i < 100; i++)
            {
                write(STDOUT_FILENO, "Lo\n", sizeof("Lo\n")-1);
                usleep(5000);
            }
        }
        else
        {
            char buffer[100];
            ssize_t nbytes;
            close(ABtoC[1]);
            dup2(ABtoC[0], STDIN_FILENO);
            close(ABtoC[0]);
            // Process C reading from both A and B
            while ((nbytes = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0)
                write(STDOUT_FILENO, buffer, nbytes);
        }
        return(0);
    }
    

    The tail end of a run on my Mac (Mac OS X 10.7.5, GCC 4.7.1) produced:

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

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.