gpt4 book ai didi

c - 如何使进程在等待带有 pause() 的信号但未收到信号时继续执行

转载 作者:太空宇宙 更新时间:2023-11-04 04:33:51 24 4
gpt4 key购买 nike

我是论坛的新手,我一直在谷歌上搜索我的问题,但没有找到。无论如何,我不是很擅长在论坛中搜索,所以如果这个问题已经得到解答,请原谅。好的,让我们进入正题:我正在为大学做一个小项目,其中我有以下用 C 编写的代码:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

int ARB,A,B,X,Y,Z,i;

void senyal(){ //signal USR1 method
if(getpid()==A){
printf("I'm A process, I've received the signal\n");
system("pstree");
}

else if(getpid()==B){
printf("I'm B process, I've received the signal\n");
system("pstree");
}
else if(getpid()==X){
printf("I'm X process, I've received the signal\n");
execlp("ls", "ls", "-al", "*.c", NULL);
}
else if(getpid()==Y){
printf("I'm Y process, I've received the signal\n");
execlp("ls", "ls", "-al", "*.c", NULL);
}
}

int main (int argc, char *argv[]){
char p=argv[1][0];
int num=(int)p;

pid_t pid;
pid_t pid2;
pid_t pid3;
ARB=getpid();
pid=fork(); //Creates father ARB and A child
if(pid==-1){
printf ("Error");
}
else if(pid==0){ // A child
printf ("I'm A process: my pid is %d. My father is %d \n",getpid(), ARB);
A=getpid();
if(num==65||num==97){
num=A;}
pid2=fork(); //A is the father and creates B child
if(pid2==-1){
printf ("Error2");}
else if(pid2==0){// B child
B=getpid();
if(num==66||num==98){
num=B;}
printf("I'm B process , my pid is %d. My father is %d. My grandfather is %d \n",getpid(),getppid(),ARB);
pid3=fork();//Creates X
switch(pid3){
case -1: printf("Error3");
break;
case 0: printf("I'm X process, my pid is %d. My father is %d. My grandfather is %d. My great grandfather is %d\n",getpid(),getppid(),A,ARB);
X=getpid();
if(num==88||num==120){
num=X;}
signal(10,senyal);
printf("I'm X (%d) and I die\n",X);
exit(X);
break;
default:
break;
}
pid3=fork();//Creates Y
switch(pid3){
case -1: printf("Error no he podido crear el proceso hijo\n");
break;
case 0: printf(I'm Y process, my pid is %d. My father is %d. My grandfather is %d. My great grandfather is %d\n",getpid(),getppid(),A,ARB);
Y=getpid();
if(num==89||num==121){
num=Y;}
signal(10,senyal);
printf("I'm Y (%d) and I die\n",Y);
exit(Y);
break;
default:
break;
}
pid3=fork();//Creates Z
switch(pid3){
case -1: printf("Couldn't create children proccess\n");
break;
case 0: printf("I'm Z process, my pid is %d. My father is %d. My grandfather is %d. My great grandfather is %d\n",getpid(),getppid(),A,ARB);
Z=getpid();
printf("I'm Z (%d) and I die\n",Z);
sleep(argc); //

kill(num,SIGUSR1);//sends a signal to any of the other process (A/B/X/Y)
exit(Z);

break;
default:
break;
}
wait();//wait for the childrens' death
wait();
wait();
printf("I'm B(%d) and I die\n",B);
exit(B);//B's death
}

else{//Father A
signal(10,senyal);
wait();//await B's death
printf("I'm A (%d) and I die\n",A);
exit(A); //A's death

}

}
else{//Padre ARB
printf ("\nI'm the process arb: my pid is %d\n",getpid());
signal(10,senyal);
wait(); //await A's death
}
wait();




printf("I'm arb (%d) and I die\n",ARB);
exit (0);//ARB's death

}

所以基本上代码应该做的是创建一个进程树,然后接收一个参数,该参数指示需要接收进程 Z 发送的 USGR1 类型信号的进程的 PID。我的问题是我希望每个进程都准备好接收信号,但如果进程没有收到信号(例如进程 A 正在等待并准备好接收来自 Z 的信号但 Z 将其发送给 B ) 我不知道如何告诉该过程继续进行,而不是永远陷入停顿状态。基本上就是这样,对不起,如果我犯了任何语法或解释错误,但顺便说一句,我来自西类牙。感谢任何愿意回答的人!

最佳答案

来自 pause() 的手册页:

DESCRIPTION

pause() causes the calling process (or thread) to sleep until a signal is delivered that either terminates the process or causes the invocation of a signal-catching function.

所以你需要另一个 thread 延迟 sleep() 或类似的东西,并在一段时间后发送信号。或者使用暂停以外的其他方式,例如 sleep 。

关于c - 如何使进程在等待带有 pause() 的信号但未收到信号时继续执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33285805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com