Hi I'm a student and my professor want us to make a C program in LINUX in wich the parent process generates two child processes. The parent will read data from stdin and will send it to his children.
The 1st child will show on screen the text read from his father,to standard output.
The 2nd child will show the read data in a file. When the parent has as input an "exit", he will send termination signals to his children and will exit.
So here I let you the code I did and the errors I get when I have to compile. The teacher want us to program it like the way is shown below , with that structure.
The program is in spanish, so the variable names too,I hope that won't be a problem and you can help me as soon as posible because I'm desperate...THANK YOU!!!!
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
int pidHijo1; // global var for 1st child
int descrTub1[2]; //global var for 1st pipe
int pidHijo2; //global var for 2nd child
int descrTub2[2]; //global var for 2nd pipe
char bufferLectura[256]; //buffer
void ProcesoHijo1(); //1st child process
void ProcesoHijo2(); //2nd child process
void ProcesoPadre(); //parent process
int main(int argc, char *argv[]) {
pipe(descrTub1);
pidHijo1 = fork();
if (pidHijo1 == 0) {
ProcesoHijo1();
return 0;
}
pidHijo2 = fork();
if (pidHijo2 == 0)
ProcesoHijo2();
else
ProcesoPadre();
}
void ProcesoHijo1() {
close(0);
dup(descrTub1[0]);
while (1) {
fgets(bufferLectura, 255, stdin);
printf("%s\n", bufferLectura);
}
}
void ProcesoHijo2() {
int descrFichero;
close(0);
dup(descrTub1[0]);
descrFichero = open("salida.txt", O_RDWR | O_TRUNC, 0600);
while(1) {
fgets(bufferLectura,255,descrTub1[0]);
write(descrFichero, bufferLectura, strlen(bufferLectura));
}
}
void ProcesoPadre() {
close(2);
dup(descrTub1[1]);
printf("[Proceso padre] Introduce un texto, o exit para salir> ");
fgets(bufferLectura, 255);
while(strcmp(bufferLectura, "exit")) {
fprintf(stderr, bufferLectura);
write(descrTub2[0], bufferLectura, strlen(bufferLectura));
printf("<[Proceso padre] Introduce un texto, o exit para salir> "); //[Parent Process] Insert text or exit
fgets(bufferLectura, 255);
}
kill(pidHijo1, SIGTERM);
kill(pidHijo2, SIGTERM);
}
ERRORS:
ejercicio.c: In function ‘ProcesoHijo2’:
ejercicio.c:71: 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’
ejercicio.c:72: warning: incompatible implicit declaration of built-in function ‘strlen’
ejercicio.c: In function ‘ProcesoPadre’:
ejercicio.c:85: error: too few arguments to function ‘fgets’
ejercicio.c:89: warning: format not a string literal and no format arguments
ejercicio.c:90: warning: incompatible implicit declaration of built-in function ‘strlen’
ejercicio.c:93: error: too few arguments to function ‘fgets’