为什么waitpid不等待进程退出?

时间:2022-02-08 02:44:49

I have this simple code written in C and my os is osx:

我有这个用C编写的简单代码,我的os是osx:

int main() {
    pid_t pid = 1244;
    int pid_status;
    waitpid(pid, &pid_status, 0);
    printf("%i\n",pid_status);
}

1244 is the process id for Opera browser running right now:

1244是Opera浏览器现在运行的进程ID:

Korays-MacBook-Pro:HelloWorld koraytugay$ ps -p 1244
  PID TTY           TIME CMD
 1244 ??         0:09.19 /Applications/Opera.app/Contents/MacOS/Opera

What I expect to happen is when I run this program, it should simply wait until I close Opera, but what I get is:

我期望发生的是当我运行这个程序时,它应该等到我关闭Opera,但我得到的是:

Korays-MacBook-Pro:HelloWorld koraytugay$ ./koko
1798668342
Korays-MacBook-Pro:HelloWorld koraytugay$ ./koko
1637806134
Korays-MacBook-Pro:HelloWorld koraytugay$ ./koko
1707163702

Why is this program directly quitting and what is these different numbers?

为什么这个程序直接退出,这些不同的数字是什么?

1 个解决方案

#1


2  

waitpid is only effective for child processes. See the manual page http://man7.org/linux/man-pages/man2/wait.2.html.

waitpid仅对子进程有效。请参见手册页http://man7.org/linux/man-pages/man2/wait.2.html。

You will find waitpid is returning -1. Please check errno

你会发现waitpid正在返回-1。请检查errno

#1


2  

waitpid is only effective for child processes. See the manual page http://man7.org/linux/man-pages/man2/wait.2.html.

waitpid仅对子进程有效。请参见手册页http://man7.org/linux/man-pages/man2/wait.2.html。

You will find waitpid is returning -1. Please check errno

你会发现waitpid正在返回-1。请检查errno