在三个管道之间传输消息

时间:2015-03-16 19:14:47

标签: c unix operating-system pipe

我正在处理一个在三个进程之间发送消息的项目:一个有两个孩子的父进程。第一个进程将接收消息,然后将消息发送到第二个进程。第二个进程将读入一条消息,然后在将其发送到第三个进程之前对其进行转换。第三个进程将读入一条消息并进一步转换该消息。最后,消息将被发送回第一个进程,并将其打印出来。

这将在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 个答案:

答案 0 :(得分:2)

如果不进行测试,就不应该编写这么多代码。 (有一种更好的方法,我马上就可以了。)但如果你发现自己处于这种状态,这是追踪问题的一种方法。

步骤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),即使我向你指出了你并且你承认了这个问题然后说你已经解决了这个问题。如果那不是' t说服你简化的重要性,一无所获。

编写代码的正确方法是慢慢构建,每一步都要进行测试。您应该在尝试将其连接到大电路之前测试fd3链接。从小而简单的东西开始,一次添加一点复杂性,隔离测试新功能,永远不会添加到无效的代码。