Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popen performance in C

I'm designing a program I plan to implement in C and I have a question about the best way (in terms of performance) to call external programs. The user is going to provide my program with a filename, and then my program is going to run another program with that file as input. My program is then going to process the output of the other program.

My typical approach would be to redirect the other program's output to a file and then have my program read that file when it's done. However, I understand I/O operations are quite expensive and I would like to make this program as efficient as possible.

I did a little bit of looking and I found the popen command for running system commands and grabbing the output. How does the performance of this approach compare to the performance of the approach I just described? Does popen simply write the external program's output to a temporary file, or does it keep the program output in memory?

Alternatively, is there another way to do this that will give better performance?

like image 688
Daniel Standage Avatar asked Feb 10 '11 15:02

Daniel Standage


2 Answers

On Unix systems, popen will pass data through an in-memory pipe. Assuming the data isn't swapped out, it won't hit disk. This should give you just about as good performance as you can get without modifying the program being invoked.

like image 188
bdonlan Avatar answered Nov 09 '22 15:11

bdonlan


popen does pretty much what you are asking for: it does the pipe-fork-exec idiom and gives you a file pointer that you can read and write from.

However, there is a limitation on the size of the pipe buffer (~4K iirc), and if you arent reading quickly enough, the other process could block.

Do you have access to shared memory as a mount point? [on linux systems there is a /dev/shm mountpoint]

like image 29
Foo Bah Avatar answered Nov 09 '22 14:11

Foo Bah