【问题标题】:Transferring a message between three pipes在三个管道之间传输消息
【发布时间】:2015-05-19 01:21:19
【问题描述】:

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

这将在 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);
 }

【问题讨论】:

  • 先试试更简单的。
  • 不确定您的意思,以及如何解决。我尝试遵循一个只有一个孩子和一个父母的例子,并且可以正常工作,所以我只是添加了另一个孩子。这可能就是我出错的地方。
  • 好吧,你同时尝试了几件事;哪一个失败了?有错误信息吗?父进程是否收到来自用户的字符串?第一个孩子是通过fd2接收的吗?它是否正确反转字符串?第二个孩子是否通过fd3接收到反转的字符串?它是否正确更改大小写?父级是否通过 fd 接收大写字符串?你能隔离出不工作的东西吗?
  • 你忘记pipe(fd3)了吗?
  • 除非这是家庭作业,否则最好使用 shell 进行管道处理,并将 C 程序编写为简单的 stdin-to-stdout 处理器。您可以使用 mkfifo 来帮助创建流程“循环”。

标签: c unix operating-system pipe


【解决方案1】:

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

第 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 步: 在不改变错误行为的情况下尽可能简化代码,直到 bug 无处可藏。在这种情况下,您会发现您仍然忽略了pipe(fd3),即使我向您指出了这一点并且您承认了问题并说您已经解决了问题。如果这不能令人信服你知道简化的重要性,什么都不会。

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

【讨论】:

  • 父进程发送消息 Hi 第一个孩子收到消息 Hi 第一个孩子发送消息 第二个孩子收到消息 iH 第二个孩子发送消息 IH 父进程收到消息乱码(一个正方形)
  • 由于某种原因无法正确格式化。对不起。
  • @MatthewWeingarten:所以simplify。这个错误会扑向你,你可以通过移动一行来修复它。最好将大部分代码从main() 中提取出来,并将其放入firstChild(...)secondChild(...)parent(...) 等函数中。
  • 在我看来,错误在于将消息从第二个孩子传回父母之间。这意味着我不会关闭所有频道或关闭太多频道,对吗?也许我也在错误的地方关闭它们。然而,我尝试的所有事情都没有改变效果。
  • @MatthewWeingarten:你简化了吗?当您可以消除除一个之外的所有内容时,您为什么要谈论“渠道”,复数形式?
猜你喜欢
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
相关资源
最近更新 更多