gpt4 book ai didi

c - 导航功能将其所有输出打印两次

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:48 24 4
gpt4 key购买 nike

当我在 main 中单独调用函数时,它们工作正常,只需打印一次所需的消息。调用这两个函数,导致导航打印其消息两次。

我查找了不同的解决方案。我尝试使用 fflush(stdout) 无济于事,我尝试在 main 中使用条件以确保它们单独运行。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <wait.h>
#include "RAND_API.h"
#define MAXLEN 500

void lifeSupport();
void navigation();

int main()
{


lifeSupport();
navigation();
return 0;
}

void lifeSupport()
{
//Create pipe


ret = write( myPipe[1], lifeSup1, strlen( lifeSup1 ) + 1 );
//sleep( getRandExponential() * 10 );
ret = write( myPipe2[1], lifeSup2, strlen( lifeSup2 ) + 1 );
//sleep( getSRand() * 6 );
sleep( getRandExponential() * 5 );
ret = write( myPipe3[1], lifeSup3, strlen( lifeSup3 ) + 1 );
sleep( getRandExponential() * 4 );
ret = write( myPipe4[1], lifeSup4, strlen( lifeSup4 ) + 1 );
close( myPipe[1] );
close( myPipe2[1] );
close( myPipe3[1] );
close( myPipe4[1] );
}
else
{
//wait(NULL);
close( myPipe[1] );
close( myPipe2[1] );
close( myPipe3[1] );
close( myPipe4[1] );
ret = read( myPipe[0], buffer, MAXLEN );
printf( "%s", buffer );
ret = read( myPipe2[0], buffer, MAXLEN );
printf( "%s", buffer );
ret = read( myPipe3[0], buffer, MAXLEN );
printf( "%s", buffer );
ret = read( myPipe4[0], buffer, MAXLEN );
printf( "%s", buffer );
}

}

void navigation()
{
//create pipe

ret = write( apipe[1], nav1, strlen( nav1 ) + 1 );
ret = write( apipe2[1], nav2, strlen( nav2 ) + 1 );
//sleep( getSRand() * 6 );
ret = write( apipe3[1], nav3, strlen( nav3 ) + 1 );

close( apipe[1] );
close( apipe2[1] );
close( apipe3[1] );

}
else
{
close( apipe[1] );
close( apipe2[1] );
close( apipe3[1] );

ret = read( apipe[0], buff, MAXLEN );
printf( "%s\n", buff );

ret = read( apipe2[0], buff, MAXLEN );
printf( "%s\n", buff );

ret = read( apipe3[0], buff, MAXLEN );
printf( "%s\n", buff );

close( apipe[0] );
close( apipe2[0] );
close( apipe3[0] );

}

}

输出:

Life support system initiiated
Adjusting breathing gas levels
Adjusting enviroment
Life support system terminating
Initiating navigation system

Initiating navigation system
Making adjustments


Making adjustments
Adjustments done. Navigation system terminating.


Adjustments done. Navigation system terminating.

预期输出:

Life support system initiiated
Adjusting breathing gas levels
Adjusting enviroment
Life support system terminating
Initiating navigation system
Making adjustments

Adjustments done. Navigation system terminating.

最佳答案

当您调用 lifeSupport() 时,它会 fork 一个 child 。子进程将消息写入所有 myPipeX 管道,父进程从中读取并打印消息,然后父进程和子进程都返回到 main()

然后两个进程都调用navigation()。他们各自创造了另一个 child 。两个 child 写入 apipeX 管道,两个 parent 从中读取,然后他们各自打印消息。因此,您从 navigation() 中获得了所有消息的两个副本。

在每个函数中, child 应该在完成后调用exit(),而不是返回。只有父级应该返回到 main()

关于c - 导航功能将其所有输出打印两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55621308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com