parent c 中 wait() 的目的
The purpose of the wait() in parent c
我是 linux 和 c 中的新手。
我正在使用这个简单的例子:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, const char * argv[]) {
pid_t child_pid_or_zero = fork(); //fork returns twice
if(child_pid_or_zero < 0)
{
//if fork returns a number smaller than zero, something wrong happened
perror("Something wrong happened\n");
exit(-1);
}
if(child_pid_or_zero > 0)
{
//if fork returns a number greater than zero, this is the parent process
printf("I'm the parent, my pid is: %d\t My child pid is %d\n", getpid(), child_pid_or_zero);
wait(NULL);
}
else
{
//this means that fork now returned 0, the child process is running
printf("I am the child with pid: %d\t My parent pid is: %d\n",child_pid_or_zero, getppid());
}
return 0;
}
如果我要省略
中的 wait() 方法
if(child_pid_or_zero > 0)
会发生什么?我自己试过了,显然,没有立竿见影的区别。我们是否总是需要使用 wait(),或者这是否只适用于 child 应该执行繁重的计算等?
提前致谢。
Wait 用于侦听状态变化并获取有关 child 的信息。状态更改是 child 终止,通过信号停止或恢复。等待允许系统释放与child关联的资源。如果不执行等待,则终止的 child 将保持 "zombie" 状态。
The kernel maintains a minimal set of information about the zombie
process (PID, termination status,
resource usage information) in order to allow the parent to later perform a wait to obtain information about the child. As long
as a zombie is not removed from the system via a
wait, it will consume a slot in the kernel process table, and if this table fills, it will not be possible to create further
processes. If a parent process terminates, then its
"zombie" children (if any) are adopted by init(1), which automatically performs a wait to remove the zombies.
系统调用wait(2)通常用于查找子进程的状态是否发生了变化(即它是否仍然运行、退出等)。
另一个目的是避免"zombie"进程。如果父进程不等待子进程并且子进程在父进程之前退出,则它成为 "zombie" 进程。因此,wait() 调用用于 "reap" 进程并释放与进程关联的系统资源。
想象一下,如果父进程是一个很长的 运行 并且定期创建多个子进程,那么所有的僵尸进程都会在进程 table 中有条目,这是对系统的不必要使用资源。
fork之后,你将拥有两个独立的进程。
调用 wait()
时,您告诉父进程等待子进程终止。
在这个例子中,没有任何变化,因为两个进程没有相互交互,所以父进程可以在创建子进程并打印字符串后退出,但在父进程必须等待子进程完成的情况下做一些操作,然后可能 return 一些值给父级,它变得有用!
我是 linux 和 c 中的新手。 我正在使用这个简单的例子:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, const char * argv[]) {
pid_t child_pid_or_zero = fork(); //fork returns twice
if(child_pid_or_zero < 0)
{
//if fork returns a number smaller than zero, something wrong happened
perror("Something wrong happened\n");
exit(-1);
}
if(child_pid_or_zero > 0)
{
//if fork returns a number greater than zero, this is the parent process
printf("I'm the parent, my pid is: %d\t My child pid is %d\n", getpid(), child_pid_or_zero);
wait(NULL);
}
else
{
//this means that fork now returned 0, the child process is running
printf("I am the child with pid: %d\t My parent pid is: %d\n",child_pid_or_zero, getppid());
}
return 0;
}
如果我要省略
中的 wait() 方法if(child_pid_or_zero > 0)
会发生什么?我自己试过了,显然,没有立竿见影的区别。我们是否总是需要使用 wait(),或者这是否只适用于 child 应该执行繁重的计算等?
提前致谢。
Wait 用于侦听状态变化并获取有关 child 的信息。状态更改是 child 终止,通过信号停止或恢复。等待允许系统释放与child关联的资源。如果不执行等待,则终止的 child 将保持 "zombie" 状态。
The kernel maintains a minimal set of information about the zombie process (PID, termination status, resource usage information) in order to allow the parent to later perform a wait to obtain information about the child. As long as a zombie is not removed from the system via a wait, it will consume a slot in the kernel process table, and if this table fills, it will not be possible to create further processes. If a parent process terminates, then its "zombie" children (if any) are adopted by init(1), which automatically performs a wait to remove the zombies.
系统调用wait(2)通常用于查找子进程的状态是否发生了变化(即它是否仍然运行、退出等)。
另一个目的是避免"zombie"进程。如果父进程不等待子进程并且子进程在父进程之前退出,则它成为 "zombie" 进程。因此,wait() 调用用于 "reap" 进程并释放与进程关联的系统资源。
想象一下,如果父进程是一个很长的 运行 并且定期创建多个子进程,那么所有的僵尸进程都会在进程 table 中有条目,这是对系统的不必要使用资源。
fork之后,你将拥有两个独立的进程。
调用 wait()
时,您告诉父进程等待子进程终止。
在这个例子中,没有任何变化,因为两个进程没有相互交互,所以父进程可以在创建子进程并打印字符串后退出,但在父进程必须等待子进程完成的情况下做一些操作,然后可能 return 一些值给父级,它变得有用!