2011-02-27 52 views
1

我有一個簡單的程序即時通訊使用叉子和管道進行學習的問題。 我想要一個發送一些數據給父母的孩子,並且這個(父母)再次發送給孩子。叉+管問題

結果是,父母的行爲就像管道是非阻塞的,並且如果管道阻塞,則子行爲。但我沒有使用任何代碼來告訴管道根本沒有阻塞。

讓我告訴它:

#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; 
} 

的實際行爲是孩子接受輸入,然後stucks閱讀 'pip_pare [0]'。同時,父進程正在循環讀取,並且始終從'pip_fill [0]'讀取值爲0.

所以,我對此有點困惑,爲什麼父母正在讀取和循環,讀'功能?

任何建議來解決它?

感謝您的幫助:)

LLORENS

回答

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

我想你可能是指sizeof(cad)這裏。

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

and nbytes here。

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

而這個沿着類似的路線,但這個版本中有一個隱藏的陷阱,我將作爲一個練習離開!

+0

嗨,它現在解決了,是閱讀大小的問題。感謝您的意見:) – 2011-02-27 22:48:50

0

發送可變長度數據時,最好先發送長度(固定長度的整數),以便接收者知道要請求多少數據。

+0

感謝您的評論:)我會盡力去做。 – 2011-02-27 22:49:32