Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I echo something to a named pipe made with mkfifo, it hangs if no process is reading from that pipe. How can I prevent that, i.e., stop that from hanging?

And by the way, why is that hanging echo process not listed in ps -e or pstree?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

It's not totally clear what your desired end result is from the question, so I will assume that your desire is to continue the program, but still have the write occur when possible. In that case, just background the operation:

echo foo > fifo &

As for why echo is not listed in the process table, it's because echo is a shell builtin.

share|improve this answer
1  
I don't think this is a reasonable answer. It will just result in a huge number of stuck echo processes lying around. (Unlike in OP's original example, they will be separate processes since they're backgrounded.) Instead some mechanism is needed to immediately produce a failure when no reader is present. –  R.. May 12 at 13:33
    
I would posit that your suggestion is far more unreasonable -- it requires storing state that never needed to be stored. The most reasonable solution entirely depends on what the expected behaviour is. –  Chris Down May 12 at 14:03
    
Storing state? You mean the absence/presence of a reader? That's part of the state of the fifo; any attempt to store it outside would be bogus and would be subject to race conditions. –  R.. May 12 at 14:33
    
Also, OP wants to buffer data in the pipe (until it fills up) rather than blocking when there's no reader, an easy way to achieve this is simply to have the script itself hold the fifo open for reading, but never actually read from it. (e.g. exec 3<fifo). –  R.. May 12 at 14:34
1  
@Susei If that's what you want, just use timeout from GNU coreutils, or cratimeout, or SIGALRM. I would not suggest writing your own timeout logic. I would also suggest clarifying your question so your intent is more obvious to readers -- at least to me, it reads as a desire not to block rather than a desire not to write at all. –  Chris Down May 12 at 15:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.