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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:06:52+00:00 2026-06-11T22:06:52+00:00

So I am at the very end of a project to create a basic

  • 0

So I am at the very end of a project to create a basic UNIX shell using C. I have finished a lot of different pieces of the program, but now I would like to conquer piping. I would specifically like to create a program that can handle any number of pipes.

For some reason my code get to s certain line (labeled: //DIES HERE) and then stops and I can’t figure out why.

Here is the code that I have so far:

//the contents of args[0] is {"ls","-l","-o"}
//the contents of args[1] is {"wc","-l"}

int pipefd[2];

pipe(&pipefd[0]);   // Error check!

fflush(stdout);
for (i = 0; i < commands; i++){
    int pid = fork();

    if (pid == 0){

        int command_no = i;
        int prev_pipe = ((command_no - 1) % 2) * 2;
        int current_pipe = (command_no % 2) * 2;
        printf("\ncmd %d: prev pipe %d, curr pipe %d\n\n", i, prev_pipe, current_pipe);
        fflush(stdout);

        // If current command is the first command, close the
        // read end, else read from the last command's pipe
        if (command_no == 0){
            close(pipefd[0]);
        }
        else{
            dup2(pipefd[prev_pipe], 0);
            close(pipefd[current_pipe]);                    
        }

        // If current command is the last command, close the
        // write end, else write to the pipe
        if (command_no == commands - 1){
            close(pipefd[current_pipe + 1]);                    
        }
        else{
            dup2(pipefd[current_pipe + 1], 1); //DIES HERE
        }
        // printf("Here?\n\n");
        execvp(*args[i], args[i]);
        fprintf(stderr, "Failed to exec: %s (%d: %s)\n", arrayOfCommands[i], errno, strerror(errno));
        _exit(1);
    }
}

Any help is appreciated! 🙂

  • 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-11T22:06:54+00:00Added an answer on June 11, 2026 at 10:06 pm

    The primary issue I see is that pipe() is outside the loop. You’re going to need a new pipe() between every pair of processes. The comments on your question make some good points as well.

    I wrote a shell many years ago in college and here’s the similar loop from my code. I’m sure I’d do it much differently now, but it may be of use to you:

        for (i = 0; i < iNumPipes; ++i) {
                if (i == iNumPipes - 1) {
                        /* this is the last command
                         */
                        p[1] = fdOutput;
                        p[0] = -1;
                } else if (-1 == pipe(p)) {
                        perror("pipe");
                        exit(1);
                }
    
                switch (iPid = fork()) {
                case -1:
                        perror("fork");
                        exit(1);
                case 0:
                        close(0);
                        dup2(fdInput, 0);
                        close(fdInput);
    
                        close(1);
                        dup2(p[1], 1);
                        close(p[1]);
    
                        if (-1 != fdErr) {
                                close(2);
                                dup2(fdErr, 2);
                                close(fdErr);
                        }
    
                        pc = SearchPath(pppcAvs[i][0]);
                        execve(pc, pppcAvs[i], ppcEnv);
                        perror(pc);
                        _exit(-1);
                default:
                        close(fdInput);
                        close(p[1]);
                        fdInput = p[0];
                }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple arithmetic operator but am at my wits end why
I am using InstallShield 2012 Professional to create a basic MSI project. My question
I have an embedded system with Flash and a very low end CPU and
I'm working on a very simple iPhone app, that in the end will have
I am using enableRetinaDisplay in my project and it is working very well except
I have a very simple Project form with a name field and a select
I have a very basic and simple class like this: unit Loader; interface uses
I am very new to Oracle, and I have been put on a project
I'm using itextsharp with a aspmvc project to create PDF versions of some of
My end goal is to create invoices as PDFs for a billing project im

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.