开发者

I/O hangs after recursive pipe() calls

开发者 https://www.devze.com 2023-04-08 21:22 出处:网络
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

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);
    }

}


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).


These two lines have the arguments in the wrong order:

dup2(0, read_fd);
dup2(1, write_fd);

You should be writing:

dup2(read_fd, 0);
dup2(write_fd, 1);

Or:

dup2(read_fd,  STDIN_FILENO);
dup2(write_fd, STDOUT_FILENO);

However, whether amended or original, the call to execute is:

 execute(strArray, 0, 1);

which means that these two dup2() calls do nothing (duplicating 0 to 0 and 1 to 1).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号