English 中文(简体)
Linux Pipes as Input and Output
原标题:

I would like to do the following inside a C program on a Linux os:

  • Create a PIPE using a syscall (or 2)
  • Execute a new process using exec()
  • Connect the process s STDIN to to the previously created pipe.
  • Connect the process s output to another PIPE.

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?

最佳回答

You need to be quite careful with the plumbing:

  1. Call pipe() twice, one for pipe-to-child, one for pipe-from-child, yielding 4 file descriptors.
  2. Call fork().
  3. In child:
    • Call close() on standard input (file descriptor 0).
    • Call dup() - or dup2() - to make read end of pipe-to-child into standard input.
    • Call close() on read end of pipe-to-child.
    • Call close() on write end of pipe-to-child.
    • Call close() on standard output (file descriptor 1).
    • Call dup() - or dup2() - to make write end of pipe-from-child into standard output
    • Call close() on write end of pipe-from-child.
    • Call close() on read end of pipe-from-child.
    • Execute the required program.
  4. In parent:
    • Call close on read end of pipe-to-child.
    • Call close on write end of pipe-from-child.
    • Loop sending data to child on write end of pipe-to child and reading data from child on read end of pipe-from-child
    • When no more to send to child, close write end of pipe-to-child.
    • When all data received, close read end of pipe-from-child.

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.

问题回答

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.

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);




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签