// 使用popen代替system函数
int my_system(const char * cmd)
{
FILE * fp;
int res;if (cmd == NULL) {
printf("my_system cmd is NULL!
");
return -1;
}
if ((fp = popen(cmd, "r") ) == NULL) {
perror("popen");
printf("popen error: %s/n", strerror(errno));
return -1;
}
if ((res = pclose(fp)) == -1) {
printf("close popen file pointer fp error!
");
return res;
} else if (res == 0) {
return res;
}
// printf("popen res is :%d
", res);
return res;
}
// 将SIGCHLD设置为SIG_DFL处理方式,这种写法可能也存在问题,能不用还是别用了。
int my_system(const char *cmd_line)
{
int ret = 0;
sighandler_t old_handler;
old_handler = signal(SIGCHLD, SIG_DFL);
ret = system(cmd_line);
signal(SIGCHLD, old_handler);
return ret;
}