分叉错误?如何修复?
这段代码有什么问题?怎么解决呢?父进程goto in if或子进程?
第一个代码产生僵尸进程或第二个代码或两者或非?
#include <signal.h>
#include <sys/wait.h>
main() {
for (;;) {
if (!fork()) {
exit(0);
}
sleep(1);
}
}
这段代码怎么样:
#include <signal.h>
#include <sys/wait.h>
main() {
for (;;) {
if (fork()) {
exit(0);
}
sleep(1);
}
}
What is the problem with this code? How to solve it? Parent processes goto in if or child process?
first code produce zombie process or second code or both or non ?
#include <signal.h>
#include <sys/wait.h>
main() {
for (;;) {
if (!fork()) {
exit(0);
}
sleep(1);
}
}
what about this code :
#include <signal.h>
#include <sys/wait.h>
main() {
for (;;) {
if (fork()) {
exit(0);
}
sleep(1);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为代码不会等待其子进程,并且不会忽略 SIGCHLD 信号,所以该进程会累积僵尸进程 - 将被清除的进程如果只有他们的父母在等他们就好了。
替代代码使父进程成功退出,而子进程进入睡眠状态一秒钟,然后分叉并自杀(留下一个新的子进程继续该过程)。
Because the code does not wait for its children, and it does not ignore SIGCHLD signals, the process accumulates zombies - processes that would be cleaned up if only their parent waited for them.
The alternative code has the parent exit successfully, while the child goes to sleep for a second, and then forks and commits suicide (leaving a new child to continue the process).
我不知道问题是什么,因为我不知道你想要它做什么。它所做的就是分叉一个子进程,然后该子进程每秒退出(成功)。
当 fork 成功完成时,它会向子进程返回 0,并向父进程返回子进程的 pid。因此,子程序在第一个程序中进入 if 主体,而父程序在第二个程序中进入 if 主体。
第一个创建了无限数量的僵尸进程,因为父进程永远存在但从不等待。第二个不会,因为父进程立即退出,所以子进程成为孤儿并被 init 收养。
编辑:解决了问题的更新。
I don't know what the problem is, because I don't know what you want it to do. What it does do is fork a child, which then exits (successfully), every second.
When fork completes successfully, it returns 0 to the child, and the pid of the child to the parent. So it's the child that enters the if body in the first program, and the parent in the second program.
The first creates an infinite number of zombie processes, because the parent lives forever but never waits. The second does not because the parent exits immediately, so the child is orphaned and adopted by init.
EDIT: Addressed update to question.
该程序每秒创建子进程并立即退出。
换句话说,子进程“进入 if”。您可以通过在 if 内添加 printf 语句来进行检查。
This program creates child processes every second which immediately quit.
Put another way, the child process "goes into the if." You could check by adding a
printf
statement inside the if.从你的命令行输入
,它会告诉你你想知道的一切。
From your command line type
and it will tell you everything you want to know.
返回值
成功完成后,fork() 将向子进程返回 0,并将子进程的进程 ID 返回给父进程。两个进程都应继续从 fork() 函数执行。否则,返回-1给父进程,不创建子进程,并设置errno来指示错误。
=================
你的意思是,成功创建进程后不再创建进程?好像有问题...
RETURN VALUE
Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.
=================
What did you mean, not to create processes after one successfully created? Seems like a problem...
我可以建议您注释掉 for 循环并插入一些
printf
语句(或使用 gdb 单步执行)。这将使您深入了解正在发生的事情。或者将 for 循环与以下内容绑定:而不是使用无限循环,以便更容易理解/调试。
另请阅读
fork()
及其工作原理。然后,您不仅能够回答问题,还能理解为什么这些答案适用,并能够避免在您自己的软件中犯错误。我建议保留睡眠声明。
我怀疑这个作业的重点是你通过修改它来尝试它并了解在你的系统上什么是安全的,什么是不安全的。
Can I suggest you comment out the for loop and insert a few
printf
statements (or step through with gdb). That'll give you an insight into what's going on. Or bound the for loop with something like:rather than using an infinite loop so that it is easier to understand/debug.
Also read up on
fork()
and how it works. Then you'll not just be able to answer the question but understand why these answers apply and be able to avoid making mistakes in your own software.I would suggest leaving the sleep statement in place.
I suspect the point of this homework is that you try it by modifying it and understand what is and isn't safe to do on your system.