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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:08:50+00:00 2026-05-28T15:08:50+00:00

This is my code #include <iostream> #include <string> #include <sys/wait.h> #include <sys/types.h> #include <unistd.h>

  • 0

This is my code

#include <iostream>
#include <string>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include <stdio.h>
using namespace std;

int main() {

    int pipe_A[2];
    int pipe_B[2];

    pipe(pipe_A);
    pipe(pipe_B);

    pid_t pid_A, pid_B, pid_C;

    if (!(pid_A = fork())) {
        dup2(pipe_A[1], 1); /* redirect standard output to pipe_A write end */

        system("./data");
        exit(0);
    }

    if (!(pid_B = fork())) {
        dup2(pipe_A[0], 0); /* redirect standard input to pipe_A read end */
        dup2(pipe_B[1], 1); /* redirect standard output to pipe_B write end */

        system("grep 5");
        exit(0);
    }

    if (!(pid_C = fork())) {
        dup2(pipe_B[0], 0); /* redirect standard input to pipe_B read end */

        system("grep 2");
        exit(0);
    }

    return 0;
}

In which “./data” is my test sample that does:

for(int i=0;i<100;i++){
cout<<i<<endl;
}

I tried to build a pipe according to this link:

Apparently my program does not work. After I run my program, there will be two processes doing pipe_wait for ever (I saw that from system monitor). It seems that there are some pipes should be closed. How should I fix it?

  • 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-28T15:08:50+00:00Added an answer on May 28, 2026 at 3:08 pm

    Each child gets its own copy of the parent’s file descriptors. And dup2 creates yet another copy. The read side of the pipe will not return EOF until all of the copies of the write side get closed.

    So after each call to fork + dup2, go ahead and close all four of pipe_A[0], pipe_A[1], pipe_B[0], and pipe_B[1].

    Also close them in the parent after you fork all of the children. (This not strictly necessary in your example, since they will be closed automatically when main() returns. But I would do it anyway for cleanliness.)

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

Sidebar

Related Questions

I've got this code: #include <iostream> #include <string> #include <vector> using namespace std; vector<string>
Running this code: #include <string> #include <iostream> using namespace std; int main() { string
For the following code: #include<iostream> #include<vector> #include<string> using namespace std; struct Test { string
I'm getting compile error in this code #include<iostream> #include<cstdio> #include<string> using namespace std; void
Given this code: #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <boost/regex.hpp> using
I have the following code: #include <iostream> #include <string> #include <unistd.h> using namespace std;
I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
Can anybody help me with this simple code?? #include <iostream> using namespace std; void

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.