I'd like to know if there is a way to pause a process but continue the program.
I need to create processes like this:
But when I hit P4
it ends, dies and then P2 creates P5. Then P2 Dies and P1 creates P3 and the same thing happens there.
I need to create the whole 'tree' before the processes start to die.
Can't use wait
nor waitpid()
because it only targets it's son.
Is there a way to Pause P4 and continue from it's father ?
If I can't how could I achieve my goal ?
My currently code: It's creating in this order:
P1 -> P2 -> P4 -> P4 DIES -> P5 -> P5 DIES -> P2 DIES -> P3 -> P6 -> P6 DIES -> P7 -> P7 DIES -> P3 DIES -> P1 DIES
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
int main(){
clock_t t;
double time_taken;
t = clock();
int status;
pid_t idProcesso, pai;
printf("P1 created: %d\n", getpid());
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0: //P2
printf("P2 created: %d - son of P1: %d\n", getpid(), getppid());
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0: //P4
printf("P4 created: %d - son of P2: %d\n", getpid(), getppid());
sleep(1);
break;
default://P2
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0://P5
printf("P5 created: %d - son of P2: %d\n", getpid(), getppid());
break;
default://P2
sleep(1);
break;
}
break;
}
break;
default:
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0://P3
printf("P3 created: %d - son of P1: %d\n", getpid(), getppid());
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0://P6
printf("P6 created: %d - son of P3: %d\n", getpid(), getppid());
sleep(1);
break;
default://P3
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0://P7
printf("P7 created: %d - son of P3: %d\n", getpid(), getppid());
break;
default://P3
break;
}
sleep(1);
break;
}
break;
default:
sleep(4);
break;
}
break;
}
printf("Process id: %d terminated\n", getpid());
exit(0);
}