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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:22:55+00:00 2026-06-07T16:22:55+00:00

Below is the code which I am trying to get it working…. I was

  • 0

Below is the code which I am trying to get it working….

I was expecting the output as

OUTPUT from PipeAttempt(args1, args2)

followed by

I am here

OUTPUT from PipeAttempt(args3, args4)

But in reality, I get only output from PipeAttempt(args1, args2);

and the program waits for input from me, when I hit the enter key the program terminates

Can you please let me know what am I missing here ?

int main () {
    char* args1 [] = {"/usr/bin/head", "/etc/passwd", NULL};
    char* args2 [] = {"/bin/sort", NULL};

    char* args3 [] = {"/bin/cat", "piped.input", NULL}; 
    char* args4 [] = {"/usr/bin/wc", NULL};



    PipeAttempt(args1, args2);

    printf("I am here\n");

    PipeAttempt(args3, args4);


    return 0;

}


void PipeAttempt(char* args1[], char* args2[]) {
    int pfildes[2]; <br>
    pid_t cpid1, cpid2; <br>
    char *envp[] = { NULL };<br>


    if (pipe(pfildes) == -1)    {perror("demo1"); exit(1);}
    if ((cpid1 = fork()) == -1)   {perror("demo2"); exit(1);}
    else if (cpid1 == 0) {        /* child:  "cat a"                      */
          close(pfildes[0]);    /* close read end of pipe               */
          dup2(pfildes[1],1);   /* make 1 same as write-to end of pipe  */
          close(pfildes[1]);    /* close excess fildes                  */

       execve(args1[0], args1, envp);

          perror("demo3");       /* still around?  exec failed           */
          exit(1);             /* no flush                             */ 
        }
    else {                      /* parent:  "/usr/bin/wc"               */
          close(pfildes[1]);    /* close write end of pipe              */
          dup2(pfildes[0],0);   /* make 0 same as read-from end of pipe */
          close(pfildes[0]);    /* close excess fildes                  */

       execve(args2[0], args2, envp);
          perror("demo4");       /* still around?  exec failed           */
          exit(1);          /* parent flushes                       */
        }   
}
  • 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-07T16:22:56+00:00Added an answer on June 7, 2026 at 4:22 pm

    Your original parent was overwritten by the exec call.So you were only able to get the output of /usr/bin/head /etc/passwd | /bin/sort and you did not get the get the output of /bin/cat piped.input|/usr/bin/wc.

    The program actually did not wait for you for keyboard input. What happended is that the shell prompt was not shown.The parent exited before the child and the prompt was immediatly shown by the shell(which waits for the parent) and the output from the child came after that, which made you think that the program is waiting for user input.

    Here to solve this you can fork another child in the function, so that effectively for each function (PipeAttempt) call two child will handle the pipe and in main you can wait for all the four child to complete, so that the parent exits at last and you get back the shell prompt. Given the modified code below, below pl. include the header files for wait, strlen etc before compiling. Hope this solves your query.

    void PipeAttempt(char* args1[], char* args2[]) ;
    int main () {
    int i;
    char* args1 [] = {"/usr/bin/head", "/etc/passwd", NULL};
    char* args2 [] = {"/bin/sort", NULL};
    
    char* args3 [] = {"/bin/cat", "piped.input", NULL}; 
    char* args4 [] = {"/usr/bin/wc", NULL};
    
    PipeAttempt(args1, args2);
    write(1, "I am here\n", strlen("I am here\n"));
    PipeAttempt(args3, args4);
    for (i=0; i < 4; i++)
    {
    
        wait(NULL);
    }
    return 0;
    }
    
    void PipeAttempt(char* args1[], char* args2[]) 
    {
        int pfildes[2]; 
        pid_t cpid1, cpid2; 
        char *envp[] = { NULL };
    
    
        if (pipe(pfildes) == -1)    {perror("demo1"); exit(1);}
    
        if ((cpid1 = fork()) == -1)   {perror("demo2"); exit(1);}
        else if (cpid1 == 0)
       {        /* child1    */
            close(pfildes[0]);    /* close read end of pipe               */
            dup2(pfildes[1],1);   /* make 1 same as write-to end of pipe  */
            close(pfildes[1]);    /* close excess fildes                  */
            execve(args1[0], args1, envp);
            perror("demo3");       /* still around?  exec failed           */
            exit(1);             /* no flush                             */ 
        }
       else {                      
        /* child2:  "/usr/bin/wc"               */
         if (0==fork())
          {
             close(pfildes[1]);    /* close write end of pipe              */
             dup2(pfildes[0],0);   /* make 0 same as read-from end of pipe */
             close(pfildes[0]);    /* close excess fildes                  */
             execve(args2[0], args2, envp);
             perror("demo4");       /* still around?  */
             exit(1);
          }
    //Parent
     close(pfildes[0]); 
     close(pfildes[1]); 
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C extension (code below) in which I'm trying to get access
I'm trying to get the code below working so that it will call a
In the below code, i am trying to reference an external .dll, which creates
I have a piece of code below which I am trying to use to
Ok, so I'm trying to get some UDP code working and I'm barely green
I'm trying to get the sample code from Mozilla that consumes a REST web
I've been trying to get the PHPUnit mock objects working for some legacy code
Below is my code which I am trying to find 'Username Ok' in the
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)

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.