named pipes

轉載自https://www.linuxjournal.com/content/using-named-pipes-fifos-bash

It's hard to write a bash script of much import without using a pipe or two. Named pipes, on the other hand, are much rarer.

Like un-named/anonymous pipes, named pipes provide a form of IPC (Inter-Process Communication). With anonymous pipes, there's one reader and one writer, but that's not required with named pipes—any number of readers and writers may use the pipe.

Named pipes are visible in the filesystem and can be read and written just as other files are:

$ ls -la /tmp/testpipe
prw-r--r-- 1 mitch users 0 2009-03-25 12:06 /tmp/testpipe|

Why might you want to use a named pipe in a shell script? One situation might be when you've got a backup script that runs via cron, and after it's finished, you want to shut down your system. If you do the shutdown from the backup script, cron never sees the backup script finish, so it never sends out the e-mail containing the output from the backup job. You could do the shutdown via another cron job after the backup is "supposed" to finish, but then you run the risk of shutting down too early every now and then, or you have to make the delay much larger than it needs to be most of the time.

Using a named pipe, you can start the backup and the shutdown cron jobs at the same time and have the shutdown just wait till the backup writes to the named pipe. When the shutdown job reads something from the pipe, it then pauses for a few minutes so the cron e-mail can go out, and then it shuts down the system.

Of course, the previous example probably could be done fairly reliably by simply creating a regular file to signal when the backup has completed. A more complex example might be if you have a backup that wakes up every hour or so and reads a named pipe to see if it should run. You then could write something to the pipe each time you've made a lot of changes to the files you want to back up. You might even write the names of the files that you want backed up to the pipe so the backup doesn't have to check everything.

Named pipes are created via mkfifo or mknod:

$ mkfifo /tmp/testpipe
$ mknod /tmp/testpipe p

The following shell script reads from a pipe. It first creates the pipe if it doesn't exist, then it reads in a loop till it sees "quit":

#!/bin/bash

pipe=/tmp/testpipe

trap "rm -f $pipe" EXIT

if [[ ! -p $pipe ]]; then
    mkfifo $pipe
fi

while true
do
    if read line <$pipe; then
        if [[ "$line" == 'quit' ]]; then
            break
        fi
        echo $line
    fi
done

echo "Reader exiting"

The following shell script writes to the pipe created by the read script. First, it checks to make sure the pipe exists, then it writes to the pipe. If an argument is given to the script, it writes it to the pipe; otherwise, it writes "Hello from PID".

#!/bin/bash

pipe=/tmp/testpipe

if [[ ! -p $pipe ]]; then
    echo "Reader not running"
    exit 1
fi


if [[ "$1" ]]; then
    echo "$1" >$pipe
else
    echo "Hello from $$" >$pipe
fi

Running the scripts produces:

$ sh rpipe.sh &
[3] 23842
$ sh wpipe.sh
Hello from 23846
$ sh wpipe.sh
Hello from 23847
$ sh wpipe.sh
Hello from 23848
$ sh wpipe.sh quit
Reader exiting

Note: initially I had the read command in the read script directly in the while loop of the read script, but the read command would usually return a non-zero status after two or three reads causing the loop to terminate.

while read line <$pipe
do
    if [[ "$line" == 'quit' ]]; then
        break
    fi
    echo $line
done

 

---------------------------------------------------------------------------------------


轉載自https://www.howtoforge.com/linux-mkfifo-command/

Linux mkfifo Command Tutorial for Beginners (with Examples)

If you're even a moderate Linux command line user, you must be aware of pipes, a fundamental command line feature that allows processes to communicate. Then there's a concept of named pipes (yeah, pipes with names, so that you can do more with pipes). The mkfifo command lets you create such named pipes.

In this tutorial, we will discuss the basics of mkfifo using some easy to understand examples. But before we do that, it's worth mentioning all examples here have been tested on Ubuntu 16.04 LTS.

Linux mkfifo command

The mkfifo command basically lets you create FIFOs (a.k.a named pipes). Following is the syntax of the command:

mkfifo [OPTION]... NAME...

And here's how the tool's man page explains it:

Create named pipes (FIFOs) with the given NAMEs.

Following are some Q&A-styled examples that should give you a good idea on how mkfifo works.

Q1. What exactly are named pipes?

To understand this, you should first be aware of the concept of basic pipes. You'd have seen commands that contain a vertical bar (|) in them. This bar is called a pipe. What it does is, it creates a channel of communication between the two processes (when the complete command is executed).

For example:

ls | grep .txt

The command mentioned above consists of two programs: ls and grep. Both these programs are separated by a pipe (|). So what pipe does here is, it creates a channel of communication between these programs - when the aforementioned command is executed, the output of ls is fed as input to grep. So finally, the output that gets displayed on the terminal consists of only those entries that have '.txt' string in them.

So that was a quick refresher of normal pipes. Now comes the concept of named pipes. As the name itself suggests, these are pipes with names. You can create a named pipe using the mkfifo command. For example:

mkfifo pipe2

So 'pipe2' is now a named pipe. Now comes the question how named pipes are more useful? Well, consider the case where you have a process running in a terminal and producing output, and what you want is to channelize that output on to a different terminal. So here, a named pipe could of great help.

For example, suppose ls is the process running in the first terminal, and you want to see its output in a different terminal.. So here's what you can do:

ls > pipe2

and here's what you can do in the second terminal:

cat < pipe2

What exactly are named pipes

Q2. How to identify named pipes?

Named pipes can be accessed normally like files. So that means you can use the ls command to access them. If you see the access permissions for a named pipe, you'll see a 'p' in the beginning. This signifies the file in question is a named pipe. Here's an example:

How to identify named pipes

Q3. How to set custom access permissions?

As you can see in the previous Q&A, the default access permissions for named pipes is 'rw', 'rw', and 'r' (for user, group, and others, respectively). However, if you want, you can set custom permissions as well, something which you can do using the -m option.

For example:

mkfifo pipe3 -m700

The following screenshot confirms custom permissions were set:

How to set custom access permissions

To know more about mkfifo, you can use the --help and --version options.

Conclusion

So depending upon what kind of work you do on the Linux command line, the mkfifo command can prove to be very useful to you. Once you're done with the basic usage we've discussed here, you can learn more about the tool by heading to its man page.

 

命名管道"或"命名管线"(Named Pipes)是一种简单的进程间通信(I P C)机制,   Microsoft Windows NT,Windows 2000,Windows 95以及Windows 98均提供了对它的支持   (但不包括Windows CE).命名管道可在同一台计算机的不同进程之间,或在跨越一个网络的   不同计算机的不同进程之间,支持可靠的,单向或双向的数据通信.用命名管道来设计应用   程序实际非常简单,并不需要事先深入掌握基层网络传送协议(如T C P / I P或I P X)的知识.这   是由于命名管道利用了微软网络提供者(M S N P)重定向器,通过一个网络,在各进程间建立   通信.这样一来,应用程序便不必关心网络协议的细节.之所以要用命名管道作为自己的网   络通信方案,一项重要的原因是它们充分利用了Windows NT及Windows 2000内建的安全特   性.   这里有一个可采纳命令管道的例子.假定我们要开发一个数据管理系统,只允许一个指   定的用户组进行操作.想像在自己的办公室中,有一部计算机,其中保存着公司的秘密.我   们要求只有公司的管理人员,才能访问及处理这些秘密.假定在自己的工作站机器上,公司   内的每名员工都可看到网络上的这台计算机.然而,我们并不希望普通员工取得对机密材料   的访问权.在这种情况下,命名管道可发挥出很好的作用,因为我们可开发一个服务器应用   程序,令其以来自客户机的请求为准,对公司的秘密进行安全操作.服务器可将客户访问限   制在管理人员身上,用Windows NT或新版Windows 2000自带的安全机制,便可非常轻松地做   到这一点.   在此要记住的一个重点是,将命名管道作为一种网络编程方案使用时,它实际上建立一   个简单的客户机/服务器数据通信体系,可在其中可靠地传输数据.本章将介绍如何来开发   一个命名管道客户机及服务器应用.首先要解释的是命名管道的命名规范(约定),然后介绍   基本的管道类型.随后,将向大家展示如何实现一个简单的服务器应用.然后以它为基础,   深入探讨高级的服务器编程技术.接下来,讲解如何开发一个简单的客户机应用.到本章末,   我们会对命名管道已知的所有问题及限制进行总结.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值