[英]How to make a process continue it's execution when it's waiting a signal with pause() and it doesn't recieve it
我是該論壇的新手,我一直在Google搜索和搜索我的問題,但沒有找到它。 無論如何,我不是在論壇中搜索的專家,因此,如果已經回答了,請原諒。 好吧,讓我們說清楚一點:我正在為大學做一個小項目,我用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()的手冊頁中:
描述
pause()使調用進程(或線程 )進入休眠狀態,直到傳遞了終止進程或導致捕獲信號的函數的信號為止。
因此,您需要另一個因sleep()或類似原因而延遲並在一段時間后仍然發送信號的線程 。 或者使用除暫停以外的其他方法,例如睡眠。
要等待信號或超時,請使用select
, pselect
, poll
或ppoll
。
p
版本用於自定義信號掩碼,這可能是您需要的,但在這種情況下可能不需要。
這些函數旨在采用文件描述符集,但它們完全不需要任何文件描述符就可以很好地工作。
有一陣子了。 很抱歉花了這么長時間回答,並再次感謝所有回答的人,您的建議非常有用。 因此,我終於找到了解決問題的方法。 在嘗試暫停,睡眠和很多東西之后,並沒有真正到達我想要的位置,我意識到我可以使用信號“警報”。 我使用了pause()命令使進程等待信號,但是這次我在進程Z中設置了SIGALARM信號,該信號可能會向其他進程拋出信號。 因此,無論如何,當Z發出信號並且將要死亡時,它都會發出警報,將每個進程從其暫停狀態喚醒。 這解決了我的問題,所以我希望這篇文章對其他人有幫助。 再次感謝大家!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.