0
我有一些麻煩得到waitpid工作有人請解釋這個代碼有什麼問題嗎?fork()和waitpid()不等待孩子
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
int main() {
string filename_memory;
decltype(fork()) pid;
if (!(pid = fork())) {
cout << "in child" << endl;
sleep(1);
}
else {
int status_child;
do {
waitpid(pid, &status_child, WNOHANG);
cout << "waiting for child to finish" << endl;
} while (!WIFEXITED(status_child));
cout << "child finished" << endl;
}
return 0;
}
你可能要檢查waitpid'的'返回值和閱讀'status_child'僅如果返回值等於'pid',像' if(ret == pid && WIFEXITED(status_child))break;' –