C ++ popen函数挂起

时间:2019-02-12 15:38:58

标签: c++ popen

我正在一个在powerPC设备上运行linux内核3.14的嵌入式系统上工作。 我需要运行一个C ++应用程序,该应用程序应启动系统命令并检索结果。 我尝试了 popen ,但是挂起了。看下面的代码:

#define PATH_MAX_MIO 255

...

FILE *fp;
char path[PATH_MAX_MIO];

fp = popen("ll", "r"); // It hangs here. "ll" is the linux command to view files inside a directory
if (fp == NULL){ return; }


while (fgets(path, PATH_MAX_MIO, fp) != NULL)
    std::cout<< path;

pclose(fp);

使用调试器,我可以运行到popen调用为止,直到挂起为止,如果启动命令 top ,我可以看到该进程几乎占用了所有CPU(在90%和99.9%之间变化)。

我不知道为什么会这样。 有什么建议吗? 谢谢。

.:编辑:。 这是strace -f命令的输出

[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5348] <... select resumed> )      = 15 (in [14], left {0, 0})
[pid  5348] select(15, [], NULL, NULL, {0, 0} <unfinished ...>
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5348] <... select resumed> )      = -1 EINTR (Interrupted system call)
[pid  5348] --- SIGPROF {si_signo=SIGPROF, si_code=SI_KERNEL, si_value={int=14594, ptr=0x3902}} ---
[pid  5348] sigreturn() (mask [])       = -1 EINTR (Interrupted system call)
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL,  <unfinished ...>
[pid  5347] --- SIGPROF {si_signo=SIGPROF, si_code=SI_KERNEL, si_value={int=14594, ptr=0x3902}} ---
[pid  5347] <... rt_sigprocmask resumed> [], 8) = 27
[pid  5347] syscall_671098914(0, 0x1, 0xba4bdeda, 0xcbcb28a, 0xb6f76f8c, 0xb6f76f20) = 0
[pid  5348] select(15, [14], NULL, NULL, {0, 0}) = 0 (Timeout)
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL,  <unfinished ...>
[pid  5348] select(15, [14], NULL, NULL, {0, 0}) = 0 (Timeout)
[pid  5347] <... rt_sigprocmask resumed> [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL,  <unfinished ...>
[pid  5347] --- SIGPROF {si_signo=SIGPROF, si_code=SI_KERNEL, si_value={int=14594, ptr=0x3902}} ---
[pid  5347] <... rt_sigprocmask resumed> [], 8) = 27
[pid  5347] syscall_268774496(0, 0x10052a5c, 0x28002824, 0xb7a2f008, 0x1, 0xb7a379c0) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5348] select(15, [14], NULL, NULL, {0, 0}) = 0 (Timeout)
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL,  <unfinished ...>
[pid  5348] select(15, [14], NULL, NULL, {0, 0}) = 0 (Timeout)
[pid  5347] <... rt_sigprocmask resumed> [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL,  <unfinished ...>
[pid  5347] --- SIGPROF {si_signo=SIGPROF, si_code=SI_KERNEL, si_value={int=14594, ptr=0x3902}} ---
[pid  5347] <... rt_sigprocmask resumed> [], 8) = 27
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
[pid  5347] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

Ted Lyngmo建议启动仅显示exec *操作的跟踪,结果就是这样:

strace -f ../runtime/Run6 2>&1 | grep -E 'exec[a-z]+\('
execve("../runtime/Run6", ["../runtime/Run6"], [/* 13 vars */]) = 0
[pid  1068] execve("/bin/sh", ["sh", "-c", "cat /proc/version"], [/* 13 vars */]) = 0
[pid  1069] execve("/bin/cat", ["cat", "/proc/version"], [/* 13 vars */]) = 0

然后它再次挂起

0 个答案:

没有答案