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