[英]Make a process wait for it's 'brothers' processes
I'm trying to create a full tree of processes without killing the parents / child. 我正在尝试创建一棵完整的进程树,而不会杀死父母/孩子。
SO far I could only create one side of the tree, then It kills all the process (bottom to top) then I create the other side of the tree. 到目前为止,我只能创建树的一侧,然后杀死所有进程(从下到上),然后创建树的另一侧。
What do I need to do to create the whole tree before the processes ends and 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; // P1
printf("I'm P1: %d | my dad: %d\n", getpid(), getppid());
idProcesso = fork();
switch(idProcesso){
case -1: exit(-1); //ERROR
case 0: //P2
printf("I'm P2: %d | my dad P1: %d\n", getpid(), getppid());
idProcesso = fork();
switch(idProcesso){
case -1: exit(-1); //Error
case 0: //P4
printf("I'm P4: %d | my dad P2: %d\n", getpid(), getppid());
break;
default: //Continue P2
wait(&status);
printf("I'm P2: %d | Already waited for my son P4: %d\n", getpid(), idProcesso);
idProcesso = fork(); //P5
switch(idProcesso){
case -1: exit(-1); //ERROR
case 0: //P5
printf("I'm P5: %d | my dad P2: %d\n", getpid(), getppid());
break;
default: //Continue P5
wait(&status); //P2 waits his son P5
printf("I'm P2: %d | Already waited for my son P5: %d\n", getpid(), idProcesso);
break;
}
}
break;
default: //Continue P1
wait(&status);
printf("I'm P1: %d | Already waited for my son P2: %d\n", getpid(), idProcesso);
idProcesso = fork(); //P1 creates son
switch(idProcesso){
case -1: exit(-1); //ERROR
case 0://P3
printf("I'm P3: %d | my dad P1: %d\n", getpid(), getppid());
idProcesso = fork(); //P3 creates son P6
switch(idProcesso){
case -1: exit(-1); //ERROR
case 0: //P6 son of P3
printf("I'm P6: %d | my dad P3: %d\n", getpid(), getppid());
break;
default: //Continue P3
wait(&status); //P3 waits his son P6
printf("I'm P3: %d | Already waited for my son P6: %d\n", getpid(), idProcesso);
idProcesso = fork(); //P3 creates son P7
switch(idProcesso){
case -1: exit(-1);//ERROR
case 0: //P7 son of P3
printf("I'm P7: %d | son of P3: %d\n", getpid(), getppid());
break;
default: //P3 waits son P7
wait(&status);
printf("I'm P3: %d | Already waited for my son P7: %d\n", getpid(), idProcesso);
break;
}
break;
}
break;
default: //Continue P1
wait(&status); // P1 waits his son P3
printf("I'm P1 again, my id: %d\n", getpid());
t = clock() - t;
time_taken = ( (double)t ) / CLOCKS_PER_SEC;
printf("Time used in seconds: %f\n", time_taken);
}
break;
} //SWITCH
} //Main
I need to do something like this: 我需要做这样的事情:
What is happening with my currently code: 我当前的代码正在发生什么:
P1 -> P2 -> P4 -> Kill P4 -> P5 -> KILL P5 -> KILL P2
P1-> P2-> P4->杀死P4-> P5->杀死P5->杀死P2
P3 -> P6 -> KILL P6 -> P7 -> KILL P7 -> KILL P3 -> KILL P1P3-> P6-> KILL P6-> P7-> KILL P7-> KILL P3-> KILL P1
I need to let them all 'alive' at same time, jusst then I kill them all. 我需要让他们全部同时“活着”,然后我杀死了他们全部。
You can use signal. 您可以使用信号。 You have to save the id of P4 and after P4 executes the printf you call
pause()
. 您必须保存P4的ID,在P4执行printf之后,您调用
pause()
。 Then do it for P5 and P6. 然后针对P5和P6执行此操作。 When you create the last one, in this case the P7, the tree is completed and you send a signal to the process that was paused using
kill()
, making them end the execution. 当您创建最后一个(在本例中为P7)时,树已完成,并且您向使用
kill()
暂停的进程发送了信号,使它们结束执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.