C 程序 fork() 结果重复

C program fork() results repeating

我正在通过编写一个简单的程序来学习 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

如果评论中的解释不清楚,您需要将完整的第二个 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";

(另请注意,不需要附上 {...}

检查一下,如果您还有其他问题,请告诉我。