Example of using named pipes in Linux Bash

前端 未结 4 526
一向
一向 2020-12-02 05:36

Can someone post a simple example of using named pipes in Bash in Linux?

相关标签:
4条回答
  • 2020-12-02 06:27

    One of the best examples of a practical use of a named pipe...

    From http://en.wikipedia.org/wiki/Netcat:

    Another useful behavior is using netcat as a proxy. Both ports and hosts can be redirected. Look at this example:

    nc -l 12345 | nc www.google.com 80
    

    Port 12345 represents the request.

    This starts a nc server on port 12345 and all the connections get redirected to google.com:80. If a web browser makes a request to nc, the request will be sent to google but the response will not be sent to the web browser. That is because pipes are unidirectional. This can be worked around with a named pipe to redirect the input and output.

    mkfifo backpipe
    nc -l 12345  0<backpipe | nc www.google.com 80 1>backpipe
    
    0 讨论(0)
  • 2020-12-02 06:31

    Terminal 1:

    $ mknod new_named_pipe p
    $ echo 123 > new_named_pipe
    
    • Terminal 1 created a named pipe.
    • It wrote data in it using echo.
    • It is blocked as there is no receiving end (as pipes both named and unnamed need receiving and writing ends to it)

    Terminal 2:

    $ cat new_named_pipe
    $ 123
    $ 
    
    • From Terminal 2, a receiving end for the data is added.
    • It read the data in it using cat.
    • Since both receiving and writing ends are there for the new_named_pipe it displays the information and blocking stops

    Named pipes are used everywhere in Linux, most of the char and block files we see during ls -l command are char and block pipes (All of these reside at /dev). These pipes can be blocking and non-blocking, and the main advantage is these provides the simplest way for IPC.

    0 讨论(0)
  • 2020-12-02 06:36

    Open two different shells, and leave them side by side. In both, go to the /tmp/ directory:

    cd /tmp/
    

    In the first one type:

    mkfifo myPipe
    echo "IPC_example_between_two_shells">myPipe
    

    In the second one, type:

    while read line; do echo "What has been passed through the pipe is ${line}"; done<myPipe
    

    First shell won't give you any prompt back until you execute the second part of the code in the second shell. It's because the fifo read and write is blocking.

    You can also have a look at the FIFO type by doing a ls -al myPipe and see the details of this specific type of file.

    Next step would be to embark the code in a script!

    0 讨论(0)
  • 2020-12-02 06:38

    Here are the commands:

    $ mkfifo named_pipe
    
    $ echo "Hi" > named_pipe &
    
    $ cat named_pipe
    

    The first command creates the pipe.

    The second command writes to the pipe (blocking). The & puts this into the background so you can continue to type commands in the same shell. It will exit when the FIFO is emptied by the next command.

    The last command reads from the pipe.

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