Computer Science, asked by msivasaikrishna123, 4 months ago

Explain lpc mechanisms 1 pipes 2 FIFOS

Answers

Answered by argon85
1
FIFOS (also called named pipes) are a mechanism that allow for IPC that's similar to using regular files, except that
the kernel takes care of synchronizing reads and writes, and
data is never actually written to disk (instead it is stored in buffers in memory) so the overhead of disk I/O (which is huge!) is avoided.
A FIFO is part of the file system, i.e. it has a name and path just like a regular file. Programs can open it for reading and writing, just like a regular file. However, the name is simply a convenient reference for what is actually just a stream of bytes, with no persistent storage or ability to move backwards of jump forward in the stream. Each byte written is read exact once in a First-In-First-Out fashion: hence the name FIFO. If a read is performed on a FIFO and the writer has not yet written a byte, the kernal actually puts the reading process to sleep until data is available to read (just as it does when you read from the terminal). If the writer has written so much data that the FIFO's buffer is full, it is put to sleep until some reader process has read some bytes, thereby making room in the buffer.
FIFO's can be created from the command-line with the mkfifo utility. Here's a fun game to try to see fifos at work:

Open up two terminals (we'll refer to them as terminal 1 and terminal 2) with both in your home directory.
In terminal 1, give the command mkfifo foo to create a fifo foo in your home directory.
In terminal 1, give the command cat > foo
In terminal 2, give the command tr ' ' 'x' < foo
In terminal 1 type a line like: the rain in spain falls mainly on the plain, hit enter and see what happens in terminal 2.
Try a few more lines out, then give ctrl-d in terminal 1. See what happens?
If you do ls foo, you'll see that foo is still there. Go ahead and remove it with rm.
FIFOs can be used for IPC (as indeed it was in the above example), but they suffer from some of the same problems as IPC with regular files, even as they address others. We still need to agree on a name ahead of time and ensure that the communicating processes know what it is. And the FIFO is still a name in the file system, so we have the same problem that several processes using the same FIFO might interfere with one another, and that if someone removes the FIFO, it could be catastrophic for processes that depend on that FIFO for communication.
Similar questions