了解fork(),sleep()并处理流量 [英] understanding fork(), sleep() and processes flux

查看:134
本文介绍了了解fork(),sleep()并处理流量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经练习过那些系统调用,但是我坚持使用以下代码:

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;
}

我得到的输出是:

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

我正在尝试的操作是通过sleep()管理时间的这些消息的简单打印. 我无法理解为什么在打印第二条子消息之前程序会返回. 默认情况(第二个分叉之后的那个)在其子级(第二个)作用于输出之前被打印出来,就像他忽略其wait()一样.因此,其子项在流程返回后被打印.

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()函数,因为如果我用wait((int *)0);代替它,则进程磁通将按照其设计的方式工作(无论如何,没有任何时间). 在这一点上,我不再确定进程通量或sleep()的使用(手册页没有那么大的帮助,简而言之,太简洁了).

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

推荐答案

实际上,您的等待呼叫有效.它检测到第一个子进程的结束,然后继续.如果您连续两次调用wait(),您将获得正确的行为.

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.

更新的测试代码:

#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

这篇关于了解fork(),sleep()并处理流量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆