我想知道是否有一种方法可以暂停进程但继续执行程序。
但是当我击打P4
它结束时,死亡,然后P2创建P5。然后P2 Dies和P1创建P3,并且在那里发生同样的事情。
在流程开始消失之前,我需要创建整个“树”。
不能使用wait
也不waitpid()
是因为它只针对它的儿子。
有办法暂停P4并从其父亲那里继续吗?
如果我不能实现我的目标?
我当前的代码:按以下顺序创建:
P1-> P2-> P4-> P4模具-> P5-> P5模具-> P2模具-> P3-> P6-> P6模具-> P7-> P7模具-> P3模具-> P1模具
#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);
}
进程彼此独立运行,因此您只需要:
使子进程保持活动状态足够长的时间以创建所有进程sleep()
,例如使用,这很容易做到。和
wait()
在创建所有子进程之前,请勿开始使用它们。
这是一个例子:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
pid_t pids[8] = {0};
printf("P1 created (%lu).\n", (unsigned long) getpid());
for ( size_t i = 1; i < 3; ++i ) {
pids[i] = fork();
if ( pids[i] == -1 ) {
perror("fork() error");
exit(EXIT_FAILURE);
}
else if ( pids[i] == 0 ) {
printf("P%zu created (%lu).\n", i + 1, (unsigned long) getpid());
for ( size_t j = 1 + i * 2; j < 3 + i * 2; ++j ) {
pids[j] = fork();
if ( pids[j] == -1 ) {
perror("fork() error");
exit(EXIT_FAILURE);
}
else if ( pids[j] == 0 ) {
printf("P%zu created (%lu).\n", j + 1,
(unsigned long) getpid());
sleep(8 - j);
printf("P%zu exiting.\n", j + 1);
exit(EXIT_SUCCESS);
}
}
for ( size_t j = 2 + i * 2; j >= 1 + i * 2; --j ) {
if ( waitpid(pids[j], NULL, 0) == -1 ) {
perror("waitpid() error");
exit(EXIT_FAILURE);
}
printf("Waited for P%zu (%lu).\n", j + 1,
(unsigned long) pids[j]);
}
printf("P%zu exiting.\n", i + 1);
exit(EXIT_SUCCESS);
}
}
for ( size_t i = 2; i > 0; --i ) {
if ( waitpid(pids[i], NULL, 0) == -1 ) {
perror("waitpid() error");
exit(EXIT_FAILURE);
}
printf("Waited for P%zu (%lu).\n", i + 1, (unsigned long) pids[i]);
}
printf("P1 exiting.\n");
return 0;
}
输出:
paul@horus:~/src/sandbox$ ./procs
P1 created (27206).
P2 created (27207).
P3 created (27208).
P4 created (27209).
P5 created (27210).
P6 created (27211).
P7 created (27212).
P7 exiting.
Waited for P7 (27212).
P6 exiting.
Waited for P6 (27211).
P3 exiting.
Waited for P3 (27208).
P5 exiting.
Waited for P5 (27210).
P4 exiting.
Waited for P4 (27209).
P2 exiting.
Waited for P2 (27207).
P1 exiting.
paul@horus:~/src/sandbox$
请注意,进程的运行顺序本质上是不可预测的,因此,每次运行它时,您得到的结果都可能与上面的略有不同。我没有尝试使它们按顺序创建或退出,除了sleep()
在四个叶子过程中半心半意地进行尝试之外,否则,它们只是为了使它们全部存活足够长的时间。您几乎可以通过策略调用来保证执行和终止顺序sleep()
,或者使用某种形式的进程间通信来保证执行和终止顺序。通常,您不必关心此顺序,而不必关心您的实际工作是否按顺序完成。
但是,它满足了在所有进程开始死亡之前保持所有进程存活的标准。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句