C++ Waitpid返回-1,为什么?

C++ Waitpid返回-1,为什么?,c++,linux,C++,Linux,process.hpp #define HPROCESS int class Process { private: int exitCode; HPROCESS hProcess; public: static void waitEnd(Process* proc) { int w = waitpid(proc->hProcess, &(proc->exitCode), 0);

process.hpp

#define HPROCESS int
class Process {
    private:
        int exitCode;
        HPROCESS hProcess;
    public:
        static void waitEnd(Process* proc) {
            int w = waitpid(proc->hProcess, &(proc->exitCode), 0);
            Debug::debug(w);
        }
};
main.cpp

int main() {
    Process* process = NULL;
    while(!process) {
        cout<<endl<<"Waiting for second process.\nPress any key";
        getchar();
        getchar();
        process = Process::takeExisting("process");
    }
    Process::waitEnd(process);
    cout<<endl<<"second process ended";
    cout<<endl<<endl;
    return 0;
}
intmain(){
进程*进程=NULL;
而(!进程){

coutWaitpid仅适用于使用fork()、exec()启动的子进程

在本例中,errno代码在调用后将为10,因为这是试图在其他进程而不是子进程上使用waitpid


顺便说一句,这并不能解决我的问题,但它解决了另一个问题:

无论如何,您都应该显式地检查错误。您确定
waitpid()
没有返回-1?而且,您的函数也不会执行您期望的操作:进程是通过值传递的,而不是通过引用传递的,因此对
exitCode
的修改不会产生任何作用。@andlabs我已经更正了代码并检查了-1,您是对的。该代码甚至没有编译。@Nikita现在检查
errno
以查看调用发生了什么;
man waitpid
了解更多信息info@andlabs谢谢,情况变得更清楚了)Errno code-10,没有子进程,所以如果我需要等待not-child进程,waitpid不适合。