繁体   English   中英

无法将消息从子进程发送回父进程

[英]Unable to send a message from a child process back to the parent process

我试图了解管道和分叉的工作方式。 因此,我编写了一个简单的程序,其中父进程向子进程发送一条消息(工作正常)。 但是,如果我尝试通过添加注释的代码从子进程发送回消息,它将停止工作。 输出“父母已发送:你好”后,程序停止执行。

int main() {
    int child_to_parent[2];
    int parent_to_child[2];
    pipe(child_to_parent);
    pipe(parent_to_child);

    pid_t id = fork();

    if (id == 0) {
        close(parent_to_child[1]);
        close(child_to_parent[0]);
        FILE* out = fdopen(child_to_parent[1], "w");
        FILE* in = fdopen(parent_to_child[0], "r");

        char msg[6];
        fscanf(in ,"%s", msg);
        printf("Child got: %s\n", msg);
        /*
        fprintf(out, "hi ");
        printf("Child sent: hi\n"); 
        */
    } else {
        close(parent_to_child[0]);
        close(child_to_parent[1]);
        FILE* in = fdopen(child_to_parent[0], "r");
        FILE* out = fdopen(parent_to_child[1], "w");

        fprintf(out, "hello");
        printf("Parent sent: hello\n");
        /*
        char msg[3];
        fscanf(in, "%s", msg);
        printf("Parent got: %s\n", msg);
        */
    }
}

我不知道为什么。 最让我感到困惑的是为什么修改代码后子进程甚至无法收到消息。 谁能告诉我哪里出了问题,或将我引导向正确的方向?

使用读/写交换任意数量消息的第一种解决方案

在这里,我用\\ n指示要读取的每个缓冲区的结尾:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

void rd(int fd, int child)
{
  char c;
  int first = 1;

  do {
    if (!read(fd, &c, 1))
      break;
    if (first) {
      printf("%s got:", (child) ? "Child" : "Parent");
      first = 0;
    }
    putchar(c);
  } while (c != '\n');
}

void wr(int fd, const char * msg, int child)
{
  write(fd, msg, strlen(msg));
  printf("%s sent:%s", (child) ? "Child" : "Parent", msg);
}

int main() {
    int child_to_parent[2];
    int parent_to_child[2];

    pipe(child_to_parent);
    pipe(parent_to_child);

    pid_t id = fork();

    if (id == 0) {
      close(parent_to_child[1]);
      close(child_to_parent[0]);

      rd(parent_to_child[0], 1);
      wr(child_to_parent[1], "hi\n", 1);

      rd(parent_to_child[0], 1);
      wr(child_to_parent[1], "fine, and you ?\n", 1);

      rd(parent_to_child[0], 1);
    } else {
      close(parent_to_child[0]);
      close(child_to_parent[1]);

      wr(parent_to_child[1], "hello\n", 0);
      rd(child_to_parent[0], 0);

      wr(parent_to_child[1], "how are you ?\n", 0);
      rd(child_to_parent[0], 0);

      wr(parent_to_child[1], "fine too\n", 0);
    }
}

编译与执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra p.c
pi@raspberrypi:/tmp $ ./a.out 
Parent sent:hello
Child got:hello
Child sent:hi
Parent got:hi
Parent sent:how are you ?
Child got:how are you ?
Child sent:fine, and you ?
Parent got:fine, and you ?
Parent sent:fine too
Child got:fine too

也可以使用fread / fwrite ,在fwrite之后有必要使用fflush才能将其阻止。 幸运的是,发送后不必关闭管道即可读取和应答,否则只能交换一条消息。 我仍然使用\\ n指示发送的缓冲区的结尾:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

void rd(FILE * fd, int child)
{
  char c;
  int first = 1;

  do {
    if (!fread(&c, 1, 1, fd))
      break;
    if (first) {
      printf("%s got:", (child) ? "Child" : "Parent");
      first = 0;
    }
    putchar(c);
  } while (c != '\n');
}

void wr(FILE *  fd, const char * msg, int child)
{
  fwrite(msg, strlen(msg), 1, fd);
  fflush(fd);
  printf("%s sent:%s", (child) ? "Child" : "Parent", msg);
}

int main() {
    int child_to_parent[2];
    int parent_to_child[2];
    pipe(child_to_parent);
    pipe(parent_to_child);

    pid_t id = fork();

    if (id == 0) {
        close(parent_to_child[1]);
        close(child_to_parent[0]);
        FILE* out = fdopen(child_to_parent[1], "w");
        FILE* in = fdopen(parent_to_child[0], "r");

    rd(in, 1);
    wr(out, "hi\n", 1);

    rd(in, 1);
    wr(out, "fine, and you ?\n", 1);

    rd(in, 1);
    } else {
        close(parent_to_child[0]);
        close(child_to_parent[1]);
        FILE* in = fdopen(child_to_parent[0], "r");
        FILE* out = fdopen(parent_to_child[1], "w");

    wr(out, "hello\n", 0);
    rd(in, 0);

    wr(out, "how are you ?\n", 0);
    rd(in, 0);

    wr(out, "fine too\n", 0);
    }
}

编译与执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra pp.c
pi@raspberrypi:/tmp $ ./a.out
Parent sent:hello
Child got:hello
Child sent:hi
Parent got:hi
Parent sent:how are you ?
Child got:how are you ?
Child sent:fine, and you ?
Parent got:fine, and you ?
Parent sent:fine too
Child got:fine too

最后,如果要使用fscanf("%s", ...) ,则需要在该单词后发送一个分隔符(例如,空格或\\ n)以不阻塞fscanf ,当然还要读取分隔符fwrite之后必须执行fflush

如果我稍微改变一下您的程序:

#include <stdio.h>
#include <unistd.h>

int main() {
    int child_to_parent[2];
    int parent_to_child[2];
    pipe(child_to_parent);
    pipe(parent_to_child);

    pid_t id = fork();

    if (id == 0) {
      close(parent_to_child[1]);
      close(child_to_parent[0]);
      FILE* out = fdopen(child_to_parent[1], "w");
      FILE* in = fdopen(parent_to_child[0], "r");
      char msg[16], c;

      fscanf(in ,"%s%c", msg, &c);
      printf("Child got: %s\n", msg);

      fprintf(out, "hi ");
      fflush(out);
      printf("Child sent: hi\n"); 

      fscanf(in ,"%s%c", msg, &c);
      printf("Child got: %s\n", msg);

      fprintf(out, "fine,you? ");
      fflush(out);
      printf("Child sent: fine,you?\n"); 

      fscanf(in ,"%s%c", msg, &c);
      printf("Child got: %s\n", msg);
    } else {
      close(parent_to_child[0]);
      close(child_to_parent[1]);
      FILE* in = fdopen(child_to_parent[0], "r");
      FILE* out = fdopen(parent_to_child[1], "w");

      fprintf(out, "hello\n");
      fflush(out);
      printf("Parent sent: hello\n");

      char msg[16], c;

      fscanf(in, "%s%c", msg, &c);
      printf("Parent got: %s\n", msg);

      fprintf(out, "how-are-you? ");
      fflush(out);
      printf("Parent sent: how-are-you?\n");

      fscanf(in, "%s%c", msg, &c);
      printf("Parent got: %s\n", msg);

      fprintf(out, "fine-too ");
      fflush(out);
      printf("Parent sent: fine-too\n");
    }
}

编译与执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra ppp.c
pi@raspberrypi:/tmp $ ./a.out
Parent sent: hello
Child got: hello
Child sent: hi
Parent got: hi
Parent sent: how-are-you?
Child got: how-are-you?
Child sent: fine,you?
Parent got: fine,you?
Parent sent: fine-too
Child got: fine-too

fscanf读取直到使用%s时找到一些空白字符(由isspace给出),但您没有发送一个空白字符。 因此fscanf永远不会返回,因为它正在等待空白。

使用freadfwrite代替fscanffprintf ,您的管道将正常工作。

在父母和孩子双方,当你写完到管道,用刷新缓冲区fflush和关闭与管道写端close

这是一个稍作修改的程序:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

FILE *fdopen(int fd, const char *mode);

int main() {
    int child_to_parent[2];
    int parent_to_child[2];
    pipe(child_to_parent);
    pipe(parent_to_child);

    pid_t id = fork();

    if (id == 0) {
        close(parent_to_child[1]);
        close(child_to_parent[0]);
        FILE* out = fdopen(child_to_parent[1], "w");
        FILE* in = fdopen(parent_to_child[0], "r");

        char msg[6];
        printf("Child gonna read...\n");
        fscanf(in ,"%s", msg);
        printf("Child got: %s\n", msg);

        fprintf(out, "hi");
        fflush(out);
        printf("Child sent: hi\n");
        close(child_to_parent[1]);
    } else {
        close(parent_to_child[0]);
        close(child_to_parent[1]);
        FILE* in = fdopen(child_to_parent[0], "r");
        FILE* out = fdopen(parent_to_child[1], "w");

        fprintf(out, "hello");
        printf("Parent sent: hello\n");
        fflush(out);
        close(parent_to_child[1]);

        char msg[3];
        printf("Parent gonna read...\n");
        fscanf(in, "%s", msg);
        printf("Parent got: %s\n", msg);
    }
}

运行时的输出:

Parent sent: hello
Parent gonna read...
Child gonna read...
Child got: hello
Child sent: hi
Parent got: hi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM