Results 1 to 3 of 3
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 ...
- 11-19-2011 #1Just Joined!
- Join Date
- Nov 2011
- Posts
- 1
I need help please...why my program is crashing?
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 soon because I'm desperate...THANK YOU!!!!
ERRORS:Code:#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <signal.h> int pidHijo1; // global var of 1st child int descrTub1[2]; //global var of 1st pipe int pidHijo2; //global var of 2nd child int descrTub2[2]; //global var of 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> "); //[Parent Process] Insert text or exit 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); }
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’
- 11-19-2011 #2
Take a look at the man page for fgets(...). Some of the calls you have to this standard function have an incorrect set of parameters. If you fix that it may well compile. One call is not passing a FILE pointer, but seems to be trying to pass an int, others are missing this parameter altogether.
Linux user #126863 - see http://linuxcounter.net/
- 11-20-2011 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The error message is pretty clear. On line 85 your call to the fgets() function is incorrect. The third argument is the I/O stream you are reading from, stdin in this case, so your call should be:
You might also want to test for the return value, which will be NULL if an error occurs, or an EOF (Ctrl-D) with no data is input.Code:fgets(bufferLectura, 255, stdin);
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote