Linux 理解 fork()、sleep() 和流程通量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8167258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
understanding fork(), sleep() and processes flux
提问by init6eb
Been practicing with those system calls, but I stucked into this code:
一直在练习这些系统调用,但我坚持使用以下代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
pid_t pid;
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //first child
printf("\ni'm the first child, my pid is %d", getpid());
fflush(stdout);
break;
default: //parent
sleep(5); /** sleep is generating problems **/
printf("\ni'm the parent process, my pid is %d", getpid());
printf("\ngenerating a new child");
fflush(stdout);
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //second child
printf("\nhere i am, the second child, my pid is %d", getpid());
break;
default: //parent
wait((int *)0);
printf("\nback to parent, my pid is %d", getpid());
}
}
return 0;
}
The output I'm getting is:
我得到的输出是:
i'm the first child, my pid is 6203 i'm the parent process, my pid is 6202 generating a new child back to parent, my pid is 6202 Process returned 0 (0x0) execution time: 5.004 s Press ENTER to continue here i am, the second child, my pid is 6204
What I'm trying it's a simple print of these messages managing the timing via sleep()
.
I can't understand why the program is returning before printing the second child message.
The default case(the one right after the second fork) got printed before its child(second) acting on the output like he's ignoring its wait()
. Its child therefore got printed after the process returns.
我正在尝试的是这些消息的简单打印,通过sleep()
. 我不明白为什么程序在打印第二个子消息之前返回。默认情况(第二个 fork 之后的那个)在它的 child(second) 作用于输出之前被打印,就像他忽略了它的wait()
. 因此,它的子进程在进程返回后被打印出来。
I wasn't able to figure out what's the problem. I've marked sleep() function since if I substitute it with wait((int *)0);
the processes flux is working how it was designed to (anyhow, without any timing).
At this point I'm not sure anymore about process flux, or the sleep() usage (man pages wasn't that helpful, way too concise to be honest).
我无法弄清楚是什么问题。我已经标记了 sleep() 函数,因为如果我用wait((int *)0);
流程代替它,flux 正在按照它的设计方式工作(无论如何,没有任何时间)。在这一点上,我不再确定进程通量或 sleep() 的用法(手册页没有那么有用,说实话太简洁了)。
采纳答案by BenC
Actually, your call to wait works. It detects the end of the first child process and continues afterwards. If you do two consecutive calls to wait(), you will get the proper behaviour.
实际上,您的等待调用有效。它检测第一个子进程的结束并在之后继续。如果您连续两次调用 wait(),您将获得正确的行为。
Updated test code :
更新测试代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
pid_t pid;
int status;
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //first child
printf("\ni'm the first child, my pid is %d", getpid());
fflush(stdout);
break;
default: //parent
sleep(5); /** sleep is generating problems **/
printf("\ni'm the parent process, my pid is %d", getpid());
printf("\ngenerating a new child");
fflush(stdout);
switch(pid = fork())
{
case -1:
printf("fork failed");
break;
case 0: //second child
printf("\nhere i am, the second child, my pid is %d", getpid());
break;
default: //parent
pid = wait(&status);
printf("\nParent detects process %d was done", pid);
pid = wait(&status);
printf("\nParent detects process %d was done", pid);
printf("\nback to parent, my pid is %d", getpid());
}
}
return 0;
}
Output :
输出 :
i'm the first child, my pid is 30897
i'm the parent process, my pid is 30896
generating a new child
here i am, the second child, my pid is 30940
Parent detects process 30897 was done
Parent detects process 30940 was done
back to parent, my pid is 30896
回答by Jim Clay
The man page for wait says the following:
等待的手册页说明如下:
The wait() function shall suspend execution of the calling thread until status information for one of the terminated child processes of the calling process is available, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. If more than one thread is suspended in wait() or waitpid() awaiting termination of the same process, exactly one thread shall return the process status at the time of the target process termination.
wait() 函数应暂停调用线程的执行,直到调用进程的已终止子进程之一的状态信息可用,或者直到传递其动作是执行信号捕获函数或终止信号的信号。过程。如果在wait() 或waitpid() 中挂起多个线程等待同一进程终止,则恰好有一个线程返回目标进程终止时的进程状态。
Wait is returning because of the first child.
因为第一个孩子,Wait 又回来了。