繁体   English   中英

在等待带有pause()的信号但没有收到信号的过程中,如何使进程继续执行

[英]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()或类似原因而延迟并在一段时间后仍然发送信号的线程 或者使用除暂停以外的其他方法,例如睡眠。

要等待信号或超时,请使用selectpselectpollppoll

p版本用于自定义信号掩码,这可能是您需要的,但在这种情况下可能不需要。

这些函数旨在采用文件描述符集,但它们完全不需要任何文件描述符就可以很好地工作。

有一阵子了。 很抱歉花了这么长时间回答,并再次感谢所有回答的人,您的建议非常有用。 因此,我终于找到了解决问题的方法。 在尝试暂停,睡眠和很多东西之后,并没有真正到达我想要的位置,我意识到我可以使用信号“警报”。 我使用了pause()命令使进程等待信号,但是这次我在进程Z中设置了SIGALARM信号,该信号可能会向其他进程抛出信号。 因此,无论如何,当Z发出信号并且将要死亡时,它都会发出警报,将每个进程从其暂停状态唤醒。 这解决了我的问题,所以我希望这篇文章对其他人有帮助。 再次感谢大家!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM