Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't write to named pipe

I'm trying to write to a named pipe, made with mkfifo. But when I run the command, (ex) ls > myNamedPipe, I can no longer enter commands into the bash. I can still write characters and that's pretty much it.

like image 320
tay10r Avatar asked Mar 13 '13 03:03

tay10r


People also ask

Are Named Pipes blocking?

Both reading and writing are by default blocking. This means that if a process tries to read from a named-pipe that does not have data, it will block. Similarly, if a process write into a named-pipe that has not yet been opened by another process, the writer will block.

Is pipe write blocking?

Read/Write Are Blocking - when a process reads from a named pipe that has no data in it, the reading process is blocked. It does not receive an end of file (EOF) value, like when reading from a file.

Are Named Pipes permanent?

A named pipe, however, can last as long as the system is up, beyond the life of the process. It can be deleted if no longer used. Usually a named pipe appears as a file, and generally processes attach to it for IPC.


1 Answers

A named pipe remains opened until you read it from some other place. This is to permit communication between different processes.

Try:

mkfifo fifo
echo "foo" > fifo

Then open another terminal and type:

cat fifo

If you return to you first terminal, you'll notice that you can now enter other commands.

See also what happends with the reverse :

# terminal 1
cat fifo

# terminal 2
echo "foo" > fifo

# and now you can see "foo" on terminal 1

If you want you terminal not to "hang on" when trying to write something to the fifo, attach to the fifo a file descriptor :

mkfifo fifo
exec 3<> fifo
echo "foo" > fifo
echo "bar" > fifo
like image 146
michaelmeyer Avatar answered Sep 21 '22 12:09

michaelmeyer