Named Pipe
In a Unix-like environment, a named pipe (also known as a FIFO for “First In, First Out”) can be created using the mkfifo
command. This command creates a named pipe in the filesystem, which then can be accessed by multiple processes for reading and writing. Below is an example that demonstrates how to use a named pipe.
- Creating the Named Pipe
- This command creates a named pipe called
my_pipe
in the current directory.
- Writing to the Named Pipe In one terminal, you can write data to the named pipe like this:
echo "Hello, this is a message" > my_pipe
- Note that this command will block (wait) until something reads from the pipe.
- Reading from the Named Pipe In another terminal, you can read data from the named pipe like this:
- This will output the message “Hello, this is a message” and unblock the first terminal.
Since the named pipe exists in the filesystem, it’s not limited to parent-child processes or even to a single session; any process can read from or write to it. Named pipes are particularly useful when you want to establish communication between processes that are not directly related.
Resources