2011-11-24 41 views
2

我想在UNIX中生成父進程生成兩個子進程的C程序。 父母會從stdin讀取數據並將其發送給他的孩子。 第一個孩子將在屏幕上顯示從他父親那裏讀取的文本到標準輸出。 第二個孩子將在文件中顯示讀取的數據。當父母輸入「退出」時,他會發送終止信號給他的孩子,並將退出。fgets在這個程序

所以這裏是我完成的代碼,但我需要幫助的地方是void ProcesoHijo2()。我仍然有一個警告,當我要編譯,這一個:

74: warning: passing argument 3 of ‘fgets’ makes pointer from integer without a cast 
/usr/include/stdio.h:624: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘int’ -->in 
void ProcesoHijo2() 

該計劃是在西班牙,所以變量名也一樣,我希望那不會是一個問題,你可以很快幫助我,因爲我絕望......謝謝你!

#include <stdio.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <string.h> 
#include <fcntl.h> 
#include <signal.h> 

int pidHijo1; 
int descrTub1[2]; 

int pidHijo2; 
int descrTub2[2]; 

void ProcesoHijo1(); 
void ProcesoHijo2(); 
void ProcesoPadre(); 

int main(int argc, char *argv[]) { 


    pipe(descrTub1); 
    pipe(descrTub2); 
     pidHijo1 = fork(); 

     if (pidHijo1 == 0) { 
      ProcesoHijo1(); 
      return 0; 
     } 

    pidHijo2 = fork(); 

     if (pidHijo2 == 0) 
      ProcesoHijo2(); 
     else 
      ProcesoPadre(); 
} 


void ProcesoHijo1() { 
char bufferLectura[256]; 

    close(0); 
    dup(descrTub1[0]); 

    while (1) { 
     fgets(bufferLectura, 255, stdin); 
     printf("%sn", bufferLectura); 
    } 
} 

void ProcesoHijo2() { //-->So here is where I have trouble to program...I don't know if it's just something from inside this process or from above... 
char bufferLectura[256]; 
int descrFichero; 

    close(0); 
    dup(descrTub1[0]); 

    descrFichero = open("salida.txt", O_CREAT | O_TRUNC, 0600); 
    descrFichero = open("salida.txt", O_RDWR | O_TRUNC, 0600);//-->do I need this one? 

    while(1) { 
     fgets(bufferLectura,255,descrTub1[0]); //-->I have to read it from pipe and save it in bufferLectura, but I don't know how to put it... 
     write(descrFichero, bufferLectura, strlen(bufferLectura)); 
     descrFichero = open("salida.txt", O_WRONLY|O_APPEND, 0600); //-->Is this correct? 
    } 
} 

void ProcesoPadre() { 
char bufferLectura[256]; 

    close(2); 
    dup(descrTub1[1]); 

    printf("[Proceso padre]Introduce un texto, o exit para salir"); 
    fgets(bufferLectura, 255,stdin); 

    while(strcmp(bufferLectura, "exitn")) { 
     fprintf(stderr,"%s/n", bufferLectura); 
     write(descrTub2[1], bufferLectura, strlen(bufferLectura)); 
     printf("[Proceso padre] Introduce un texto, o exit para salir "); 
     fgets(bufferLectura, 255,stdin); 
    } 

    kill(pidHijo1, SIGTERM); 
    kill(pidHijo2, SIGTERM); 

} 

回答

2

pipe是OS /較低的水平,併爲您提供了一個UNIX文件描述符(int)。 fgets是libstdc /更高級別並使用FILE*。您可以從文件低級別樣式中讀取(OS功能讀取),或者您可以使用fdopen爲您的descrTub1[0]獲得FILE*

2

函數fgets想要一個指向FILE的指針,同時給它一個文件描述符,它是一個int

您可以通過使用該功能fdopen得到一個文件描述符FILE指針:

FILE *fp = fdopen(descrTub1[0], "r"); 

和使用,在調用fgets

fgets(bufferLectura,255,fp); 
+0

謝謝,但仍然不工作,雖然警告已經消失。也許我把它放在了錯誤的地方,我不知道,但我沒有創建任何.txt文件,程序也沒有要求我介紹文字,所以......無論如何,謝謝。 – user1064131

0

dup返回文件描述符,分別是int &沒有文件流,即FILE *,所以你得到的警告。使用read而不是fgets或使用fdopen獲取文件流FILE*以便文件描述符與fgets一起使用。一旦您完成操作,根據您的需要打開文件只有一個打開的電話while之外打開電話和close。當你使用了大量的系統調用查詢失敗使用perrorsrterror的情況下返回值&打印有意義的錯誤信息(這是調試有用)或許真的在這些線路上:

if(dup(descrTub1[0]) < 0) 
{ 
    perror("dup"); 
    /*Handle error */ 
} 

希望這有助於!