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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:50:20+00:00 2026-05-20T10:50:20+00:00

I’m trying to develop a shell in Linux as an Operating Systems project. One

  • 0

I’m trying to develop a shell in Linux as an Operating Systems project. One of the requirements is to support pipelining (where calling something like ls -l|less passes the output of the first command to the second). I’m trying to use the C pipe() and dup2() commands but the redirection doesn’t seem to be happening (less complains that it didn’t receive a filename). Can you identify where I’m going wrong/how I might go about fixing that?

EDIT: I’m thinking that I need to use either freopen or fdopen somewhere since I’m not using read() or write()… is that correct?

(I’ve heard from others who’ve done this project that using freopen() is another way to solve this problem; if you think that would be better, tips for going that direction would also be appreciated.)

Here’s my execute_external() function, which executes all commands not built-in to the shell. The various commands in the pipe (e.g. [ls -l] and [less]) are stored in the commands[] array.

 void execute_external()
{        
    int numCommands = 1;
    char **commands;
    commands = malloc(sizeof(char *));

    if(strstr(raw_command, "|") != NULL)        
    {
        numCommands = separate_pipeline_commands(commands);
    }
    else
    {
        commands[0] = malloc(strlen(raw_command) * sizeof(char));
        commands[0] = raw_command;
    }

    int i;
    int pipefd[2];
    for (i = 0; i < numCommands; i++)
    {
        char **parameters_array = malloc(strlen(commands[i]) * sizeof(char *));
        int num_params;
        num_params = str_to_str_array(commands[i], parameters_array);

        if (numCommands > 1 && i > 0 && i != numCommands - 1)
        {
            if (pipe(pipefd) == -1)
            {
                printf("Could not open a pipe.");
            }
        }

        pid_t pid = fork();


        pmesg(2, "Process forked. ID = %i. \n", pid);
        int status;
        if (fork < 0)
        {
            fprintf(to_write_to, "Could not fork a process to complete the external command.\n");
            exit(EXIT_FAILURE);
        }

        if (pid == 0) // This is the child process
        {
            if (numCommands > 1) { close(pipefd[1]); } // close the unused write end of the pipe
                           if (i == 0) // we may be pipelining and this is the first process
            {
                dup2(1, pipefd[1]); // set the source descriptor (for the next iteration of the loop) to this proc's stdout
            }
            if (i !=0 && (i != numCommands-1)) // we are pipelining and this is not the first or last process
            {
                dup2(pipefd[0], 0); // set the stdin of this process to the source of the previous process
            }
            if (execvp(parameters_array[0], parameters_array) < 0)
            {
                fprintf(to_write_to, "Could not execute the external command. errno: %i.\n", errno);
                exit(EXIT_FAILURE);
            }
            else    { pmesg(2, "Executed the child process.\n");}
        }
        else
        {
            if (numCommands > 1) { close(pipefd[0]); } // close the unused read end of the pipe
            if (backgrounding == 0) { while(wait(&status) != pid); }// Wait for the child to finish executing
        } 
        free(parameters_array);
    }
free(commands);
    }
  • 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-20T10:50:21+00:00Added an answer on May 20, 2026 at 10:50 am

    It looks like there are a couple of bugs going on in your code.

    First, all your dup2’s are only in the child. In order to connect a pipe you will need to dup2 the stdout of the parent to the write end pipefd[1] of the pipe. Then you would hook up the read end to stdin.

    Also it looks like on of your dup2’s is backwards with dup2 fildes is duplicated to fildes2. So when you reassign stdin you want dup2(in, 0) and for stdout you want dup2(out, 1).

    So a stripped down piece of piping code is going to look like:

     int pipefd[2];
     pipe(pipefd);
    
     pid_t pid = fork();
    
     if (pid == 0) //The child
     {
          dup2(pipefd[0], 0);
     }
     else
     {
          dup2(pipefd[1], 1);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.