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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:21:24+00:00 2026-05-26T21:21:24+00:00

I am working on an assignment for my Operating System class (Posix & C),

  • 0

I am working on an assignment for my Operating System class (Posix & C), building a mini-shell, and I don’t know how to solve the following problem:

My mini-shell has to accept two commands, for example ls | grep a. For that I create a pipe of size two and a child. The child closes all that it has to close and opens all that it has to open (standard/pipe’s in & out). It then executes “ls,” using execvp. I am sure this works well. After that, the parent shuts and opens inputs and outputs (I am sure I do it well), and then executes grep a.

Now, the problem is that the process grep a never finishes. Same for tail -1, e.g.. Yet it does work for head -1. I think that happens because grep and tail, which are executed by the parent, wait for more input, even though the child has finished its operation. ls | grep a produces the right output, displayed on the console (The pipe’s output is set as default output), but, as I’ve said, grep a does not finish.

So, my question is: how can I inform the parent that the pipe has finished writing, so it can finish the execution of grep a for example?

Thank you.

Here’s the code:

[fd is the pipe, it is initialized previously in the code. If you can see any incongruous thing, please let me know; I’ve cleaned the code a bit, and this is only the problematic part, as you can see.]

   int fd[2];
   pipe(fd);

   if ((pid = fork()) != -1){ 
     if(pid == 0){         /*Child executing */
        close(fd[0]);
        close(1);
        dup(fd[1]);
        close(fd[1]);
        execvp(argvv[0][0], argvv[0]); /* Here's stored the first instruction */

      } else{             /* Parent executing */
        wait(&status);
        close(fd[1]);
        close(0);
        dup(fd[0]); 
        close(fd[0]);
        execvp(argvv[1][0], argvv[1]); /* Here's stored the second instruction */
       }
   }  
  • 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-26T21:21:25+00:00Added an answer on May 26, 2026 at 9:21 pm

    If the grep continues to run after the ls has exited, that indicates that you have not closed all the pipes that you need to close.

    In particular, the write end of the pipe whose read end is attached to the grep process is still open in another process. You will need to show your code to know more.


    The code you have pasted works correctly (when expanded to a full program, as per the below). Both subprocesses exit just fine.

    This means that you’ve eliminated the code that has the problem when you created your cut-down version here – perhaps you have another fork() between the pipe() call and this fork()?

    #include <unistd.h>
    #include <sys/wait.h>
    
    int main()
    {
       pid_t pid;
       char *argvv[2][3] = { { "ls", 0, 0}, { "grep", "a", 0 } };
       int status;
    
       int fd[2];
       pipe(fd);
    
       if ((pid = fork()) != -1) {
         if(pid == 0){         /*Child executing */
            close(fd[0]);
            close(1);
            dup(fd[1]);
            close(fd[1]);
            execvp(argvv[0][0], argvv[0]); /* Here's stored the first instruction */
    
          } else{             /* Parent executing */
            wait(&status);
            close(fd[1]);
            close(0);
            dup(fd[0]);
            close(fd[0]);
            execvp(argvv[1][0], argvv[1]); /* Here's stored the second instruction */
           }
       }
    
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are working on a toy operating system as a assignment for a class.
We are working on a lab assignment for my CS&E class and I think
I'm working on a class assignment that started small, so I had it all
I am working on an Operating Systems assignment for one of my summer classes.
I'm new to C++ and was working on an assignment for a class. We
I'm working on an assignment has a class hierarchy like blow: abstract EventClass someEventClass1
I'm working on an assignment for class and the question asks to find matches
I'm working on a assignment for class so this is only part of the
I'm working on an assignment for a class, and need a bit of direction.
i'm working on a network programming assignment about writing a simple IM system (pretty

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.