How to create a linux fifo "pipe" (or something), which does not block writer and reader?
16,344
Use a fifo, but write your own reader and writer. Open the fifo with O_NONBLOCK set, and open will return immediately if no other process has the other side open. Your write command will return immediately (as requested), but the data will be lost. If you want the data to persist, use a regular file.
Comments
-
bpgergo almost 2 years
I created a fifo pipe
$ mkfifo pipename
Now if I write somthing into it, the command won't not return,
$ echo "foo" > pipename
until I read it:
$ cat < pipename foo
Also, also read command won't return until something is written to it.
Now, I would like to create a such a thing (actually, maybe this thing should not be considered to be a pipe, rather some sort of buffer) that
- reading command will return immediately, regardless of there is something in the pipe or not (if pipe is empty, then reading should return immediately with zero bytes)
- write command returns immediately
Thanks
-
bpgergo over 12 yearsThanks. I want the data to persist until the box is up. Regular file writes data to disk, doesn't it? Is there a way to achieve the same in the memory?
-
LiKao over 12 yearsYes, with a regular file the data is written to the disk. If you need some kind of buffering it is usually hard to make sure data is only stored in memory, but not on the disk (it might get swapped out). If you just are concerned about write/read cycles and speed, you could use a ramdisk (usually available under some directory on all distributions).
-
hildogjr about 6 yearsI want to use this to create a "virtual device". So I would like to create a FIFO with buffer.