Multiple pipe implementation using system call fork() execvp() wait() pipe() - it is simply not working

前端 未结 1 933
情话喂你
情话喂你 2020-12-21 02:30

I need to implement my shell that handles multiple pipe commands. For example I need to be able to handle this: ls | grep -i cs340 | sort | uniq | cut -c 5. I a

相关标签:
1条回答
  • 2020-12-21 02:35

    I suggest a new strategy, R2:

    function do(commands)
        if commands is of size 1
            exec commands[0] || die
        split commands into c1 (first command) c2 (the rest of them)
        open
        if fork 
            close input end of pipe
            dup output of pipe to stdin
            do (c2) || die
        close output end of pipe
        dup input of pipe to stdout
        exec c1 || die
    

    Using a recursive function, especially if you're maintaining a list, will help you simplify your logic. You don't really have to worry about stack depth here, as your whole address space will be overwritten anyway.

    In other news, from the man page:

    After a successful return from one of these system calls, the old and new file descriptors may be used interchangeably. They refer to the same open file description (see open(2)) and thus share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the descriptors, the offset is also changed for the other.

    Which meanse when you say you're closing both ends of the pipe? You really are closing it - it AND the standard in/out that your program is intending on using.

    --> MUCH LATER EDIT <--

    As Jonathan Leffler pointed out, the above information is in correct. I have confirmed it with the following program:

    #include <unistd.h>
    
    int main(){
        dup2(0, 7);
        write(7, "Hey, 1\n", 7);
        close(0);
        write(7, "Hey, 2\n", 7);
        close(7);
        write(7, "Hey, 3\n", 7);
    }
    

    Which results in the following output:

    $ gcc dup2Test.c && ./a.out
    Hey, 1
    Hey, 2
    

    Thanks, Jonathan!

    0 讨论(0)
提交回复
热议问题