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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:53:15+00:00 2026-05-25T11:53:15+00:00

I have created two processes using fork(). The child process is producing and writing

  • 0

I have created two processes using fork(). The child process is producing and writing continuously a variable amount of data (array char) to the pipe. The parent process reads from the pipe and prints the received data to stdout.

The code is very simple:

switch (fork()) {
  case -1: 
    exit (1);
    break;
  case 0:
    close(fd[0]);
    generate_data(fd[1]);
    break;
  default:
    close(fd[1]);
    while(1) {
        n = read(fd[0], readbuffer, sizeof(readbuffer));
        readbuffer[n] = 0;
        if (n > 0)
            printf ("read: %s\n", readbuffer);            
        else
            exit(1);
    }   
   break;
}   

Where generate_data(int) iterates over a list, writing each element (string) to the file descriptor given as argument (the write end of the pipe in this case):

void generate_data(int fd) 
{
   node_t node* = list;
   while (node != NULL) {
     write(fd, node->data, strlen(node->data)+1);
     node = node->next();
   }

}

The problem here is that the output is always unpredictable: the child process writes data to the pipe when the other process is processing the last read, so when it calls to read again the rest of the data is not there anymore.

According to man 2 pipe, this shouldn’t be happening:

Data written to the write end of the pipe is buffered by the
kernel until it is read from the read end of the pipe.

Taking a list of 10 elements, some output examples:

Example 1:

read: element_4
read: element_8
read: element_9

Example 2:

read: element_7
read: element_8
read: element_9
read: element_10

Example 3:

read: element_2
read: element_8

Anyone has any idea what’s happening here?

  • 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-25T11:53:16+00:00Added an answer on May 25, 2026 at 11:53 am

    You call read and capture the return value, but then you largely ignore it; it’s telling you how many valid bytes are in readbuffer, but you’re treating readbuffer as if it contains a zero-terminated string, which it does not necessarily. In fact, a single read may be giving you multiple zero-terminated strings, if your data-writing process is sending 0 bytes through the pipe; using printf means you’re ignoring the second and subsequent ones. At the very least, you’ll need to use fwrite to write the specific, correct number of bytes to stdout, although I suspect what you’ll actually need to do is replace those zeroes with newlines first. It might be a better idea to modify generate_data to send newlines instead of zeroes.

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

Sidebar

Related Questions

I have created an alert view with two buttons using the following code: UIAlertView
I have two processes which exange messages each other. Process A is a normal
I have two frames - left frame contains menu options created using css and
I have created two forms in my Windows Application. One Form acts as a
I have created two files: tunables.h #ifndef TUNABLES_H #define TUNABLES_H void tunables_load_conservative(); void tunables_load_aggressive();
I have created two JavaScript files. One file is "validators.js" and the other is
Let's say I have created two objects from class foo and now want to
I'm having a problem with mixing managed and unmanaged code. I have created two
I have created an item swapper control consisting in two listboxes and some buttons
@Solved The two subquestions I have created have been solved (yay for splitting this

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.