linux环境c++程序调用shell返回值问题

来源:互联网 发布:顽固软件卸载。 编辑:程序博客网 时间:2024/05/18 16:54

省外结算有一个根据socket消息执行本地shell脚本的C++程序,这个程序需要获取shell脚本的执行结果,shell脚本通过echo $?来返回执行失败还是成功。

测试发现返回值不正常,自己写的小demo可以返回正常执行结果,但是在admserver中无法正常获取。

调试发现是程序中忽略了子进程的信号signal(SIGCLD, SIG_IGN);而调用shell脚本的popen函数内部实现机制,导致无法正常获取popenfork的子进程的返回结果。

因为admserver每接收到一个消息,创建一个子进程去处理消息,处理完退出,所以要用waitpid回收退出的子进程,否则每处理完一个消息就会有一个僵尸进程。


修改办法:

signal(SIGCLD, UserChildTerm); 捕获子进程信号

void UserChildTerm(int signo)
{
signal(SIGCLD,UserChildTerm);

int status;   
int pid = waitpid(-1, &status, WNOHANG);   
if (WIFEXITED(status)) {   
//printf("The child %d exit with code %d\n", pid, WEXITSTATUS(status));   
}
}


脚本: ffcs_scp_bin.sh

#!/bin/bash
bin_path=/home/zhjs/settle/bin/

scp ${bin_path}$1 zhjs@$2:${bin_path}
echo $?



admserver调用shell的代码

FILE *fpss;
fpss = popen(Command, "r");    
if(fpss != NULL)
{
char chBuffer[BUF_SIZE];
memset(chBuffer,0,sizeof(chBuffer)); 
fread(SendBuffer,sizeof(char), sizeof(SendBuffer), fpss);
}

原创粉丝点击