简体   繁体   English

叉+管问题

[英]Fork + Pipe problem

I have a problem with a simple program im making with fork and pipes for learning purpose. 我有一个简单的程序我用叉子和管道制作用于学习目的的问题。 I want a child that send some data to the parent and this (the parent) send it again to the child. 我想要一个将一些数据发送给父母的孩子,然后这个(父母)再次将数据发送给孩子。

The result is that the parent acts like the pipe is non-blocking and the child acts if the pipe is blocking. 结果是父级的行为就像管道是非阻塞的,子级的行为就像管道是非阻塞的。 But i didnt use any code to tell that the pipes are non-blocking at all. 但是我没有使用任何代码来告诉管道完全没有阻塞。

Let me show it: 让我展示一下:

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <conio.h>

void WriteStr(char* inStr)
{
    write(1,inStr,strlen(inStr));
}

int main(void){

    char *cad1 = "Introdueix un valor:  ";
int bytesr = 0;
char valor[256];

//Un pipe serveix solament per una comunicació unidereccional, 
//si es necessari que pare i fill es comuniquin en amdos sentits, 
//s'hauran d'utilitzar dos pipes.

int pip_fill[2];    //pip_fp[0]: file descriptor de lectura
                //pip_fp[1]: file descriptor d'escriptura

int pip_pare[2];    //pip_pf[0]: file descriptor de lectura
                //pip_pf[1]: file descriptor d'escriptura
int pid, nbytes = 0;
char cad[256];
int num = 0;

if (pipe (pip_fill)<0){
    write (1,"Pipe error!!!!\n",15);
    exit (1);
}

if (pipe (pip_pare)<0){
    write (1,"Pipe error!!!!\n",15);
    exit (1);
}

fflushnou();

pid = fork();
switch (pid){
    case -1:    
            //Error
            write (2,"Tractar_Valor: Error!!!!\n",25);
            exit (-1);
    case 0:
            //Fill
            //printf("Fill: fp[0]:%d fp[1]:%d pf[0]:%d pf[1]:%d \n",pip_fp[0],pip_fp[1],pip_pf[0],pip_pf[1]);
            close(pip_fill[0]);
                close(pip_pare[1]);

            while(1){
                bytesr = read(0, valor, 256);

                valor[bytesr] = '\0';

                write(pip_fill[1],valor,strlen(valor)); //el fill escriu a la pipe la var valor

                WriteStr("Fill avans\n");
                nbytes = read(pip_pare[0],cad,strlen(cad)); //el fill llegeix de la pipe la var un cop ha estat tractada per el pare
                WriteStr("Fill despres\n");

                if (nbytes != 0){   //vol dir que hem llegit algo per la pipe pip_pf
                    write(1,cad,strlen(cad));   //pintem cad per pantalla
                }

                sleep(1);
            }
    default:
            //Pare
            close(pip_fill[1]);
            close(pip_pare[0]);
            close(0);

            while(1){   

                nbytes = read(pip_fill[0],valor,strlen(valor));//el pare llegeix de la pipe la var valor 
                //WriteStr("Pare despres\n");
                if (nbytes != 0){   //vol dir que hem llegit algo per la pipe pip_fp
                    //tractem la variable valor
                    num = atoi(valor);
                    num = num*2;

                    sprintf(cad,"Valor actual de la variable: %d \n\n",num);

                    write(1,cad,strlen(cad));

                    write(pip_pare[1],cad,strlen(cad)); //el pare escriu a la pipe la var tractada
                }

                sleep(1);
            }
}
return 0;
}

The actual behaviour is that child accepts input, and then stucks reading 'pip_pare[0]'. 实际的行为是孩子接受输入,然后卡住读“ pip_pare [0]”。 At the same time, parent process is looping and reading all the time from 'pip_fill[0]' a value of 0. 同时,父进程正在循环并一直从'pip_fill [0]'读取值为0。

So, i am little confused about that, why parent is reading and loopinf without bloking in the 'read' function ?? 所以,我对此一点都不感到困惑,为什么父母在不读取“ read”功能的情况下进行读取和loopinf?

Any suggestion to fix it ? 有什么建议可以解决吗?

Thanks for the help :) 谢谢您的帮助 :)

LLORENS 劳伦斯

nbytes = read(pip_pare[0],cad,strlen(cad));

I think you probably meant sizeof(cad) here. 我认为您可能在这里指的是sizeof(cad)

write(1,cad,strlen(cad));

And nbytes here. 还有nbytes

nbytes = read(pip_fill[0],valor,strlen(valor));

And this along similar lines, but there's a hidden trap in this version which I shall leave as an exercise! 和这类似,但是在这个版本中有一个隐藏的陷阱,我将在练习中留下!

发送可变长度的数据时,最好先发送长度(以固定长度的整数形式),以便收件人知道要请求多少数据。

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

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