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

The Archive Base Latest Questions

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

This is a followup to my previous question . I am writing a linux

  • 0

This is a followup to my previous question. I am writing a linux shell, and so I need to deal with the possibility of users inputting multiple pipe commands. It is almost working correctly, except for after calling execvp() on the last command the I/O hangs. My prompt never reappears and I have to ctrl+C to leave the shell. I do not think it is an infinite loop happening, but rather I am not closing my streams correctly. I cannot figure out the correct way to do it.

Example – If I do not use a pipe at all, the shell runs correctly:

ad@ubuntu:~/Documents$ gcc mash.c -o mash
ad@ubuntu:~/Documents$ ./mash
/home/ad/Documents> ls
a.out     bio1.odt  blah.cpp       controller.txt  mash.c
bio1.doc  blahblah.txt  Chapter1Notes.odt  mash
/home/ad/Documents> 

However, if I type in:

/home/ad/Documents> ls -l | grep sh
-rwxr-xr-x 1 ad ad  13597 2011-09-26 00:03 mash
-rw-r--r-- 1 ad ad   3060 2011-09-25 23:58 mash.c

The prompt does not appear again. main() originally calls execute() with stdin and stdout.

Thanks for your time!

Code:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

int MAX_PATH_LENGTH = 1024; //Maximum path length to display.
int BUF_LENGTH = 1024; // Length of buffer to store user input
char * delims = " \n"; // Delimiters for tokenizing user input.
const int PIPE_READ = 0;
const int PIPE_WRITE = 1;

void execute(char **argArray, int read_fd, int write_fd){
    dup2(read_fd, 0);
    dup2(write_fd, 1);
    //Problem when entering only newline character
    char **pA = argArray;
    int i = 0;
    while(*pA != NULL) {
        if(strcmp(argArray[i],"<") == 0) { 
            int input = open(argArray[i+1], O_RDWR | O_CREAT);
            pid_t pid = fork();
            if(pid == 0) {
                dup2(input, 0);
                argArray[i] = 0;
                execvp(argArray[0], &argArray[0]);
                printf("Error redirecting input.\n");
                exit(1);
            }
            wait(pid);
        }
        else if(strcmp(argArray[i],">") == 0) { 
            int output = open(argArray[i+1], O_RDWR | O_CREAT);
            pid_t pid = fork();
            if(pid == 0){
                dup2(output,1);
                close(output);
                argArray[i] = 0;
                execvp(argArray[0], &argArray[0]);
                printf("Error redirecting output.\n");
                exit(1);
            }
            close(output);
            wait(NULL);

        }
        else if(strcmp(argArray[i],"|") == 0) {
            int fds[2];
            pipe(fds);
            pid_t pid = fork();
            if(pid == 0) {
                dup2(fds[PIPE_WRITE], 1);
                close(fds[PIPE_READ]);
                close(fds[PIPE_WRITE]);
                argArray[i] = 0;
                execvp(argArray[0], &argArray[0]);       
                printf("%s: command not found.\n", argArray[0]);     
                exit(1);
            } else {
                dup2(fds[PIPE_READ], 0);
                execute(&argArray[i+1], 0, 1);
                close(fds[PIPE_READ]);
                close(fds[PIPE_WRITE]);
                wait(pid);
                printf("herp\n");
            }
        }
        *pA++;
        i++;
    }
    pid_t pid = vfork();
    if(pid == 0){
        execvp(argArray[0], &argArray[0]);
        printf("%s: command not found.\n", argArray[0]);
        exit(1);
    }
    else {
        wait(NULL);
    }
}

int main () {

    char path[MAX_PATH_LENGTH];
    char buf[BUF_LENGTH];
    char* strArray[BUF_LENGTH];
    /**
     * "Welcome" message. When mash is executed, the current working directory
     * is displayed followed by >. For example, if user is in /usr/lib/, then
     * mash will display :
     *      /usr/lib/> 
     **/
    getcwd(path, MAX_PATH_LENGTH);
    printf("%s> ", path);
    fflush(stdout);

    /**
     * Loop infinitely while waiting for input from user.
     * Parse input and display "welcome" message again.
     **/ 
    while(1) {
        fgets(buf, BUF_LENGTH, stdin);
        char *tokenPtr = NULL;
        int i = 0;
        tokenPtr = strtok(buf, delims);

        if(strcmp(tokenPtr, "exit") == 0){

            exit(0);
        }
        else if(strcmp(tokenPtr, "cd") == 0){
            tokenPtr = strtok(NULL, delims);
            if(chdir(tokenPtr) != 0){
                printf("Path not found.\n");
            }
            getcwd(path, MAX_PATH_LENGTH);
        }
        else if(strcmp(tokenPtr, "pwd") == 0){
            printf("%s\n", path);
        }
        else {
            while(tokenPtr != NULL) {
                strArray[i++] = tokenPtr;
                tokenPtr = strtok(NULL, delims);
            }
            execute(strArray, 0, 1);
        }

    bzero(strArray, sizeof(strArray)); // clears array
    printf("%s> ", path);
    fflush(stdout);
    }

}
  • 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-25T21:39:26+00:00Added an answer on May 25, 2026 at 9:39 pm

    This line – dup2(fds[PIPE_READ], 0); – overwrites your current stdin file descriptor with a descriptor referring to the pipe. Once the pipe command completes, any attempt to read from stdin will fail.

    This line – fgets(buf, BUF_LENGTH, stdin); – doesn’t check for error conditions.

    Finally – you wait for the second process in the pipe to finish before you have started the second one. This is what is causing your deadlock; the “grep” command is waiting for input, but you haven’t exec’d the “ls” command yet. You wait for the grep command to finish, but it can’t finish because it is waiting for input.

    In your latest incarnation of the code: When the execute() function is called, it scans the arguments and finds a pipe; it then forks and runs the first command (“ls”):

            pid_t pid = fork();
            if(pid == 0) {
                dup2(fds[PIPE_WRITE], 1);
                close(fds[PIPE_READ]);
                close(fds[PIPE_WRITE]);
                argArray[i] = 0;
                execvp(argArray[0], &argArray[0]);       
                printf("%s: command not found.\n", argArray[0]);     
                exit(1);
    

    Then it recurses, calling execute() again:

            } else {
                dup2(fds[PIPE_READ], 0);
                execute(&argArray[i+1], 0, 1);  // <--- HERE
    

    … this will of course fork and run “grep” before returning. Note that it does so with /both/ the pipe filed descriptors /open/. Therefore, the grep process itself will hold both ends of the pipe open. execute() performs a wait(NULL) before returning; this however will actually wait for the “ls” to finish (since that is the process that completes first). Then it returns, and proceeds:

                close(fds[PIPE_READ]);
                close(fds[PIPE_WRITE]);
                wait(pid);   // <--- WRONG, compile with -Wall to see why
                printf("herp\n");
            }
    

    I’ve pointed out one error. Try compiling with “-Wall”, or reading the documentation for the wait() function! If you change it to wait(NULL) it will be correct, however, in that case it will block. The reason is that the “grep” command hasn’t completed and is still reading input. The reason it is still reading input is because the grep process itself has the write end of the pipe open!! So, grep never sees the “end” of the input coming from the pipe. A simple fix is to close the pipe fds before calling execute() recursively (there remains other problems with your code however, including, as I have already pointed out, that you are trashing your stdin descriptor).

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

Sidebar

Related Questions

this is a followup to my previous question , whereby I'm writing a PHP
THis is a followup to my previous question Font-dependent control positioning. It's an attempt
This is a followup on the question: ASP.NET next/previous buttons to display single row
Hey guys this is a followup to my previous question . I now have
This is somewhat of a followup to my previous question , where people pointed
This question is a followup to my previous one: Previous Questions . So I
A followup to this previous question: Current code: var query = from b in
So, this is something of a followup to my previous question. I'm running into
This is a followup on my previous question , I am using the 2to3
This question is a followup to a previous question I had about discovering unused

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.