简体   繁体   English

使用等待系统调用时

[英]When wait system call is used

The theory says that, if wait is not called parent wont be getting information about terminated child and child becomes zombie. 该理论说,如果不叫等待,父母将不会获得有关被终止的孩子的信息,而孩子会变成僵尸。 But when we create a process, zombies are not created even if we are not calling wait. 但是,当我们创建一个进程时,即使不调用wait,也不会创建僵尸。 My question is whether the wait is called automatically? 我的问题是等待是否自动调用?

In many languages, calling a sub process will call wait() for you. 在许多语言中,调用子进程将为您调用wait() For example, in ruby or perl, you often shell out like this: 例如,在ruby或perl中,您经常会像这样剥壳:

#!/usr/bin/ruby
system("ls /tmp")
`ls /tmp`

This is doing a bunch of magic for you, including calling wait() . 这正在为您做很多事情,包括调用wait() In fact, Ruby must wait for the process to exit anyway to collect the output before the program can continue. 实际上,Ruby必须等待程序退出以收集输出,然后程序才能继续。

You can easily create zombies like this: 您可以轻松地创建如下僵尸:

#!/usr/bin/ruby
if fork
  sleep 1000 # Parent ignoring the child
else
  exec "ls /tmp" # short-lived child
end

When we manually fork/exec, there is no magic calling wait() for us, and a zombie will be created. 当我们手动fork / exec时,没有魔术为我们调用wait() ,并且将创建一个僵尸。 But when the parent exits, the zombie child will get re-parented to init , which will always call wait() to clean up zombies. 但是当父级退出时,僵尸子级将重新成为init的父级,而init始终会调用wait()来清理僵尸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM