在三个管道之间传输消息
Transferring a message between three pipes
我正在开发一个在三个进程之间发送消息的项目:一个 parent 和两个 children。第一个进程将接收消息,然后将消息发送给第二个进程。第二个进程将读取一条消息,然后在将其发送到第三个进程之前对其进行转换。第三个进程将读取消息并进一步转换该消息。最后,消息将被发送回第一个进程,打印它。
这将在 UNIX 中使用管道和系统调用来实现。我不太熟悉这个过程,所以如果你能提供任何 tips/advice,我将不胜感激。下面是我的代码。谢谢!
*#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 link 连接到大电路之前,您应该已经对其进行了测试。从小而简单的事情开始,一次增加一点复杂性,单独测试新功能,永远不要添加不起作用的代码。
我正在开发一个在三个进程之间发送消息的项目:一个 parent 和两个 children。第一个进程将接收消息,然后将消息发送给第二个进程。第二个进程将读取一条消息,然后在将其发送到第三个进程之前对其进行转换。第三个进程将读取消息并进一步转换该消息。最后,消息将被发送回第一个进程,打印它。
这将在 UNIX 中使用管道和系统调用来实现。我不太熟悉这个过程,所以如果你能提供任何 tips/advice,我将不胜感激。下面是我的代码。谢谢!
*#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 link 连接到大电路之前,您应该已经对其进行了测试。从小而简单的事情开始,一次增加一点复杂性,单独测试新功能,永远不要添加不起作用的代码。