C程序fork()结果重复

问题描述 投票:1回答:1

我正在通过编写一个简单程序来学习fork()和管道。我成功返回了结果,但是结果之一重复了一次。我一直在尝试用代码来解决它,但无法正确完成。

睡眠时间就是模拟系统调整的时间。我一直试图将导航睡眠时间设置为介于0到6之间的随机秒数,但我无法设法做到这一点并打印出生成给报告的随机数。这是我愿意忍受的问题,但希望它能奏效。

当前代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <wait.h>
#include <time.h>

#define MAX 150
#define PipeStdIn 0             //UNIX StdIn
#define PipeStdOut 1            //UNIX StdOut

void lifeSupport();
void navigation();

int main()
{
    const char *message = {"Calibrate Systems\n"};
    int pipes[2], ret;
    char buf[MAX + 1];


    if(pipe(pipes) == 0)
    {
        if(fork() == 0)
        {
            ret = read(pipes[PipeStdIn], buf, MAX);
            printf("Life Support receives instruction: %s\n", buf);

            sleep(5);

            sleep(4);

            const char *breathGL = {"Breathing gas levels have been adjusted\nAdjustment time: 5 seconds\nLighting and temperature levels have been adjusted\nAdjustment time: 4 seconds\n"};

            ret = write(pipes[PipeStdOut], breathGL, strlen(breathGL) + 1);
        }
        else
        {       
            ret = write(pipes[PipeStdOut], message, strlen(message) + 1);                   //write

            ret = wait(NULL);

            ret = read(pipes[PipeStdIn], buf, MAX);

            time_t now;
            time(&now);
            printf("Report received: %s\n", buf);
            printf("Report time: %s\n", ctime(&now));
        }
    }

    if(pipe(pipes) == 0)
    {
        if(fork() == 0)
        {
            ret = read(pipes[PipeStdIn], buf, MAX);
            printf("Navigation receives instruction: %s\n", buf);

            sleep(3);

            const char *nav = {"Navigation system has been adjusted\nAdjustment time: 3 seconds\n"};

            ret = write(pipes[PipeStdOut], nav, strlen(nav) + 1);
        }
        else
        {       
            ret = write(pipes[PipeStdOut], message, strlen(message) + 1);                   //write

            ret = wait(NULL);

            ret = read(pipes[PipeStdIn], buf, MAX);

            time_t now;
            time(&now);
            printf("Report received: %s\n", buf);
            printf("Report time: %s\n", ctime(&now));
        }       
    }

    close(pipes[PipeStdIn]);
    close(pipes[PipeStdOut]);

    return 0;
}

预期结果:

Life Support receives instruction: Calibrate Systems

Report received: Breathing gas levels have been adjusted
Adjustment time: 5 seconds
Lighting and temperature levels have been adjusted
Adjustment time: 4 seconds

Report time: Tue Apr 28 08:48:49 2020

Navigation receives instruction: Calibrate Systems

Report received: Navigation system has been adjusted
Adjustment time: 3 seconds

Report time: Tue Apr 28 08:48:52 2020

当前结果:

Life Support receives instruction: Calibrate Systems

Navigation receives instruction: Calibrate Systems

Report received: Navigation system has been adjusted
Adjustment time: 3 seconds

Report time: Tue Apr 28 08:48:49 2020

Report received: Breathing gas levels have been adjusted
Adjustment time: 5 seconds
Lighting and temperture levels have been adjusted
Adjustment time: 4 seconds

Report time: Tue Apr 28 08:48:49 2020

Navigation receives instruction: Calibrate Systems

Report received: Navigation system has been adjusted
Adjustment time: 3 seconds

Report time: Tue Apr 28 08:48:52 2020
c pipe fork
1个回答
0
投票

如果注释中的解释不清楚,则需要将完整的第二个if(fork() == 0) {...} else {...}移动到第一个else中,以防止父级和子级都执行该块。向上移动if(fork() == 0) {...} else {...}后,您的逻辑将类似于:

    if(pipe(pipes) == 0)
    {
        if(fork() == 0)
        {
            ret = read(pipes[PipeStdIn], buf, MAX);
            ...
            ret = write(pipes[PipeStdOut], breathGL, strlen(breathGL) + 1);
        }
        else
        {       
            ret = write(pipes[PipeStdOut], message, strlen(message) + 1);
            ...

            if(fork() == 0)
            {
                ret = read(pipes[PipeStdIn], buf, MAX);
                ...
                const char *nav = "Navigation system has been adjusted\n"
                                    "Adjustment time: 3 seconds\n";

                ret = write(pipes[PipeStdOut], nav, strlen(nav) + 1);
            }
            else
            {       
                ret = write(pipes[PipeStdOut], message, strlen(message) + 1);
                ...
                // time_t now;
                time(&now);
                printf("Report received: %s\n", buf);
                printf("Report time: %s\n", ctime(&now));
            }       
        }
    }

    close(pipes[PipeStdIn]);
    close(pipes[PipeStdOut]);

还请注意,C编译将在编译过程中将相邻的字符串连接为单个字符串,使您可以将长行分解为一系列双引号的字符串,例如,在编译时将它们连接在一起。

        const char *breathGL = "Breathing gas levels have been adjusted\n"
                        "Adjustment time: 5 seconds\n"
                        "Lighting and temperature levels have been adjusted\n"
                        "Adjustment time: 4 seconds\n";

            const char *nav = "Navigation system has been adjusted\n"
                                "Adjustment time: 3 seconds\n";

((也请注意不包含{...}

仔细检查,如果还有其他问题,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.