简体   繁体   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

I'm new to the forum and i've been googling and searching here my problem but didn't find it. 我是该论坛的新手,我一直在Google搜索和搜索我的问题,但没有找到它。 Anyway i'm not very expert in searching in the forum so please excuse me if this has already been answered. 无论如何,我不是在论坛中搜索的专家,因此,如果已经回答了,请原谅。 Ok let's get to the point: i'm doing a small project for college where i have the following code written in C: 好吧,让我们说清楚一点:我正在为大学做一个小项目,我用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

}

So basically what the code is supposed to do is create a proccess tree and then receive a parametrer which indicates the PID of the proccess that needs to recieve the signal of type USGR1 that the process Z is sending. 因此,基本上,该代码应该执行的操作是创建一个过程树,然后接收一个参数,该参数指示需要接收进程Z正在发送的USGR1类型信号的过程的PID。 My problem here is that i want every process to be prepared to receive the signal, but in the case the process doesn't recieve it (for example the process A is waiting and ready to receive the signal from Z but Z sends it to B) i don't know how to tell that process to go on and not get stuck in pause forever. 我的问题是我希望每个进程都准备好接收信号,但是在这种情况下进程无法接收到该信号(例如,进程A正在等待并准备从Z接收信号,但是Z将其发送给B )我不知道如何告诉该过程继续进行,而不会永远陷入暂停状态。 That's basically it, sorry if i commited any grammar or explanation errors but i'm from spain by the way. 基本上就是这样,对不起,如果我犯了语法或解释错误,但是我来自西班牙。 Thanks to anybody willing to answer! 感谢任何愿意回答的人!

From the man page of pause(): 在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. pause()使调用进程(或线程 )进入休眠状态,直到传递了终止进程或导致捕获信号的函数的信号为止。

So you need another thread that delays with sleep() or similar and sends the signal anyway after a while. 因此,您需要另一个因sleep()或类似原因而延迟并在一段时间后仍然发送信号的线程 Or use something else than pause, like sleep. 或者使用除暂停以外的其他方法,例如睡眠。

To wait for a signal or a timeout, use select , pselect , poll or ppoll . 要等待信号或超时,请使用selectpselectpollppoll

The p versions are used for custom signal masks, which might be something you need, but in this case probably not. p版本用于自定义信号掩码,这可能是您需要的,但在这种情况下可能不需要。

These functions are designed to take sets of file descriptors, but they work perfectly well without any file descriptors at all. 这些函数旨在采用文件描述符集,但它们完全不需要任何文件描述符就可以很好地工作。

it's been a while. 有一阵子了。 Sorry for taking so long to answer and thanks again to everyone who answered, your advice was quite useful. 很抱歉花了这么长时间回答,并再次感谢所有回答的人,您的建议非常有用。 So, i finally found a way to solve my problem. 因此,我终于找到了解决问题的方法。 After trying with pause, sleep and a lot of stuff without really getting where i wanted i realised i could use the signal 'alarm'. 在尝试暂停,睡眠和很多东西之后,并没有真正到达我想要的位置,我意识到我可以使用信号“警报”。 I used the pause() command to make the processes wait for a signal, but this time i set a SIGALARM signal in the process Z which is supossed to throw a signal to the others. 我使用了pause()命令使进程等待信号,但是这次我在进程Z中设置了SIGALARM信号,该信号可能会向其他进程抛出信号。 So, in any case when Z has thrown the signal and it's about to die, it throws an alarm which awakes every process from their pause state. 因此,无论如何,当Z发出信号并且将要死亡时,它都会发出警报,将每个进程从其暂停状态唤醒。 This solved my problem, so i hope this post helps anyone else with this problem. 这解决了我的问题,所以我希望这篇文章对其他人有帮助。 Thank you all again guys! 再次感谢大家!

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

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