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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:26:50+00:00 2026-05-26T18:26:50+00:00

I am trying to setup a pipe between one process that is launched with

  • 0

I am trying to setup a pipe between one process that is launched with execvp and its parent process.
Right now, I fork/pipe/dup/exec like this:

pid_t child = 0;
int fds[2];
...
void fork_exec(string cmd) {  
    pipe(fds);
    child = fork();
    if(child == 0) {
        close(fds[1]);
        char *input = (char*)cmd.c_str();
        char *cmdArg[100];
        char *token = strtok(input, " \n");
        int i = 0;
        cmdArg[i] = token;
        //while this is not the last token
        while(token != NULL) {
            token = strtok(NULL, " \n");
            cmdArg[++i] = token;
        }
        cmdArg[++i] = NULL;
        close(fds[0]);
        dup2(1, fds[1]);
        execvp(cmdArg[0], cmdArg);
    } else {
        close(fds[1]);
    }
}

And then, in another part of the program, when the function returns, some time later another function (running as part of the parent thead) tries to write the piped value to a file.

void out(string name) {
    ofstream fout;
    fout.open((*cmdIter).c_str(), ofstream::app);
    long lSize = 1000;
    char * buffer;
    buffer = new char[lSize];
    close(fds[1]);
    read(fds[0], buffer, lSize);
    fclose (pFile);
    fout.write(buffer, lSize);
    fout.close();
    delete [] buffer;
    waitpid(child, &res, 0);
}

But this part of the code does not work, instead creates the file but does not write anything to it, and also prints the output to the screen. I believe the child thread should block until there is something to write to?

  • 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-26T18:26:50+00:00Added an answer on May 26, 2026 at 6:26 pm

    I believe the mistake is here:

        dup2(1, fds[1]);
    

    From dup2(2):

       int dup2(int oldfd, int newfd);
    ...
    
       dup2() makes newfd be the copy of oldfd, closing newfd first
       if necessary, but note the following:
    

    So you’ve just closed the filedescriptor returned from pipe(2) a few lines above. Try swapping the two:

        dup2(fds[1], 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After trying to setup my site for Google Webmaster Tools I found that my
I'm trying to setup a bidirectional localConnection (LC) between two flash objects on the
I'm trying setup a map that displays different pin colors depending on the type/class
how can I setup httpd.conf so that I can pipe all requests to that
I am trying to setup two way communication between a daemon and a client
Trying to setup a SQL stored procedure that if the variable passed to it
Background I have a simple socket server setup that I am trying to allow
Im trying to setup a program that will accept an incoming email and then
Trying to setup a test page that access a video hosted on S3 and
Trying to setup a windows development machine for a Rails project that has a

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.