gpt4 book ai didi

c - 在三个管道之间传输消息

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:23 25 4
gpt4 key购买 nike

我正在从事一个在三个进程之间发送消息的项目:一个有两个 child 的 parent 。第一个进程将接收消息,然后将消息发送给第二个进程。第二个进程将读取一条消息,然后在将其发送到第三个进程之前对其进行转换。第三个进程将读取消息并进一步转换该消息。最后,消息将被发送回第一个进程,打印它。

这将在 UNIX 中使用管道和系统调用来实现。我不太熟悉这个过程,所以我很感激你能提供的任何提示/建议。下面是我的代码。谢谢你!

*#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define ERR (-1) /* indicates an error condition */
#define READ 0 /* read end of a pipe */
#define WRITE 1 /* write end of a pipe */
#define STDIN 0 /* file descriptor of standard in */
#define STDOUT 1 /* file descriptor of standard out */
int main()
{
int pid_1, /* will be process id of first child, which inverts the string */
pid_2; /* will be process id of second child, which converts the string to uppercase */
int fd[2]; //descriptor array for parent process
int fd2[2]; //descriptor array for first child process
int fd3[2]; //descriptor array for second child process
char ch [100]; //original character array
char ch2 [100]; //character array after reversal
int index = 0; //size
char character;
while((character = getchar()) != '\n') //get input and put it into array
{
ch[index] = character;
index++;
}
if(pipe (fd) == ERR)
{
perror("Parent pipe cannot be created\n");
exit (ERR);
}
if (pipe (fd2) == ERR) /* create a pipe */
{ /* must do before a fork */
perror ("Pipe cannot be created.\n");
exit (ERR);
}
if (pipe (fd3) == ERR) /* create a pipe */
{ /* must do before a fork */
perror ("Second pipe cannot be created.\n");
exit (ERR);
}
if ((pid_1 = fork()) == ERR) /* create 1st child */
{
perror ("Second process cannot be created.\n");
exit (ERR);
}
if (pid_1 != 0) /* in parent */
{
close(fd2[0]); //close read end of first child
close(fd[1]); //close write end of parent
printf("Parent process sends message %s\n", ch);
write(fd2[1], ch, sizeof(ch)); //write to write end of first child
close(fd2[1]); //close write end of first child
close(fd[0]);
if ((pid_2 = fork ()) == ERR) /* create 2nd child */
{
perror ("Third process cannot be created.\n");
exit (ERR);
}
if (pid_2 != 0) /* still in parent */
{
wait ((int *) 0); /* wait for children to die */
wait ((int *) 0);
read(fd[0], ch2, sizeof(ch2)); //read read end of parent process
printf("Parent process receives message %s\n", ch2);
int i = 0;
while (i < index)
{
printf("%c", ch2[i]); //print message
i++;
}
printf("\n");
close(fd3[1]); //close write end of second child
close(fd[0]); //close read end of parent process
}
else /* in 2nd child */
{
close(fd3[1]); //close write end of second child
close(fd2[0]); //close read end of first child
read(fd3[0], ch2, sizeof(ch2)); //read read end of second child
printf("Second child receives %s\n", ch2);
int i = 0;
while (i < index)
{
ch2[i] = toupper(ch2[i]); //convert to uppercase
i ++;
}
printf("Second child sends message %s\n", ch2);
write(fd[1],ch2, sizeof(ch2)); //write to write end of parent process
close(fd3[0]); //close read end of second child
close(fd[1]); //close read end of parent process
}
}
else /* in 1st child */
{
close(fd2[1]); //close write end of first child
close(fd[0]); //close read end of parent process
read(fd2[0], ch, sizeof(ch)); //read read end of first child
printf("First child receives message %s\n", ch);
int i = 0;
while (i < index)
{
ch2[i] = ch[index - 1 - i]; //reverse
i++;
}
printf("First child sends message %s\n", ch2);
write(fd3[1], ch2, sizeof(ch2)); //write to write end of second child
close(fd3[1]); //close write end of second child
close(fd2[0]); //close read end of first child
}
exit(0);
}

最佳答案

一个人不应该在没有测试的情况下编写这么多代码。 (有一种更好的方法,我稍后会讲到。)但是如果您发现自己处于这种状态,可以使用以下一种方法来查找问题。

第 1 步:放入一些诊断输出语句以验证代码是否按照您的意图和期望执行:

...
printf("Here\n");
read(fd3[0], ch2, sizeof(ch2)); //read read end of second child
close(fd3[0]); //close read end of second child
printf("second child receives %s\n", ch2);
...
printf("Input reversed\n");
printf("first child sends %s\n", ch2);
write(fd3[1], ch2, sizeof(ch2)); //write to write end of second child
...

这足以表明某些东西不起作用。

第 2 步:在不改变错误行为的情况下尽可能简化代码,直到错误无处可藏。在这种情况下你会发现你仍然忽略了pipe(fd3) ,即使在我向您指出这一点并且您承认问题并说您已解决它之后。如果这不能使您相信简化的重要性,那么什么都不会。

编写代码的正确方法是慢慢构建,每一步都进行测试。在尝试将其连接到大电路之前,您应该已经测试过 fd3 链接。从小而简单的事情开始,一次增加一点复杂性,单独测试新功能,永远不要添加不起作用的代码。

关于c - 在三个管道之间传输消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085112/

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