我在我的 cpp 程序中调用了一个外部二进制文件( Webots Simulatorfork()
),方法是调用and execlp()
,它接受一个作为特定文件路径的参数。当文件的路径有效并且文件存在时没有问题,但是,当我将其指向无效文件时,我得到了预期Could not open file: 'file.wbt'
的但子进程没有退出。我虽然这可能是由于 Webot 的内部代码以及它如何处理此类错误,但是,如果我调用webots file.wbt
系统的 bash shell 会引发相同的错误,但进程会按原样结束。
我的程序的可编译部分:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
int start_webots(const char* world){
//starting webots as a child process with known PID
int pid = fork();
if (pid == 0){
printf("Starting Webots...");
execlp("/usr/bin/webots","webots",world,NULL);
printf("Finished executing Webots");
exit(1);
}
else printf("Webots PID: %i\n", pid);
return pid;
}
int main(int argc, char const *argv[]){
int pid=start_webots("/home/joao/Work/ASBG/code/cpp/darwin/worlds/darwin_cpgs_noise.webt");
if (kill(pid,0)) printf("NO PROCESS RUNNING\n");
return 0;
}
只要二进制文件存在(安装了 webots),execlp 之后的代码行自然不会执行,因为 webots 替换了子进程。但是,在出现错误后Could not open (...)
我没有得到任何输出stdout
,程序挂起,并且我让进程webots
在后台运行。
我到底做错了什么/如何找到解决方法或解决方案?