繁体   English   中英

Linux IPC管道无法正常工作

[英]Linux IPC pipe is not working

所以我不熟悉linux中的进程间通信和进程,所以我真的无法弄清楚问题是什么。 我编写的以下程序与我在家庭作业中使用的问题相同,包括使用缩减的管道。 它基本上是从孩子发送一个字符到父母,但它不会打印出该字符。
它打印出来:

hello from child
sending a
hello from parent
trying to receive...
received: reaping child

它应该说在第三行的哪个位置

received: a

任何答案都表示赞赏,如果您对该计划中的任何其他内容有任何有用的批评。 感谢大家

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


int main(int argc, char* argv[])
{
    pid_t pid = fork();
    int comm[2];

    pipe(comm);

if (pid == 0)
{
    char send = 'a';
    int check;
    close(comm[0]);
    printf("hello from child\n");
    printf("sending %c\n", send);
    check =  write(comm[1], &send, 1);
    printf("%d\n", check);
    exit(1);
}
else if (pid > 0)
{
    char get = ' ';
    int check;
    close(comm[1]);
    printf("hello from parent\n");
    printf("trying to receive...\n");
    read(comm[0], &get, 2);
    printf("received: %c\n", get);
    printf("reaping child\n");
    wait(NULL);
    return 0;
}

return 0;

}

你的管子和叉子的顺序错了! 您的进程分叉,然后两个进程都调用管道,因此正在创建2个单独的管道。 你写的那个人没有人读它,而你正在读的那个没有写任何东西。

暂无
暂无

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

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