Reputation: 35
I would like to do the following inside a C program on a Linux os:
The idea is to circumvent any drive access for performance purposes.
I know that the creation of pipes is quite simple using the PIPE system call and that I could just use popen for creating a pipe for input OR output purposes.
But how would you go about doing this for both input and output?
Upvotes: 3
Views: 6838
Reputation: 3286
Depending on your needs, you may find it easier just to use mkfifo(1) to make a named pipe and have your processes read/write to that file. While the file is named in the filesystem, the overhead of using an anonymous pipe shouldn't be appreciable.
Upvotes: 1
Reputation: 753455
You need to be quite careful with the plumbing:
Note how many closes there are, especially in the child. If you use dup2(), you don't have to close standard input and output explicitly; however, dup() works correctly if you do the explicit closes. Also note that neither dup() nor dup2() closes the file descriptor that is duplicated. If you omit closing the pipes, then neither program can detect EOF correctly; the fact that the current process can still write to a pipe means that there is no EOF on the pipe, and the program will hang indefinitely.
Note that this solution does not alter standard error for the child; it goes to the same place as standard error for the parent. Often, this is correct. If you have a specific requirement that error messages from the child are handled differently, then take appropriate action on the child's standard error too.
Upvotes: 7
Reputation: 20456
The easiest way is probably executing /bin/sh
, this way you can use familiar shell syntax for specifying pipes and offload all complexities.
Something like:
execlp("sh", "-c", "cat /dev/zero | cat > /dev/null", NULL);
Upvotes: 0