I want to test the pclose(3) whether it will wait the shell command terminate.I write two little shell program.
我想测试一下pclose(3)是否会等待shell命令终止。我写了两个小shell程序。
//a.sh
#!/bin/bash
sleep 3
//b.sh
#!/bin/bash
echo "something"
sleep 3
c program:
//ptest.c
#include <stdio.h>
#include <sys/wait.h>
int main(int argc, char **argv) {
char *filename = argv[1];
char *mode = argv[2];
FILE *fl = popen(filename, &mode);
int t = pclose(fl);
if(WIFEXITED(t)) {
printf("exit status:%d\n", WEXITSTATUS(t));
}
return 0;
}
then, compile: $ gcc -o ptest ptest.c
然后,编译:$ gcc -o ptest ptest.c
next run the ptest(my computer is Ubuntu 12.04.3 LTS):
接下来运行ptest(我的电脑是Ubuntu 12.04.3 LTS):
$ ./ptest "sh a.sh" r
$ exit status:0
this test will wait the shell terminate and output exit status 0.However when I run the ptest as following form:
此测试将等待shell终止并输出退出状态0.但是当我按以下形式运行ptest时:
$ ./ptest "sh b.sh" r
$ exit status:141
this time, ptest don't wait shell program and terminate itself immediately, I just add an echo statement before the sleep, But the result was different. I don't know why .
这次,ptest不要等待shell程序并立即终止自己,我只是在睡眠前添加一个echo语句,但结果却不同了。我不知道为什么。
1 个解决方案
#1
1
exit status:141
is a SIGPIPE
error. It is well explained in this question Why exit code 141 with grep -q?
退出状态:141是SIGPIPE错误。在这个问题中有很好的解释为什么用grep -q退出代码141?
The issue is that your b.sh
script tries to write to the pipe, but nobody is reading this pipe in your C program.
问题是您的b.sh脚本尝试写入管道,但没有人在您的C程序中读取此管道。
#1
1
exit status:141
is a SIGPIPE
error. It is well explained in this question Why exit code 141 with grep -q?
退出状态:141是SIGPIPE错误。在这个问题中有很好的解释为什么用grep -q退出代码141?
The issue is that your b.sh
script tries to write to the pipe, but nobody is reading this pipe in your C program.
问题是您的b.sh脚本尝试写入管道,但没有人在您的C程序中读取此管道。