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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:32:42+00:00 2026-05-12T05:32:42+00:00

I have a child process which generates some output of variable length and then

  • 0

I have a child process which generates some output of variable length and then sends it to the parent using a half duplex pipe. In the parent, how do I use the read() function?
Since the data can be of different length each time, how can I at run time know the size of the data to do any malloc() for a buffer? Can the fstat() function be used on a pipe file descriptor?

I know that the read() function will read a specified number of bytes but will return 0 if the end of file (not the EOF character) is reached before the bytes requested have been read.

I am specifically running Ubuntu GNU/Linux with a 2.6.27-9 Kernel.

All examples in Richard Stevens’ Advanced Programming in the UNIX Environment have specified either the length of data while writing into the pipe or have relied on fgets() stdio.h function. Since I am concerned with speed, I want to stay away from using stdio.h as much as possible.

Will this be necessarily faster with shared memory?

Thanks,
-Dhruv

  • 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-12T05:32:42+00:00Added an answer on May 12, 2026 at 5:32 am

    Since it seems that you intend to make a single read of all the data from the pipe, I think the following will serve you better than delimiter+encoding or miniheader techniques suggested in other answers:

    From the pipe (7) manpage:

    If all file descriptors referring to
    the write end of a pipe have been
    closed, then an attempt to read(2)
    from the pipe will see end-of-file
    (read(2) will return 0).

    The following example was taken from the pipe (2) manpage and reversed so that the child does the writing, the parent the reading (just to be sure). I also added a variable size buffer. The child will sleep for 5 seconds. The delay will ensure that the exit() of the child can have nothing to do with pipeio (the parent will print a complete line before the child exits).

    #include <sys/wait.h>
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    char *
    slurpfd(int fd)
    {
        const int bytes_at_a_time = 2;
        char *read_buffer = NULL;
        int buffer_size = 0;
        int buffer_offset = 0;
        int chars_io;
        while (1) {
          if (buffer_offset + bytes_at_a_time > buffer_size) {
            buffer_size = bytes_at_a_time + buffer_size * 2;
            read_buffer = realloc(read_buffer, buffer_size);
            if (!read_buffer) {
              perror("memory");
              exit(EXIT_FAILURE);
            }
          }
    
          chars_io = read(fd,
                      read_buffer + buffer_offset,
                      bytes_at_a_time);
          if (chars_io <= 0) break;
          buffer_offset += chars_io;
        }
    
        if (chars_io < 0) {
          perror("read");
          exit(EXIT_FAILURE);
        }
    
        return read_buffer; /* caller gets to free it */
    }
    
    int
    main(int argc, char *argv[])
    {
      int pipefd[2];
      pid_t cpid;
    
      assert(argc == 2);
    
      if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
      }
    
      cpid = fork();
      if (cpid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
      }
    
      if (cpid == 0) {     /* Child writes argv[1] to pipe */
        close(pipefd[0]);  /* Close unused read end */
    
        write(pipefd[1], argv[1], strlen(argv[1]) + 1);
    
        close(pipefd[1]);  /* Reader will see EOF */
        /* sleep before exit to make sure that there
           will be a delay after the parent prints it's
           output */
        sleep(5);
        exit(EXIT_SUCCESS);
      } else {             /* Parent reads from pipe */
        close(pipefd[1]);  /* Close unused write end */
    
        puts(slurpfd(pipefd[0]));
    
        close(pipefd[0]);
        wait(NULL);        /* Wait for child */
        _exit(EXIT_SUCCESS);
      }
    }
    

    From your comment I see now that you may want to read the data as it becomes available, to update the UI or whatever, to reflect your system’s status. To do that open the pipe in non-blocking (O_NONBLOCK) mode. Read repeatedly whatever is available until -1 is returnd and errno == EAGAIN and do your parsing. Repeat unil read returns 0, which indicates that the child has closed the pipe.

    To use an in-memory buffer for File* functions you can use fmemopen() in the GNU C library.

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

Sidebar

Ask A Question

Stats

  • Questions 146k
  • Answers 146k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Download all the source files listed on the page: CameraSource,… May 12, 2026 at 9:03 am
  • Editorial Team
    Editorial Team added an answer You need to put an empty array as the third… May 12, 2026 at 9:03 am
  • Editorial Team
    Editorial Team added an answer You can't use <base> when your URLs are absolute like… May 12, 2026 at 9:03 am

Related Questions

I have parsed XML file into objects, in which each object has a 1:1
I am building a website in CakePHP that processes files uploaded though an XML-RPC
I'm spawning a child process that runs in a visible console window (it's a
I've seen many similar questions & answers, but they've used other DB-specific tricks, or
Suppose I have a process which spawns exactly one child process. Now when the

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.