Suppress output from popen()
from stackoverflow
病例样本:
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.
Answers:
-- –
-- –
-- –
-- –
-- –
--
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 accuratelyexplained, 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, sopopen()
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-levelfork()
andexec*()
routines instead.
--
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
Changelog:
* Apr 28 2012 Richard Qi <qihh85@126.com>
- 如何判断子进程执行成功与否及获取错误信息
the following macros, which are defined in <sys/wait.h> and evaluate to integral expressions; the stat_valargument is the integer value pointed to by stat_loc.
WIFEXITED( stat_val)
-
Evaluates to a non-zero value if status was returned for a child process that terminated normally.
WEXITSTATUS( stat_val)
-
If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().
WIFSIGNALED( stat_val)
-
Evaluates to a non-zero value if status was returned for a child process that terminated due to the receipt of a signal that was not caught (see <signal.h>).
WTERMSIG( stat_val)
-
If the value of WIFSIGNALED(stat_val) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process.
WIFSTOPPED( stat_val)
-
Evaluates to a non-zero value if status was returned for a child process that is currently stopped.
WSTOPSIG( stat_val)
-
If the value of WIFSTOPPED(stat_val) is non-zero, this macro evaluates to the number of the signal that caused the child process to stop.
WIFCONTINUED( stat_val)
-
Evaluates to a non-zero value if status was returned for a child process that has continued from a job control stop.