shell 脚本中,程序的标准输出重定向到 FIFO, 需要注意的问题 - yanbin's Blog

shell 脚本中,程序的标准输出重定向到 FIFO, 需要注意的问题

yanbin posted @ 2015年6月28日 18:44 in Programming with tags programming linux shell , 3658 阅读
FIFO, 又称为命名管道 (named pipes)。与 PIPE 不同和相同之处:
1. PIPE 只能用于关联进程之间,比如: 父子进程之间,两个子进程之间。
2. 关于读写:
   (a) 写一个没有读端的 PIPE,会触发 SIGPIPE(Broken pipe: write to pipe with no readers).
   (b) 写一个没有读端的 FIFO,没有设置 O_NONBLOCKING 会阻塞,直到有进程读;
        设置了 O_NONBLOCKING, 将会返回 -1.
   (c) 没有写端时读 PIPE/FIFO 都是会阻塞,而设置了 O_NONBLOKING 读都会返回 -1.
 
由于 FIFO 没有读端写端会阻塞的特性。
在 shell 脚步中,一个程序的标准输出重定向到 FIFO, 即写 FIFO 的程序启动之时或之后,
确保读 FIFO 的程序是否存在并且及时存在了,否则可能会有如下问题:
1. 读 FIFO 的程序没有启动过,写 FIFO 的程序永远阻塞在 FIFO 操作上。
2. 读 FIFO 的程序后来启动了,写 FIFO 的程序可能丢失了大量数据当其阻塞在写 FIFO 操作之上时。
3. 假如写 FIFO 的程序是一个多线程程序,她的产生数据操作写数据操作是分开,并且有内部数据缓存,
   当读 FIFO 的程序启动较晚时,会有大量数据要读,而且写 FIFO 程序也有大量数据要写,
   这至少会耗费时间。
4. 实在要考虑写 FIFO 程序读 FIFO 程序的启动先后顺序问题,要考虑时间窗口。
5. Linux Programmer's Manual, pipe(7) 给出的设计思想:
   Applications should not rely on a particular capacity: an application should be designed so that
   a reading process consumes data as soon as it is available, so that a writing process does not
   remain blocked.
Avatar_small
spanish to english 说:
2018年12月26日 10:35

This article has suggested to me many new ideas. I will embark on doing it. Hope you can continue to contribute your talents in this area. Thank you.

Avatar_small
tank trouble 说:
2019年4月16日 09:58

This is a great little post with some valuable tips. I totally agree. The way you bring passion and engagement into the things you do can really change your outlook on life.

Avatar_small
remove password in w 说:
2019年5月13日 22:30

If you have any password like problem in window 10 you can visit a fabulous site where 
we can remove your password in window 10.

Avatar_small
starjack io 说:
2019年10月17日 12:12

I think this is an informative post and it is very useful and knowledgeable. I really enjoyed reading this post. big fan, thank you!

Avatar_small
t rex game 说:
2020年2月11日 16:54

The information is very special, I will have to follow you, the information you bring is very real, reflecting correctly and objectively, it is very useful for society to grow together.

Avatar_small
Emma 说:
2022年12月09日 21:01

This post shares the details regarding an issue that is related to the shell script. the t score osteoporosis standard output of the program is redirected to FIFO and the issue needs more attention. Hope that the experts will provide answers to this issue and keep sharing more updates here.

Avatar_small
pavzi.com 说:
2024年1月09日 14:23

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us. pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee