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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:42:04+00:00 2026-05-27T01:42:04+00:00

I have two pipes that both get different data at random points. What I

  • 0

I have two pipes that both get different data at random points. What I want is to print the content from both pipes to stdout.

              __________________
   pipe1     |                  |
]==============>               -----------.
             |                  |          \
             |     process1     |           -----> stdout
   pipe2     |                  |          /
]==============>               -----------´
             |__________________|

My code looks about this in process1:

while (1) {
  read(pipe1[0], &buff, sizeof(char));  
  write(1, &buff, sizeof(char));
  read(pipe2[0], &buff2, sizeof(char)); 
  write(1, &buff2, sizeof(char));
}

However that doesn’t work as one read() can be blocked(if no data is coming) while data comes from the other pipe.

How can I print simultaneously from both pipes without being blocked in one pipe? Or any other suggestion on how to solve this is welcome.

  • 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-27T01:42:05+00:00Added an answer on May 27, 2026 at 1:42 am

    Use select to wait on both sockets. When data is ready, it will tell you which pipes have data available.

    void setnonblocking(int fd) {
        int opts;
    
        opts = fcntl(fd,F_GETFL);
        if (opts < 0) {
            perror("Couldn't get file descriptor flags");
            exit(EXIT_FAILURE);
        }
        opts = (opts | O_NONBLOCK);
        if (fcntl(fd,F_SETFL,opts) < 0) {
            perror("Couldn't set file descriptor to non-blocking");
            exit(EXIT_FAILURE);
        }
        return;
    }
    
    #ifndef BUFSIZE
    #  define BUFSIZE 1024
    #endif
    void cat(fd_set* waiting, int fd) {
       static char buf[BUFSIZE];
    
       int readCnt;
       if (FD_ISSET(fd, waiting)) {
           while ((readCnt = read(fd, buf, BUFSIZE)) > 0) {
               write(stdout, buf, readCnt);
           }
           if (readCnt < 0) {
               perror("Error reading from pipe");
           }
       }
    }
    
    ...
    {
        fd_set pipes, readable;
    
        setnonblocking(pipes1[0]);
        setnonblocking(pipes2[0]);
    
        FD_ZERO(&pipes);
        FD_SET(pipe1[0],&pipes);
        FD_SET(pipe2[0],&pipes);
    
        int ready;
        while (1) {
            if ((ready = select(2, &pipes, NULL, NULL, NULL)) > 0) {
                cat(&pipes, pipe1[0]);
                cat(&pipes, pipe2[0]);
            } else {
                // no time limit, so there was an error
                perror("Error waiting for input");
                exit(EXIT_FAILURE);
            }
            FD_SET(pipe1[0],&pipes);
            FD_SET(pipe2[0],&pipes);
        }
    }
    

    Note the above runs forever unless there’s an error. You will likely want your program to stop at some point.

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

Sidebar

Related Questions

I have two (UNIX) programs A and B that read and write from stdin/stdout.
I have always used || (two pipes) in OR expressions, both in C# and
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
OK I have a program that creates two pipes -> forks -> the child's
I have a program that creates pipes between two processes. One process constantly monitors
I have two calls of event_wait. Thread 1: Listening pipes from child processes struct
I have two .NET applications that talk to each other over a named pipe.
I have two applications written in Java that communicate with each other using XML
I have two identical tables and need to copy rows from table to another.
I have two WCF apps communicating one-way over named pipes. All is nice, except

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.