shell/文件系统中的非阻塞/异步 fifo/命名管道?
有没有办法在 shell 中创建非阻塞/异步命名管道或类似的东西?这样程序就可以在其中放置行,这些行将保留在 ram 中,并且当某些程序可以从管道中读取一些行,同时将未读取的内容保留在 fifo 中时?程序也很可能同时写入和读取该 fifo。起初,我认为也许这可以使用文件来完成,但在网上搜索了一下后,文件同时读写的事实似乎没有什么好处。命名管道几乎可以工作,只是有两个问题:第一,如果另一端没有人,它们会阻止读/写;第二,即使我让写入被阻止并设置两个进程在没有人读取时写入管道,通过尝试为每个进程写入一行,然后尝试 head -n 1
我只得到我需要的一行,但是两个写入进程都终止,并且第二行丢失。有什么建议吗?
编辑:也许可以使用一些中间程序来帮助解决这个问题,充当作者和读者之间的调解人?
Is there a way to create non blocking/asynchronous named pipe or something similar in shell? So that programs could place lines in it, those lines would stay in ram, and when some program could read some lines from pipe, while leaving what it did not read in fifo? It is also very probable that programs can be writing and reading to this fifo at the same time. At first I though maybe this could be done using files, but after searching a web for a bit it seems nothing good can come from the fact that file is read and written at same time. Named pipes would almost work, just there are two problems: first they block reads/writes if there is no one at the other end, second even if I let writing to blocked and set two processes to write to pipe while no one is reading, by trying to write one line with each process, and then try head -n 1 <fifo>
I get just one line as I need, but both writing processes terminate, and second line is lost. Any suggestions?
Edit: maybe some intermediate program could be used to help with this, acting like mediator between writers and readers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您可以使用特殊程序 - 缓冲区。缓冲区旨在尝试使写入端持续忙碌,以便在写入磁带驱动器时可以进行流式传输,但您也可以将其用于其他目的。内部缓冲区是一对通过共享内存中保存的大型循环队列进行通信的进程,因此您的进程将异步工作。如果队列已满,您的读取器进程将被阻塞,而写入器进程将被阻塞,如果队列为空。示例:
bzcat archive.bz2 |缓冲区 -m 16000000 -b 100000 |处理脚本 | bzip2 > archive_processed.bz2
http://linux.die.net/man/1/buffer
You can use special program for this purpose - buffer. Buffer is designed to try and keep the writer side continuously busy so that it can stream when writing to tape drives, but you can use for other purposes. Internally buffer is a pair of processes communicating via a large circular queue held in shared memory, so your processes will work asynchronously. Your reader process will be blocked in case the queue is full and the writer process - in case the queue is empty. Example:
bzcat archive.bz2 | buffer -m 16000000 -b 100000 | processing_script | bzip2 > archive_processed.bz2
http://linux.die.net/man/1/buffer