|
popen and pclose:
Common use of pipes is to send data to or receive data from a program being run in a sub process. One can eliminate invoking of pipe, fork, dup2, excec, and fdopen by using the popen and pclose functions.
#include
#include
int main() {
FILE* stream = popen(“sort”,”w”);
fprintf(stream,”This is a test\n”);
fprintf(stream,“Hello world\n”);
fprintf(stream,“This program is great\n”);
return pclose(stream);
}
The call to popen creates a child process executing the sort command, replacing call to pipe, fork, dup2 and execlp. The second argument, “w”, indicates that this process wants to write to the child process. The return value to popen is one end of a pipe; the other end is connected to the child process’s standard input. After the writing finishes, pclose closes the child process’s stream, waits for the process to terminate, and returns its status value.
The first argument to popen is executed as a shell command in a sub process running /bin/sh. The shell searches the PATH environment variable in the usual way to find programs to execute. If the second argument is “r”, the function returns the child process’s standard output stream so that the parent can read the output. If the second argument is “w”, the function returns the child process’s standard input stream so that the parent can send data. If an error occurs, popen returns a null pointer.
Call pclose to close a stream returned by popen. pclose waits for the child process to terminate after closing the specified stream.
FIFO:
A first-in-first-out (FIFO) file is a pipe that has a name in the file system. Any process can open or close the FIFO; the processes on either end of the pipe need to be related to each other. FIFOs are also called named pipes. You can make a FIFO using the mkfifo command. Specify the path to the FIFO on the command line. For eg. you can create a FIFO in /tmp/fifo as follows:
$ mkfifo /tmp/fifo
$ ls –l /tmp/fifo
prw-rw-rw- 1 ritcher users 0 Jan16 14:04 /tmp/fifo
ls gives p as the first character in the output, which indicates the file actually being a FIFO (named pipe). In one window, read from the FIFO by invoking the following:
$ cat < /tmp/fifo
In a second window, write to the FIFO by invoking this:
$ cat < /tmp/fifo
Then type in some lines of text. Each time you press Enter, the line of text is sent through the FIFO and appears in the first window. The FIFO can be closed by pressing Ctrl+d. Moreover the FIFO can be removed with this line:
$ rm /tmp/fifo
Creating a FIFO:
A FIFO can be created programmatically using the mkfifo function. The first argument is the path at which to create the FIFO; the second parameter specifies the pipe’s owner, group, and permissions. Because a pipe must have a reader and writer, the permission must include both read and write permissions. If the pipe cannot be created (for instance if a file with that name already exists), mkfifo returns -1. Include and if you call mkfifo.
Accessing a FIFO:
Access a FIFO just like an ordinary file. To communicate through a FIFO, one program must open it for writing, and another program must open it for reading. Either low level I/O functions (open, write, read, etc.) or C library I/O function (fopen, fprintf, fscanf, fclose etc.) may be used.
For Example: to write a buffer of data to a FIFO using low-level I/O routines, you could use this code:
int fd = open(fifo_path,O_WRONLY);
write(fd,data,data_length);
close(fd);
To read a string from FIFO using C library I/O functions, you could use this code:
FILE* fifo = fopen(fifo_path,”r”);
fscanf(fifo,”%s”, buffer);
fclose(fifo);
Differences from Windows named pipe:
Pipes in the Win32 Operating System are very similar to Linux Pipes. The main differences concern named pipes, which, for Win32, function more like sockets. Win32 named pipes can connect processes on separate computers connected via a network. On Linux, sockets are used for this purpose. Also, Win32 allows multiple reader-writer communications on a named pipe that too without interleaving data, and two-way communication can be done using pipes.
|