Score:1

How to keep writing in a FIFO even if there's no readers

cn flag

There's a service that pipes it's output to a named pipe, which I create with mkfifo name.

The service pauses if there's no reader in pipe. How to make it to keep piping the data even if there's no reader?

The reader(s) may (or may not) use the pipe later.

Edit: No buffer is needed. Readers will start reading from where they use the pipe. Data before that is lost. And that's OK. Consider it's a video stream.

hr flag
AFAIK you're always going to be limited by buffer size - see for example [Buffering (named) pipe in GNU OS](https://unix.stackexchange.com/questions/164290/buffering-named-pipe-in-gnu-os) and [Non-blocking buffered named pipe?](https://unix.stackexchange.com/questions/23488/non-blocking-buffered-named-pipe)
james hofer avatar
cn flag
I don't need to buffer data. Readers will lose what they lose. But that's no problem. They get the data from where they start reading.
james hofer avatar
cn flag
I think I should attach some sort of dummy reader to the pipe as soon as it's created, so the process won't pause, isn't it?
Doug Smythies avatar
gn flag
The first link in @steeldriver s comment explains how to prevent the writer being blocked by no reader. I tried it and it works great.
james hofer avatar
cn flag
@DougSmythies I checked the link. It fills pipe buffer and after that no write occurs. I'm not a Linux expert really. After `mkfifo something`, what command(s) should I use to handle this case?
Raffa avatar
jp flag
`tail -F namedpipe` might be of interest to you in this case. See for example https://askubuntu.com/a/1416100
james hofer avatar
cn flag
@Raffa I tried to use `tail -F my_pipe` after creating the pipe and it's working! But one question, does it consume high memory or any resource in scale?
Raffa avatar
jp flag
You can, if you want, redirect its output to e.g. `/dev/null` and it should use very minimal resources AFAIK.
james hofer avatar
cn flag
@Raffa Could you post your comments as an answer?
Score:1
jp flag
tail -F namedpipe

might be of interest to you in this case … It should keep the pipe open and writable for your service constantly.

You can as well, if you want, discard its output to minimize used resources by redirecting it to e.g. /dev/null like so:

tail -F namedpipe > /dev/null
Score:0
cd flag

Use socat to extract the data from the socket as soon as possible, put it into a file for example, and then read from it in another socat to another socket:

$ socat gopen:socket1 create:file1 
$ mkfifo socket2
$ socat gopen:file1 gopen:socket2

You might play with multiple socat options.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.