在的popen和pclose函数的不同行为 [英] the difference behaviour in popen and pclose
问题描述
近日,笔者得到一个奇怪的问题,当我使用的popen(3)
和 pclose函数(3)
。就像这样:
Recently, I get a strange problem when i used popen(3)
and pclose(3)
. Just like this:
//a.sh
#!/bin/bash
cat /etc/issue
sleep 3
//b.sh
#!/bin/bash
cat /etc/issue
echo "before sleep"
sleep 3
我的C程序:
#include <stdio.h>
int main(void) {
1: FILE *fl = popen("sh a.sh", "r");
2: FILE *fl = popen("sh b.sh", "r");
int t = pclose(fl);
printf("%d\n", t);
return 0;
}
接下来,我将编译并运行此程序为4个步骤:
next, i will compile and run this program as 4 steps:
案例1:注释行2,然后编译并运行,程序不打印0,直到shell CMD终止。
case 1: comment line 2, then compile and run, the program do not print 0 until the shell cmd terminate.
案例2:注释行1,然后编译并运行,程序会马上终止,其打印13,也就是说函数,pclose
的退出状态为13 ,我抬头linux的错误号:
case 2: comment line 1, then compile and run, the program will terminate right now and it print 13, that is to say the exit status of pclose
is 13, i look up the linux errno:
13 EACCES +Permission denied
samebody将帮助我,告诉我原因吗?谢谢。
samebody would help me and tell me the reason? thanks.
推荐答案
您应该使用的 WEXITSTATUS 宏来获得实际收益code。
You should use the WEXITSTATUS macro to get the real return code.
这篇关于在的popen和pclose函数的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!