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

  • Home
  • SEARCH
  • 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 8955251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:30:31+00:00 2026-06-15T14:30:31+00:00

I am trying to implement a simple shell. I fork processes this way: void

  • 0

I am trying to implement a simple shell. I fork processes this way:

void forkProcess(char* cmd[]) {
    pid_t pid;
    char programPath[BUFFERLENGTH] = "/bin/";
    strcat(programPath, cmd[0]);
    int exitStatus;
    pid = fork();
    switch (pid) {
    case -1:
        printf("Fork failed; pid == -1\n");
        break;
    case 0:
        execv(programPath, cmd);
        exit(0);
        break;
    default:
        waitpid(pid, &exitStatus, 0);
        //printf("Exitstatus = %d\n", WEXITSTATUS(exitStatus));
        break;
    }
}

Now the cmd parameter might contain a pipe, e.g.:

"ls" "-l" "|" "grep" "whatever" "(char*)NULL";

So how can I implement the pipe functionality? I know there are functions like pipe() and dup(), but I don’t know how to use them in this context.

Thank you for any suggestions.

  • 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-15T14:30:33+00:00Added an answer on June 15, 2026 at 2:30 pm

    You must fully parse your command line before fork()ing to start the child.

    If a pipe operator is being used, you must set up the pipe before calling fork(), so it is inherited.

    In general you must also use close() and often dup() to make the pipe replace the forked process’ stdin.

    Continue reading up on these functions to “get” the big picture, or get a book which covers Unix I/O.

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

Sidebar

Related Questions

I’m trying to implement a simple fixed headers table. I know this can in
I'm trying to implement simple Comet chat example and for this I implemented Long
I am trying to implement simple xhr abstraction, and am getting this warning when
I am trying to implement a shell in C. I can execute simple commands
I'm trying to implement simple Event Bus I started like this: public class RegistrationData
I'm trying to implement a simple layout, closely represented like this: _________________________ | |
Trying to implement a shell, mainly piping. I've written this test case which I
I was trying to implement a simple shell prompt, that only needs to handle
I'm trying to implement a simple shell program that supports multiple piping. For now
trying to implement a simple sieve of erathosthenes to solve this question on project

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.