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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:16:34+00:00 2026-06-15T00:16:34+00:00

For an assignment we need to implement a basic shell with re-directions and pipes.

  • 0

For an assignment we need to implement a basic shell with re-directions and pipes. My code for the pipe exits the program and does not ask for the user input again. The pipe is executed in a function, so basically the control is not returned to main. How do I make it do so ? Here is my code :

void execute_pipe(char **argv,char **args)
{

    int pfds[2];
    pid_t pid;
    int status;
    pipe(pfds);
    if ((pid = fork()) < 0) {    
          printf("*** ERROR: forking child process failed\n");
          exit(1);
     }
    printf("here");
    if (pid==0) {
        close(1);      
        dup(pfds[1]);   
        close(pfds[0]); 
           if(execvp(argv[0],argv)<0){
            printf("**error in exec");

        }

    } 
    else {
    while (wait(&status) != pid)  ;  
        close(0);      
        dup(pfds[0]);   
        close(pfds[1]); 

           if(execvp(args[0],args)<0){
            printf("**error in exec");
        }
    }

}
  • 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-15T00:16:35+00:00Added an answer on June 15, 2026 at 12:16 am

    If you dup() or dup2() a pipe to standard input or standard output, you should close both of the file descriptors for the pipe before using execvp() or one of its relatives. Add:

    close(pfds[1]);
    

    to the child code.

    The child process has to die before you run the parent process (because you wait() in a loop). Note that you should check that wait() does not return an error such as ECHILD; if it does, you won’t get beyond the loop. Again, you should be closing both the pipe file descriptors.

    Note that if the child generates more data than will fit in the pipe, the child will be blocked writing to the pipe, while the parent is blocked waiting for the child to die — deadlock.


    How to make it return to main without exiting?

    You can’t make that code return to main() unless either the child or the parent fails to execvp() the command it is given. You would need an extra fork() in there, most probably.

    And the waiting would normally be done by the parent process, not by one of the two children.

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

Sidebar

Related Questions

For a class assignment I need to implement a destructor for the objects of
Hi I have a homework assignment where I need to implement an intersection of
I need to implement quicksort in SML for a homework assignment, and I'm lost.
I need to implement a chess game for a school assignment, and you have
I have the following C++ code that I need to implement merge-sort with. Its
I need to implement a few commands of Linux shell for my homework -
For my homework assignment, I need to implement Horners Algorithm for converting between bases.
For a homework assignment, I need to implement a function which takes a char
I'm trying to implement an Euclidean vector for my programming assignment. I need to
I have an assignment where I need to implement church numerals in SML using

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.