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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:00:09+00:00 2026-06-15T07:00:09+00:00

Currently am doing two forks to pipeline two process, but I think am doing

  • 0

Currently am doing two forks to pipeline two process, but I think am doing my wait(&status) wrong because after the command my shell just hangs and does not return to my prompt. I know my pipe is working because I can see the result if I remove the wait.

Any tips?

pipe(mypipe);
pid1=fork();
if(pid1==0)
{       
    pid2=fork();
    if(pid2==0)
    {
        close(0);
        dup(mypipe[0]);
        close(mypipe[1]);
        execv(foundnode2->path_dir,arv2);
        exit(0);
    }
    close(1);
    dup(mypipe[1]);
    close(mypipe[0]);
    pid2 = wait(&status2);
    execv(foundnode1->path_dir,arv1);
    exit(0);
}
pid1 = wait(&status2);  
  • 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-15T07:00:12+00:00Added an answer on June 15, 2026 at 7:00 am

    Rule of Thumb: if you use dup() or dup2() to map one end of a pipe to standard input or standard output, you should close() both ends of the pipe itself. You’re not doing that; your waits are waiting for the programs to finish, but the programs will not finish because there is still a process with the pipe open that could write to the pipe. Also, the process which created the pipe needs to close both ends of the pipe since it is not itself using the pipe (the child processes are using it). See also C MiniShell — Adding Pipelines.

    Also, you should not be waiting for the first child to finish before launching the second (so the pid2 = wait(&status2); line is a bad idea). Pipes have a fairly small capacity (traditionally it was 5 KiB, on many modern systems it is still only 64 KiB); if the total data to be transferred is too large, the writing child may block waiting for the reading child to read, but the reading child hasn’t started yet because it is waiting for the writing child to exit (and it takes a long time for this deadlock to resolve itself). You’re seeing the output appear without the wait() calls because the second part of the pipeline executes and processes the data from the first part of the pipeline, but it is still waiting for more data to come from the shell.

    Taking those tips into account, you might end up with:

    pipe(mypipe);
    pid1 = fork();
    if (pid1 == 0)
    {        
        pid2 = fork();
        if (pid2 == 0)
        {
            close(0);
            dup(mypipe[0]);
            close(mypipe[1]);
            close(mypipe[0]);
            execv(foundnode2->path_dir, arv2);
            fprintf(stderr, "Failed to exec %s\n", foundnode2->path_dir);
            exit(1);
        }
        close(1);
        dup(mypipe[1]);
        close(mypipe[0]);
        close(mypipe[1]);
        execv(foundnode1->path_dir, arv1);
        fprintf(stderr, "Failed to exec %s\n", foundnode1->path_dir);
        exit(1);
    }
    close(mypipe[0]);
    close(mypipe[1]);
    pid1 = wait(&status1);
    

    Notice the error reporting to standard error when the commands fail to execv(). Also, the exit status of 0 should be reserved for success; 1 is a convenient error exit status, or you can use EXIT_FAILURE from <stdlib.h>.

    There is a lot of error checking omitted still; the fork() operations could fail; the pipe() might fail. One consequence is that if the second fork() fails, you still launch the second child (identified by foundnode1->path_dir).

    And I note that you could save yourself a little work by moving the pipe creation into the first child process (the parent then does not need to — indeed, cannot — close the pipe):

    int pid1 = fork();
    if (pid1 == 0)
    {
        int mypipe[2];      
        pipe(mypipe);
        int pid2 = fork();
        if (pid2 == 0)
        {
            close(0);
            dup(mypipe[0]);
            close(mypipe[1]);
            close(mypipe[0]);
            execv(foundnode2->path_dir, arv2);
            fprintf(stderr, "Failed to exec %s\n", foundnode2->path_dir);
            exit(1);
        }
        close(1);
        dup(mypipe[1]);
        close(mypipe[0]);
        close(mypipe[1]);
        execv(foundnode1->path_dir, arv1);
        fprintf(stderr, "Failed to exec %s\n", foundnode1->path_dir);
        exit(1);
    }
    pid1 = wait(&status1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query that I'm currently solving by doing two queries. I
Currently doing a VB 6.0 project,after registering few ocx files added the same in
I'm currently doing two implementations of an algorithm, one in C and the other
As title says, I'm currently doing some small hacks with pointers in C++, but
I want to compare two collections in C# that I'm currently doing using nested
Im currently doing a console app that has to send scheduled emails. In one
I'm currently doing facebook integration and have gone through this tutorial . So far,
I am currently doing a project in which I have to request data from
I'm currently doing the following to compensate for boolean's not mapping well to radio
I am currently doing a C# WPF application that generates a table that does

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.