shell 命名管道,进程间通信, ncat作http server
命名管道也被称为FIFO文件, 在文件系统中是可见的,并且跟其它文件一样可以读写!
命名管道特点:
当写进程向管道中写数据的时候,如果没有进程读取这些数据,写进程会堵塞
当读取管道中的数据的时候,如果没有数据,读取进程会被堵塞
当写进程堵塞的时候,有读进程读取数据,那么写进程恢复正常
当读进程堵塞的时候,如果写进程写了数据,那么读进程会读取数据,然后正常执行后面的代码
# 写进程堵塞的情况 [[email protected]_10.2.1.242 test]$ echo 1 >p & [1] 17091 [[email protected]_10.2.1.242 test]$ jobs [1]+ Running echo 1 > p & [[email protected]_10.2.1.242 test]$ cat p 1 [1]+ Done echo 1 > p [[email protected]_10.2.1.242 test]$ jobs [[email protected]_10.2.1.242 test]$ # 读进程堵塞的情况 [[email protected]_10.2.1.242 test]$ cat p & [1] 17351 [[email protected]_10.2.1.242 test]$ jobs [1]+ Running cat p & [[email protected]_10.2.1.242 test]$ echo 2 > p 2 [[email protected]_10.2.1.242 test]$ jobs [1]+ Done cat p命名管道的作用,不同的进程之间通信,比如在后台执行一个备份进程,,然后执行另外一个进程,等待备份完成才会处理想对应的事情!
创建管道的命令: $ mkfifo /tmp/testpipe $ mknod /tmp/testpipe p 下面是命名管道的一个应用例子: reader.sh读取管道的内容,代码如下: #!/bin/bash # filename: reader.sh # 逐行读取管道中的内容 pipe=http://www.mamicode.com/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 else echo $line fi fi done echo "Stop reader...." writer.sh写数据到管道,代码如下: #!/bin/bash # writer.sh # 把当前进程的pid写到管道 pipe=http://www.mamicode.com/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 reader和writer调用的例子: [[email protected]_10.2.1.242 test]$ sh reader.sh & [1] 17053 [[email protected]_10.2.1.242 test]$ sh writer.sh test test [[email protected]_10.2.1.242 test]$ sh writer.sh Hello from 17057 [[email protected]_10.2.1.242 test]$ sh writer.sh quit stop Reader [[email protected]_10.2.1.242 test]$ sh writer.sh quit Reader not running [1]+ Done sh reader.sh [[email protected]_10.2.1.242 test]$ sh writer.sh quitshell 中的$$是当前进程的进程ID
Ncat作HTTP server:
https://github.com/sunsky/Bash-NetCat-HTTPD/
shell 命名管道,进程间通信, ncat作http server
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/39685.html
- 上一篇:HTML前端命名规则
- 下一篇:Web核心之Cookie