C 我有一个问题与2先进先出读取和写入在每个

C 我有一个问题与2先进先出读取和写入在每个,c,stream,fifo,C,Stream,Fifo,所附代码应允许两个终端之间进行通信。通过在当前目录中创建的2个FIFO进行通信。该程序必须打开2个fifo,子读取标准输入法,并将其置于fifo上,父读取另一个fifo,并在终端上打印。以这种方式进行通信,因为对程序的调用是:./myprog fifo1 fifo2(对于第一个终端)和./myprog fifo2 fifo1(对于第二个终端)。代码工作不好,我怀疑在fifo上执行的child write()工作不好。希望我能解释清楚,帮帮meeee:'( 定义GNU源 #包括 #包括 #包括 #

所附代码应允许两个终端之间进行通信。通过在当前目录中创建的2个FIFO进行通信。该程序必须打开2个fifo,子读取标准输入法,并将其置于fifo上,父读取另一个fifo,并在终端上打印。以这种方式进行通信,因为对程序的调用是:./myprog fifo1 fifo2(对于第一个终端)和./myprog fifo2 fifo1(对于第二个终端)。代码工作不好,我怀疑在fifo上执行的child write()工作不好。希望我能解释清楚,帮帮meeee:'(

定义GNU源
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
如果(argcfd=STDIN_FILENO;
fd_set_c->events=POLLIN;
如果((num_poll_c=poll(fd_set_c,1,-1))=-1)
perror(“错误轮询子项”);//在中对fifo_进行轮询
if((长度=getline(缓冲区输入和尺寸缓冲区输入))=-1)
perror(“错误获取行”);
printf(“写入的字是::%s\n”,*buffer\u in);/*我的控件,用于查看buffer\u in中的内容*/
如果((写入=写入(fifo输入,*缓冲区输入,dim buff))=-1)
perror(“错误写入”);
}
else/*父亲*/
{   
fd\u set\u f->fd=fifo\u out;
fd_set_c->events=POLLIN;
如果((num_poll_f=poll(fd_set_f,15000))=-1)
perror(“错误轮询父”);//在fifo输出时轮询
如果((读取计数=读取(fifo输出,*缓冲区输出,SSIZE最大值))=-1)
perror(“错误读取”);//在fifo输出时读取
对于(i=0;i您应该使用管道(man 2管道或共享内存:man shmget)在进程和信号量之间进行通信,以保护读/写。在google上查找“生产者/消费者”

看看这个: 还有这个:

你这里一团糟

父进程可能会从FIFO中阻塞
read()
,因为它仍然有打开的可写文件描述符。打开的可写文件描述符属于父进程本身。并且由于它在
read()
中被阻塞,因此无法写入

将FIFO的打开置于
fork()
之后。以更严格的模式打开:仅用于写入或读取,而不是通配符O_RDWR。这样,您将镜像通常应用于管道()的逻辑


此外,poll()在STDIN_FILENO上进行缓冲是危险的,因为stdio是缓冲的。文件描述符可能没有什么可读取的内容-因为它以前已经被读取过,但在stdio缓冲区内。如果您确实知道自己在做什么,至少尝试禁用缓冲,
man setvbuf
。但在fork()在场的情况下,我仍然不会这么做:缓冲数据可能会被读取两次:一次由孩子读取,一次由家长读取。

Mmm…我对您的回答有点困惑:1-这不是普通管道,而是命名管道(man 3 mkfifo)2-数据受轮询保护(man 2 poll)3-我不认为我必须为FIFO创建共享内存!是否?
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>

int main(int argc,char* argv[])
{
    if(argc<3)
    {
        printf("Error: Too few arguments...\n");
        exit(-1);
    }

    char** buffer_in=(char**) malloc(sizeof(char*));
    char** buffer_out=(char**) malloc(sizeof(char*));
    size_t dim_buff=sizeof(char*);
    FILE* stream;
    FILE* input;
    int fifo_in, fifo_out, num_poll_c, num_poll_f, read_count, i,write_b;
    pid_t pid;
    ssize_t length;
    struct pollfd* fd_set_c=(struct pollfd*) malloc(sizeof(int));//for the child
    struct pollfd* fd_set_f=(struct pollfd*) malloc(sizeof(int));//for the father


    printf("Write character e press enter:\n");

    if((fifo_in=open(argv[1],O_RDWR|O_NONBLOCK))==-1)
        perror("error open");
    if((fifo_out=open(argv[2],O_RDWR|O_NONBLOCK))==-1)
        perror("error open");

    if((input=fdopen(STDIN_FILENO,"r"))==NULL)
        perror("error fdopen");


    if((pid=fork())==-1)
        perror("error fork");
    while(1)
    {   
        if(pid==0)  /*child*/   
        {   
            fd_set_c->fd=STDIN_FILENO;
            fd_set_c->events=POLLIN;
            if((num_poll_c=poll(fd_set_c, 1, -1))==-1)
                perror("error poll child");//poll on fifo_in
            if((length=getline(buffer_in,&dim_buff,input))==-1)
                perror("error getline");



                printf("The written word is::%s\n",*buffer_in);/*my control for see what in buffer_in is*/


            if((write_b=write(fifo_in,*buffer_in,dim_buff))==-1)
                perror("error write");

        }

        else    /*father*/
        {   
            fd_set_f->fd=fifo_out;
            fd_set_c->events=POLLIN;

            if((num_poll_f=poll(fd_set_f, 1, 5000))==-1)
                perror("error poll father");//poll on fifo_out      
            if((read_count=read(fifo_out,*buffer_out,SSIZE_MAX))==-1)
                perror("error read");//read on fifo_out
            for(i=0;i<=read_count;i++)
                printf("%s",buffer_out[i]);//print on stdout buffer_out


        }
    }
    return 0;   

}