Search code examples
linuxbashshellpipelinefile-descriptor

Pipeline management in linux shell


i'm currently looking how pipelining is managed into shells. for example, in my shell, if i enter "ls | wc | less". The result of this operation will be the creation of three process, ls wc and less. Ouput of ls will be piped to the enter input of wc, and the ouput of wc will be piped to the enter intput of less.

For me, it means that during the execution of "ls | wc | less". The standard input of less will not be the keyboard, but the ouput of wc. But, less will still be responsive to my keyboard. Why ? I don't understand, because for me, less should not be sensitive to the keyboard since it have been piped.

Do somebody have an idea ? Thanks


Solution

  • The code from less

    #if HAVE_DUP
        /*
         * Force standard input to be the user's terminal
         * (the normal standard input), even if less's standard input 
         * is coming from a pipe.
         */
        inp = dup(0);
        close(0);
    #if OS2
        /* The __open() system call translates "/dev/tty" to "con". */
        if (__open("/dev/tty", OPEN_READ) < 0)
    #else
        if (open("/dev/tty", OPEN_READ) < 0)
    #endif
            dup(inp);
    #endif
    

    It opens a direct stream from /dev/tty as well as whatever your stdin is.