Please enable JavaScript.
Coggle requires JavaScript to display documents.
Inter-Process Communication: Pipes (Opening a FIFO with open (open(const…
Inter-Process Communication: Pipes
Pipe
This term is used when connecting a data flow from one process to another. Generally you attach the output of one process to the input of another.
Process Pipes
The simplest way of passing data between two programs is with popen and pclose functions.
Popen
Allows a program to invoke another program as a new process and either pass data to it or receive data from it. It is invoked like this FILE
popen(const char
command, const char *open_mode);. Where command is the name of the program to run and open_mode must be "r" or "w".
Pclose
When the started with popen has finished you can close the file stream associated with it using pclose. This function will return only when the process started with popen finishes execution.
The Pipe Call
This function provides a means of passing data between two programs without the overhead of invoking a shell to interprete the requested command.
int pipe(int file_descriptor[2]); . Pipe is passed an array of two integer file descriptors. It fills the array with two new file descriptors and returns a zero. On failure returns -1 and sets errno to indicate the reason for failure.
The following are the possible errors that the pipe call can produce.
EMFILE
Too many file descriptors are in use by the process.
ENFILE
The system file table is full.
EFAULT
The file descriptor is not valid.
The two file descriptors returned are connected in a special way. Any data written to file_descriptor[1] can be read back from file_descriptor[0].
Named Pipes: FIFOs
A named pipe is a special type of file that exists as a name in the file system but behaves like the unnamed pipes.
You can create named pipes in the command line and from within a program. For command line you use mkfifo filename. And within a program you use int mkfifo (const char *filename, mode_t mode); .
You can used named pipes in command where you would usually use a filename since they appear in the filesystem.
Opening a FIFO with open
The main restriction on opening FIFOs is that a program may not open a FIFO for reading and writing.
Another difference with a regular file is the use of the open_flag with the option O_NONBLOCK. Using this open mode changes how read and write processes are processed.
open(const char *path, O_RDONLY)
The open call will block, it will not return until a process opens the same FIFO for writing.
open(const char *path, O_RDONLY | O_NONBLOCK
The open call will know succeed and return immediately even if the FIFO is not open for writing.
open(const char *path, O_WRONLY)
The open call will block until a process opens the same FIFO for reading.
open(const char *path, O_WRONLY | O_NONBLOCK)
This will return immediately but if no FIFO is open for reading then open will return an error, -1 and the FIFO wont be opened.