I've been practicing an exercise in C where I had to create a simple tree of the process. Basically, the main process creates a child process using fork(), then it ends. The child process prints a message, create a child process of its own and it ends too, this procedure iterates 3 times. Well, apparently it's working OK somewhere in the execution I got a weird output (It prints the directory where the executable is). I'm sharing the code and the output from terminal. (Output messages are in Spanish, sorry about that):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
int i;
pid_t cpid[3];
printf("¡Hola, soy el proceso padre!\n");
for (i=0;i<3; i++){
cpid[i] = fork();
if (cpid[i] == 0){
printf("¡Hola, soy el proceso hijo N°%i!\n",i+1);
}else{
printf ("Terminando proceso %i....\n",i);
exit (EXIT_SUCCESS);
}
}
printf ("Terminando proceso %i....\n",i);
return 0;
}
And this my output:
fuhranku@frank-ubuntuVB:~/Escritorio/Práctica #1 - Solución/Ejercicios programados$ gcc -o foo ejercicio23.c
fuhranku@frank-ubuntuVB:~/Escritorio/Práctica #1 - Solución/Ejercicios programados$ ./foo
¡Hola, soy el proceso padre!
Terminando proceso 0....
fuhranku@frank-ubuntuVB:~/Escritorio/Práctica #1 - Solución/Ejercicios programados$ ¡Hola, soy el proceso hijo N°1! <---- WEIRD OUTPUT
Terminando proceso 1....
¡Hola, soy el proceso hijo N°2!
I don't know why it's showing the directory where the executable file is in the middle of the execution, any idea of the reasons?
Thanks! :-)