[Home] [Credit Search] [Category Browser] [Staff Roll Call] | The LINUX.COM Article Archive |
Originally Published: Thursday, 23 August 2001 | Author: Subhasish Ghosh |
Published to: develop_articles/Development Articles | Page: 4/5 - [Printable] |
Understanding Linux Kernel Inter-process Communication: Pipes, FIFO & IPC (Part 1)
In this article, part one of a two part article, the prolific and talented Subhasish returns to give Linux.com readers another trip into understanding Linux kernel behavoir and programming. There's a lot of information covered here for free, so hang up your hat and have fun. Part 2 of Understanding Linux Kernel Inter-process Communication will be published tomorrow.
|
<< Page 4 of 5 >> | |
FIFOsA couple of weeks back one of my friends asked: "Is 'FIFO' same as a 'pipe'"? She appeared to be confused. No, pipes and FIFOs are different entities. Just as I had discussed "processes" and "threads" earlier. So, though pipes and FIFOs are related, but NO, they are not the same entities. So, the question that comes up is: What are FIFOs? And how are they "better" than pipes? Although pipes are a simple, flexible and efficient communication mechanism, they suffer from one major drawback. There is no way to open an already existing pipe. This makes it impossible for two arbitrary processes to share the same pipe. So far, we have only been able to pass data between programs that are related, i.e. programs that have been started from a common ancestor process. But what if two unrelated processes want to be able to exchange data? We do this using FIFOs, often referred to as "named pipes". A named pipe is a special file that behaves like the unnamed pipes we have seen so far. Also called FIFOs (i.e. "first in, first out"; the first byte written into the special file is also the first byte that will be read).FIFO files are similar to device files. They have a disk
inode, but they do not make use of data blocks. FIFOs are similar
to unnamed pipes in that they also include a kernel buffer to
temporarily store the data exchanged by two or more processes.
Now the question is, how to create a "named pipe"? We
can create named pipes from the command line and from within a
program. But the creation of named pipes can be a bit confusing.
But don't worry, read on. A process creates a FIFO by issuing a
Note: All readers should note that some
older versions of UNIX only have the
Since I like dealing with POSIX standards, and also as Linux
supports
When we compile and run this program, it creates a named pipe for us (look out for the output "FIFO created successfully" on the screen). We can look for the pipe with the command:
Notice that the first character is a 'p',
indicating a pipe. The '|' symbol at the end is
added by the If we wish to pass data in both directions between programs,
it's much better to use either a pair of FIFOs or pipes, one for
each direction. The main difference between opening a FIFO and a
regular file is the use of the
In case of 1), the
| |
<< Page 4 of 5 >> |