分叉错误?如何修复?

发布于 2024-08-28 04:11:44 字数 413 浏览 5 评论 0原文

这段代码有什么问题?怎么解决呢?父进程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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烂人 2024-09-04 04:11:44
  • 该代码不是 C99 代码
  • 该代码不包含正确的标头
  • 该代码每秒分叉一次子进程;子进程立即退出(表示成功)
  • 程序不会停止,直到被信号中断
  • 因为代码不会等待其子进程,并且不会忽略 SIGCHLD 信号,所以该进程会累积僵尸进程 - 将被清除的进程如果只有他们的父母在等他们就好了。

    #include ;
    #include ;
    int 主函数(无效)
    {
        为了 (;;)
        {
            if (fork() == 0)
                退出(0);
            睡眠(1);
        }
        /* 未到达 */
        返回(0);
    }
    

替代代码使父进程成功退出,而子进程进入睡眠状态一秒钟,然后分叉并自杀(留下一个新的子进程继续该过程)。

  • The code is not C99 code
  • The code does not include the correct headers
  • The code forks a child once a second; the child immediately exits (indicating success)
  • The program doesn't stop until interrupted by a signal
  • 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.

    #include <stdlib.h>
    #include <unistd.h>
    int main(void)
    {
        for (;;)
        {
            if (fork() == 0)
                exit(0);
            sleep(1);
        }
        /* Unreached */
        return(0);
    }
    

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).

野心澎湃 2024-09-04 04:11:44

我不知道问题是什么,因为我不知道你想要它做什么。它所做的就是分叉一个子进程,然后该子进程每秒退出(成功)。

当 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.

远昼 2024-09-04 04:11:44

成功完成后,fork()
返回值 0 给子级
进程并返回进程 ID
子进程到父进程
过程。否则,值为 -1
返回父进程,没有
子进程被创建,并且
全局变量 errno 设置为 indi-
找出错误。

该程序每秒创建子进程并立即退出。

换句话说,子进程“进入 if”。您可以通过在 if 内添加 printf 语句来进行检查。

Upon successful completion, fork()
returns a value of 0 to the child
process and returns the process ID of
the child process to the parent
process. Otherwise, a value of -1 is
returned to the parent process, no
child process is created, and the
global variable errno is set to indi-
cate the error.

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.

很糊涂小朋友 2024-09-04 04:11:44

从你的命令行输入

man 2 fork

,它会告诉你你想知道的一切。

From your command line type

man 2 fork

and it will tell you everything you want to know.

平安喜乐 2024-09-04 04:11:44

返回值

成功完成后,fork() 将向子进程返回 0,并将子进程的进程 ID 返回给父进程。两个进程都应继续从 fork() 函数执行。否则,返回-1给父进程,不创建子进程,并设置errno来指示错误。

=================

if (!fork()) equals to     if (0 == fork())

你的意思是,成功创建进程后不再创建进程?好像有问题...

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.

=================

if (!fork()) equals to     if (0 == fork())

What did you mean, not to create processes after one successfully created? Seems like a problem...

太阳哥哥 2024-09-04 04:11:44

我可以建议您注释掉 for 循环并插入一些 printf 语句(或使用 gdb 单步执行)。这将使您深入了解正在发生的事情。或者将 for 循环与以下内容绑定:

int i = 0;
for ( i = 0; i < 5; i++ )

而不是使用无限循环,以便更容易理解/调试。

另请阅读 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:

int i = 0;
for ( i = 0; i < 5; i++ )

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文