【问题标题】:Only one message passes through pipe只有一条消息通过管道
【发布时间】:2011-12-25 19:47:46
【问题描述】:

我一遍又一遍地做这个任务,这大约是第 10 个版本。问题是只有一条消息通过管道,并且计算出正确的结果。以下字符串根本不通过,或者在摆弄缓冲区后只有一些字符。请帮忙,我真的在这个问题上浪费了很多时间,我需要学习这些东西以便即将进行的测试。

#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <stdio_ext.h>

/* Prototypes */

void usage(void);
void calchild(void);

char command[] = "<not yet set>";

int main(int argc, char **argv)
{
  char input1[512];
  char input2[512];
  char tmp[512];
  char *endptr;
  char c = 0;
  int a, b, result;
  pid_t cpid;
  int status = 0;
  int stocpipe[2]; /* Server to client pipe. [0] -read; [1]- write*/
  int ctospipe[2]; /* Client to server pipe.       - || -         */
  int i = 0;
  FILE *send, *receive;

  if(argc > 1)
    {
      usage();
    }

  /* Pipe Setup */
  if(pipe(stocpipe) != 0 || pipe(ctospipe) != 0)
    {
      fprintf(stderr, "ERROR: Can't create unnamed pipe! \n");
      exit(EXIT_FAILURE);
    }

  switch(cpid = fork())
    {
    case -1:
      fprintf(stderr, "ERROR: Can't fork! \n");
      exit(EXIT_FAILURE);
      break;
    case 0:
      /* calchild */
      close(stocpipe[1]);
      close(ctospipe[0]);

      receive = fdopen(stocpipe[0], "r");
      send = fdopen(ctospipe[1], "w");

/*Gets the string from the parent process and does the computation.*/
      while(fgets(input2, 17, receive) != NULL)
        {
      strcpy(tmp, input2);
      fprintf(stdout, "After receive: %s", tmp);
      a = strtol(tmp, &endptr, 10);
      fprintf(stdout, "a = %d\n", a);
      b = strtol(endptr, &endptr, 10); 
      fprintf(stdout, "b = %d\n", b);
      c = endptr[0];

/*Loops until it finds a non-space char*/
      for(i = 0; isspace(c = endptr[i]); i++);

      switch(c)
    {
    case '+':
      /*add*/
      result = a + b;
      break;
    case '-':
      /*subtract*/
      result = a - b;
      break;
    case '*':
      /*multiply*/
      result = a * b;
      break;
    case '/':
      /*divide*/
      result = a / b;
      break;
    default:
      fprintf(stderr, "the funk!? %c\n", c);
      break;
    }

      fprintf(stderr, "%d\n", result);
      fprintf(send, "%d", result);

    }
      break;
    default:

  close(stocpipe[0]);
  close(ctospipe[1]);

  send = fdopen(stocpipe[1], "w");
  receive = fdopen(ctospipe[0], "r");

/*Reads string from stdin and sends it to the child process through a pipe. */
  while(fgets(input1, 17, stdin) != NULL)
    {
      fprintf(stdout, "Before send: %s", input1);
      fwrite(input1, 17, 1, send);

if(fflush(send) == EOF)
    {
      fprintf(stderr, "Flush error!");
  }
}

  (void) waitpid(cpid, &status, 0);
  if(status != 0)
    {
      fprintf(stderr, "ERROR: Child calculator exited with %d \n", status);
    }
      break;
   }


  return 0;
}

void usage(void)
{
  fprintf(stderr,"Usage: %s", command);
  exit(EXIT_FAILURE);
}

该程序是一个计算器。它的目的是学习IPC。父进程接受来自标准输入的字符串(例如 3 5 +)并将其发送给子进程。孩子解析字符串并计算结果。然后它将结果发送回父进程,然后将其打印到标准输出。

我在将字符串发送给孩子的过程中遇到了困难。接受的第一个字符串被发送给孩子。它可以很好地计算结果。第二个字符串和之后的每个字符串都是空的,或者至少看起来是空的。

【问题讨论】:

  • 嵌套了switch/while/swicth 和一个没有身体的for?整洁的。真的,真的很难读。为父处理和子处理创建函数,并尝试稍微取消嵌套您的代码。 (并正确缩进。)
  • 我认为您需要使程序更易于理解。首先尝试使用精简的最小程序显示相同的不良行为;还要解释程序应该做什么。
  • 我已经为无身者添加了评论。 :D 我已经用单独的函数完成了它,甚至是单独的可执行文件,现在它们在 switch 中是分开的。另外,我已经写了它的作用,以及发送和接收完成的位置(发生问题的部分)。问题甚至可能是我选择了错误的缓冲区大小或某事。我在 2 天内找不到问题... :-/
  • 返回单独的函数。这段代码乱七八糟。
  • 是的,至少请调整缩进!

标签: c pipe


【解决方案1】:

注意fwrite(input1, 17, 1, send);这一行。 进程可能在 '\n' 字符之后向 进程发送了随机内容。在 child while(fgets(input2, 17, receive) != NULL) 中,fgets 在得到 '\n' 时停止,可能得到少于 17-1 个字符。它的下一个阅读管道会得到随机的东西。

一个直接的解决方法是fwrite(input1, strlen(input1), 1, send);。提到'man fwrite',最好使用fwrite(input1, sizeof (input1[0]), strlen(input1), send);

无论如何,使用幻数 17 是危险的。请记住,PIPE 是一个连续的字符流。

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多