Mis preguntas son:¿Por qué mis procesos no se ejecutan simultáneamente?
1.) ¿Cómo puedo hacer que el proceso principal siempre muera último? Entiendo que esto no se está logrando porque el padre es el primer pid que se ejecuta, pero no sé cómo cambiarlo.
2.) ¿Cómo hago que mis procesos hijo se ejecuten al mismo tiempo como el suyo? Incluso he subido el número realmente alto para ver si era solo una coincidencia, pero parece que no lo es.
EDIT: SOLUCIONES
1.) agregó wait (NULL) dos veces en defecto interno
2.) que estaba ocurriendo. Usé el sueño (1) para probarlo.
Mi código es el siguiente
#include <stdio.h>
int main() {
int pid, i;
pid = fork();
switch(pid) {
case -1:
// error
printf("Fork error");
break;
case 0:
// child process
printf("First child is born, my pid is %d\n", getpid());
for(i=1; i<10; i++)
printf("First child executes iteration %d\n", i);
printf("First child dies quietly.\n");
break;
default:
// parent process
printf("Parent process is born, my pid is %d\n", getpid());
pid = fork();
switch(pid) {
case -1:
// error
printf("Fork error");
break;
case 0:
// child process
printf("Second child is born, my pid is %d\n", getpid());
for(i=1; i<10; i++)
printf("Second child executes iteration %d\n", i);
printf("Second child dies quietly.\n");
break;
default:
// parent process
printf("Parent process dies quietly.");
}
}
return 0;
}
Mi salida siempre se ve así:
Parent process is born, my pid is 7847 First child is born, my pid is 7848 First child executes iteration: 1 First child executes iteration: 2 First child executes iteration: 3 First child executes iteration: 4 First child executes iteration: 5 First child executes iteration: 6 First child executes iteration: 7 First child executes iteration: 8 First child executes iteration: 9 First child executes iteration: 10 First child dies quietly. Parent process dies quietly. Second child is born, my pid is 7849 Second child executes iteration 1 Second child executes iteration 2 Second child executes iteration 3 Second child executes iteration 4 Second child executes iteration 5 Second child executes iteration 6 Second child executes iteration 7 Second child executes iteration 8 Second child executes iteration 9 Second child executes iteration 10 Second child dies quietly.
Mi misión es:
Escribir un programa en C ("procs.c") que crea tres procesos: un proceso principal que crea dos procesos secundarios.
El primer hijo debe hacer lo siguiente:
pantalla "primer hijo ha nacido, mi pid es ..."
pantalla diez veces el mensaje "Primer niño ejecuta iteración X" , donde X es el número de la iteración
pantalla "El primer hijo muere silenciosamente".
El segundo hijo debe hacer lo siguiente:
pantalla "El segundo niño nace, mi pid es ..."
pantalla diez veces el mensaje "Segundas ejecuta niño iteración X ", donde X es el número de la iteración
pantalla" El segundo hijo muere silenciosamente ".
El proceso padre debe hacer lo siguiente:
"nace proceso padre, mi pid es ..." pantalla
crear el primer hijo
crear el segundo hijo
mostrar "Parent el proceso muere silenciosamente"
compilar el programa con gcc y el nombre de los ejecutables 'procs' Ejecutar el programa varias veces y observe cómo la salida de los dos hijos de entrelazado
Una posible salida de este programa es:..
nova> ./procs Parent process is born, my pid is 7847 First child is born, my pid is 7848 First child executes iteration: 1 First child executes iteration: 2 First child executes iteration: 3 First child executes iteration: 4 First child executes iteration: 5 Second child is born, my pid is 7849 Second child executes iteration 1 Second child executes iteration 2 Second child executes iteration 3 First child executes iteration: 6 Second child executes iteration 4 Second child executes iteration 5 Second child executes iteration 6 First child executes iteration: 7 Second child executes iteration 7 Second child executes iteration 8 Second child executes iteration 9 Second child executes iteration 10 Second child dies quietly. First child executes iteration: 8 First child executes iteration: 9 First child executes iteration: 10 First child dies quietly. Parent process dies quietly.
Mi libro tiene un ejemplo como el mío, excepto que también tiene un bucle for. Y los resultados a menudo se ven como parent1, parent2, child1, child2, child3, child4, parent3, etc. En la mía, ¿por qué el segundo niño no empieza a ejecutar en la mitad de la ejecución del primer hijo? –
Su máquina es demasiado rápida. –
Gracias. Tu ayuda me inspiró a dormir (1) en el proceso de mi hijo para probar que se estaban ejecutando al mismo tiempo. –