C 了解fork()、sleep()和流程流量
我一直在练习那些系统调用,但我无意中学习了以下代码:C 了解fork()、sleep()和流程流量,c,linux,fork,sleep,C,Linux,Fork,Sleep,我一直在练习那些系统调用,但我无意中学习了以下代码: #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");
#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;
}
#包括
#包括
#包括
#包括
main()
{
pid_t pid;
开关(pid=fork())
{
案例1:
printf(“fork失败”);
打破
案例0://第一个孩子
printf(“\n我是第一个孩子,我的pid是%d”,getpid());
fflush(stdout);
打破
默认值://parent
睡眠(5);/**睡眠会产生问题**/
printf(“\n我是父进程,我的pid是%d”,getpid());
printf(“\n生成新子代”);
fflush(stdout);
开关(pid=fork())
{
案例1:
printf(“fork失败”);
打破
案例0://第二个孩子
printf(“\n我是第二个孩子,我的pid是%d”,getpid());
打破
默认值://parent
等待((int*)0);
printf(“\n回到父级,我的pid是%d”,getpid());
}
}
返回0;
}
我得到的结果是:
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
我是第一个孩子,我的pid是6203
我是父进程,我的pid是6202
产生一个新的孩子
回到家长那里,我的pid是6202
进程返回0(0x0)执行时间:5.004秒
按ENTER键继续
我在这里,第二个孩子,我的pid是6204
我尝试的是通过sleep()
管理时间的这些消息的简单打印。
我不明白为什么程序在打印第二个子消息之前返回。
默认情况(第二个fork后面的一个)在其子对象(第二个)作用于输出之前被打印,就像他忽略了它的wait()
。因此,它的子进程在进程返回后得到打印
我想不出是什么问题。我标记了sleep()函数,因为如果我用wait((int*)0替换它代码>流程流量按其设计方式工作(无论如何,没有任何计时)。
此时,我不再确定进程流量或sleep()的用法(手册页没有那么大帮助,说实话太简洁了)。等待手册页说:
wait()函数应暂停调用线程的执行,直到调用进程的一个已终止子进程的状态信息可用,或者直到传递其操作是执行信号捕获函数或终止进程的信号为止。如果在等待同一进程终止的wait()或waitpid()中挂起了多个线程,则在目标进程终止时,只有一个线程返回进程状态
等待返回是因为第一个孩子。实际上,您对等待的调用是有效的。它检测第一个子进程的结束,然后继续。如果连续两次调用wait(),将获得正确的行为
更新的测试代码:
#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;
}
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
非常感谢你的回答