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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:25:49+00:00 2026-05-14T21:25:49+00:00

I am using popen to execute a command under linux, then 4 process wile

  • 0

I am using popen to execute a command under linux, then 4 process wile use the same output.
I am trying to duplicate the file descriptor again to pass it to each process.
here is my code:

FILE* file_source = (FILE*) popen(source_command, "r");
int fd = fileno(file_source);
fdatasync(fd);

int dest_fd[4], y, total = 4;
    for (y = 0; y < total; y++) {
        dest_fd[y] = dup(fd);
    }

actually if total set to 1 it work fin, after changing total = 4 it does not work anymore.
this answer is too close to what i need:
link

  • 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-14T21:25:50+00:00Added an answer on May 14, 2026 at 9:25 pm

    Your current approach probably won’t do what you want. When you just duplicate the file descriptors, they all refer to the same pipe – no data is going to get duplicated. For each block of data sent by the source command, exactly one process is going to read it.

    If you want to duplicate the data (like the tee utility does), then you will need to explicitly do so:

    #define TOTAL 4
    
    int dest_fd[TOTAL];
    int dest_fd_wr[TOTAL];
    int y;
    
    /* Build pipes for reading the data from the child process */
    for (y = 0; y < TOTAL; y++)
    {
        int p[2];
    
        pipe(p);
        dest_fd[y] = p[0];
        dest_fd_wr[y] = p[1];
    }
    
    /* Create a child process to handle the "tee"-style duplication */
    if (fork() == 0)
    {
        /* Child process */
        FILE *file_source = popen(source_command, "r");
        FILE *file_sink[TOTAL];
        char buffer[2048];
        size_t nbytes;
    
        for (y = 0; y < TOTAL; y++)
        {
            close(dest_fd[y]);
            file_sink[y] = fdopen(dest_fd_wr[y], "w");
        }
    
        while ((nbytes = fread(buffer, 1, sizeof buffer, file_source)) > 0)
        {
            for (y = 0; y < TOTAL; y++)
            {
                fwrite(buffer, 1, nbytes, file_sink[y]);
            }
        }
    
        _exit(0);
    }
    
    for (y = 0; y < TOTAL; y++)
    {
        close(dest_fd_wr[y]);
    }
    
    /* Now have a set of file descriptors in dest_fd[0..TOTAL-1] that each have
     * a copy of the data from the source_command process. */
    

    Error-handling is left as an exercise for the reader 😉

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

Sidebar

Ask A Question

Stats

  • Questions 410k
  • Answers 411k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no need to use the error_messages_for helper for… May 15, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer The delete rule govern what happens to the object at… May 15, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer Try this: $from = mysql_real_escape_string($_POST['from']); $to = mysql_real_escape_string($_POST['to']); $message =… May 15, 2026 at 7:38 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.