system函数

system()函数是调用/bin/sh执行脚本的,有些命令如rosrun不能在/bin/sh下执行

WIFEXITED(status) 这个宏用来指出子进程是否为正常退出的,如果是,它会返回一个非零值。当WIFEXITED返回非零值时,我们可以用这个宏来提取子进程的返回值,如果子进程调用exit(5)退出,WEXITSTATUS(status)就会返回5;如果子进程调用exit(7),WEXITSTATUS(status)就会返回7。请注意,如果进程不是正常退出的,也就是说,WIFEXITED返回0,这个值就毫无意义。

所以一个典型的system函数的使用是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pid_t status;
std::string cmd = "rosnode kill /lidar";
status = system(cmd.data());
if (-1 == status)
{
return -1;
}
else
{
ROS_INFO("WIFEXITED return: %d",WIFEXITED(status));
if (WIFEXITED(status)) //返回一个非零值, 正常退出
ROS_INFO("child process exit done: %d", WEXITSTATUS(status) );
else
ROS_INFO("child process exit abnormally");
}

参考:wait, WIFEXITED, WEXITSTATUS