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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:48:37+00:00 2026-06-04T20:48:37+00:00

I have written this code: #include <stdio.h> #include <unistd.h> #include <sys/wait.h> #define N 10

  • 0

I have written this code:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define N 10

int fd[2];

void son ()
{
int data=0;
if(read(fd[0], &data, sizeof(int))==sizeof(int))
    fprintf(stderr,"%d\n",data);
else
    fprintf(stderr,"Read failed\n");
}

int main(int argc, char** argv)
{
pid_t sons[N];
pipe(fd);
int data=5;
for(unsigned int i=0; i<N;i++)
{
    sons[i]=fork();
    if(!sons[i])
    {
        son();
        break;
    }
}
write(fd[1], &data, sizeof(int));
data=6;
}

If I try executing it, I get 10 times printed “5”.
What happened? Why all processes have read the same data? Can’t the data be read just once? I thought the reader process was consuming the data read.
I modified the code this way:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define N 10

int fd[2];

void son ()
{
int data=0;
if(read(fd[0], &data, sizeof(int))==sizeof(int))
    fprintf(stderr,"%d\n",data);
else
    fprintf(stderr,"Read failed\n");
if(read(fd[0], &data, sizeof(int))==sizeof(int))
    fprintf(stderr,"%d\n",data);
else
    fprintf(stderr,"Read failed\n");
}

int main(int argc, char** argv)
{
pid_t sons[N];
pipe(fd);
int data=5;
for(unsigned int i=0; i<N;i++)
{
    sons[i]=fork();
    if(!sons[i])
    {
        son();
        break;
    }
}
write(fd[1], &data, sizeof(int));
data=6;
write(fd[1], &data, sizeof(int));
return 0;
}

So I write/read two times, the output I get is “5” and “6”, just once, doesn’t happen that all the 10 processes can read the pipe.Why this?

  • 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-04T20:48:39+00:00Added an answer on June 4, 2026 at 8:48 pm

    When you fork() you execute son(), then break out of the loop – but that is not the end of the child process! The child then continues and writes to fd[1].

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

Sidebar

Related Questions

I have written this code which is simple #include <stdio.h> #include <string.h> void printLastLetter(char
see this code #include<stdio.h> int main() { void test(void) { printf(test); return; } printf(main);
I have written this simple c code in microsoft visual c++ 2010. #include<stdio.h> #include<conio.h>
I have written small C++ console application and this is source code : #include<stdio.h>
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written a simple Hello World program. #include <stdio.h> int main() { printf(Hello
I have written a simple code using getopt for understanding perspective. #include <stdio.h> #include
#include<stdio.h> #include<fstream.h> class Test { char name[10]; int data; public: void getData() { cin>>name;
I have written this code for a search page in PHP and I would
I have written this code to join ArrayList elements: Can it be optimized more?

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.