Question

Is there a way to suppress the output from popen() without losing the Wait().

Test 1:

FILE * stream = NULL;
char buffer [120];

stream = popen ("ffmpeg -y -i test.amr -ar 16000 test.wav -v quiet", "r");

while (fgets (buffer, sizeof(buffer), stream))
{
}

pclose (stream);

Test 2:

FILE * stream = NULL;
char buffer [120];

stream = popen ("ffmpeg -y -i test.amr -ar 16000 test.wav -v quiet &> /dev/null", "r");

while (fgets (buffer, sizeof(buffer), stream))
{
}

pclose (stream);

The problem with Test 2 is that pclose() is not waiting for the pipe to finish processing. I don't want to have a bunch of FFMPEG output every time I have to do a pipe.

Was it helpful?

Solution

You should only use popen() when you want to send data to, or read data from, the child process (and that is an exclusive-or; if you want to do both, you have to set up the connection yourself).

If you don't want to do that, don't use popen().

As jim mcnamara accurately explained, given that you redirect the output of the child to /dev/null after the pipe is created, the redirection closes the pipe input to your program, so popen() gets zero bytes to read, which counts as EOF. And it returns - there is nothing more for it to read (if there might be more for it, it would not have received EOF).

In this context, use system(); it will wait for the child to finish - even when the output of the child is redirected to /dev/null. In other contexts, it might be appropriate to use the lower-level fork() and exec*() routines instead.

OTHER TIPS

output to /dev/null means that popen (which is calling read() ) does not block on a closed file descriptor for stdout. It returns right away.

You effectively closed stdout by redirecting it (dup() ), in reality it returns EOF

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top