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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:25:46+00:00 2026-05-26T17:25:46+00:00

I am trying to implement a shell in C. I can execute simple commands

  • 0

I am trying to implement a shell in C. I can execute simple commands just fine with a simple execvp() but one of the requirements is to manage commands like this: “ls -l | head | tail -4” with a ‘for’ loop and only one ‘pipe()’ statement redirecting stdin and stdout. Now after days I’m a bit lost.

N = Number of simple commands (3 in the example: ls, head, tail)
commands = a list of structs with the commands, like this:

commands[0].argv[0]: ls
commands[0].argv[1]: -l
commands[1].argv[0]: head
commands[2].argv[0]: tail
commands[2].argv[1]: -4

So, I made the for loop, and started to redirect stdin and stdout in order to connect all the commands with pipes, but…I’m just clueless why it doesn’t work.

for (i=0; i < n; i++){

pipe(pipe);
if(fork()==0){  // CHILD

    close(pipe[0]);
    close(1);
    dup(pipe[1]);
    close(pipe[1]);

    execvp(commands[i].argv[0], &commands[i].argv[0]);
    perror("ERROR: ");
    exit(-1);

}else{      // FATHER

    close(pipe[1]);
    close(0);
    dup(pipe[0]);
    close(pipe[0]);

}
}

What I want to create is a ‘line’ of childed processes:

[ls -l] —-pipe—-> [head] —-pipe—-> [tail -4]

All this processes have a root (the process runing my shell) so, the first father is also a child of the shell process, I’m a bit exhausted already, can anyone help me here please?

I’m not even sure if the childs should be the ones executing the commands.

Thanks guys !!

  • 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-26T17:25:46+00:00Added an answer on May 26, 2026 at 5:25 pm

    Nothing complex here, just have in mind that the last command should output to the original process’ file descriptor 1 and the first should read from original process file descriptor 0. You just spawn the processes in order, carrying along the input side of the previous pipe call.

    So, here’s are the types:

    #include <unistd.h>
    
    struct command
    {
      const char **argv;
    };
    

    Make a helper function with a simple well defined semantics:

    int
    spawn_proc (int in, int out, struct command *cmd)
    {
      pid_t pid;
    
      if ((pid = fork ()) == 0)
        {
          if (in != 0)
            {
              dup2 (in, 0);
              close (in);
            }
    
          if (out != 1)
            {
              dup2 (out, 1);
              close (out);
            }
    
          return execvp (cmd->argv [0], (char * const *)cmd->argv);
        }
    
      return pid;
    }
    

    And here’s the main fork routine:

    int
    fork_pipes (int n, struct command *cmd)
    {
      int i;
      pid_t pid;
      int in, fd [2];
    
      /* The first process should get its input from the original file descriptor 0.  */
      in = 0;
    
      /* Note the loop bound, we spawn here all, but the last stage of the pipeline.  */
      for (i = 0; i < n - 1; ++i)
        {
          pipe (fd);
    
          /* f [1] is the write end of the pipe, we carry `in` from the prev iteration.  */
          spawn_proc (in, fd [1], cmd + i);
    
          /* No need for the write end of the pipe, the child will write here.  */
          close (fd [1]);
    
          /* Keep the read end of the pipe, the next child will read from there.  */
          in = fd [0];
        }
    
      /* Last stage of the pipeline - set stdin be the read end of the previous pipe
         and output to the original file descriptor 1. */  
      if (in != 0)
        dup2 (in, 0);
    
      /* Execute the last stage with the current process. */
      return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
    }
    

    And a small test:

    int
    main ()
    {
      const char *ls[] = { "ls", "-l", 0 };
      const char *awk[] = { "awk", "{print $1}", 0 };
      const char *sort[] = { "sort", 0 };
      const char *uniq[] = { "uniq", 0 };
    
      struct command cmd [] = { {ls}, {awk}, {sort}, {uniq} };
    
      return fork_pipes (4, cmd);
    }
    

    Appears to work. 🙂

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

Sidebar

Related Questions

I'm writing a simple shell that accepts some standard commands like cd and ls
I'm trying to implement a simple shell program that supports multiple piping. For now
I am trying to implement a linux shell that supports piping. I have already
I'm trying to implement a simple method to read new lines from a log
Trying to implement an autocomplete based on this It looks like it's very straight
I'm trying to write a shell in Ruby, and to implement tab completion I
I am trying to implement a underline\fix while you type spell checker can anyone
Trying to implement a shell, mainly piping. I've written this test case which I
I'm trying to implement pipelining in an Operating Systems shell project by creating a
I use the homebrew package manager and the z-shell. I'm trying to implement 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.