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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:37:29+00:00 2026-05-27T01:37:29+00:00

I’m trying to implement a simple shell program that supports multiple piping. For now

  • 0

I’m trying to implement a simple shell program that supports multiple piping. For now my shell can implement some simple builtin commands and also external commands. I did this by getting the user input from the command line which is separated by space " " and put each string in a char* argv[]. The problem now is executing pipes. I read a litle bit about it in this link http://www.cs.loyola.edu/~jglenn/702/S2005/Examples/dup2.html. And I understood how it works.

So I thought about strcmp each argv[i] with "|" and when I meet a pipe, I fork a new process. I then put the strings before the pipe in a char* argv[] and the one after the pipe in another char* argv[]. This might work for 1 pipe but if the user entered multiple have multiple pipes, things might get tedious with this method. My main problem is about separating the strings at both ends of the pipes. Any ideas on how I can implement it? Thanks.

  • 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-27T01:37:30+00:00Added an answer on May 27, 2026 at 1:37 am

    Divide and conquer:

    1. Find the first pipe argument
    2. If no pipe is found, execute the array with stdout as output and terminate.
    3. Divide the argument array between the non-pipe first part and the rest.
    4. Make a pipe and execute the non-pipe first part with the pipe as output
    5. Remove the non-pipe first part and the pipes from the argument array
    6. Goto 1

    Step 3 is, due to C array semantics, pretty simple:

    char **arguments;
    int pipe_position = /* for example: */ 5;
    assert( strcmp( arguments[pipe_position], "|" ) == 0 );
    arguments[pipe_position] = NULL;
    char **my_arguments = arguments;
    arguments = arguments + pipe_position + 1;
    

    and then my_arguments is an array of length pipe_position containing the first arguments, and arguments is an array of length argc - pipe_position - 1 containing the rest. You can give these pointers to execvp without a problem. Yes, they point to the same block of memory, but that’s not execvp’s concern.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.