如何在特定进程周围发送信号? [英] How to send a signal around specific processes?

查看:64
本文介绍了如何在特定进程周围发送信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一定时间内围绕进程圈发送信号.我的第一个参数代表我希望创建的进程数.我的第二个只是一个占位符,我目前正在初始化为 0.我的第三个是我想要传递这个信号的次数.我已经将流程设计为具有如下关系:Parent->child1,child1->child2,child2->child3……等等.我只是想弄清楚 C,我对为什么我的代码在中途停止感到困惑.它运行一两次迭代,然后停止.有人能解释一下原因吗?

I'm trying to send a signal around circle of processes for a certain amount of times. my first argument represents the number of processes I wish to create. my second one is just a place holder I am currently initiating to be 0. My third is the number of time I want to pass this signal around. I have designed the processes to have a relationship as such: Parent->child1, child1->child2, child2->child3.... and so on. I'm just figuring out C and I'm confused to why my code is stopping midway. It runs for an iteration or two and then stalls out. Can someone explain why?

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int startProcess;
int N;
int numOfCycles;
sigset_t killSet;



void myHandler1 () {
  if(N >= 2 && numOfCycles > 0) {
    printf("N=%d, numOfCycles=%d, Signal caught. PID = %d\n",N,numOfCycles,getpid());
    numOfCycles--;
    kill((getpid()+1),SIGUSR1);
  }
  else if(N >= 2 && numOfCycles == 0) {
    exit(1);
  }
  else if(N == 1 && numOfCycles > 0) {
    printf("N=%d, numOfCycles=%d, Signal caught. PID = %d\n",N,numOfCycles,getpid());
    numOfCycles--;
    kill(startProcess,SIGUSR1);
  }
  else if(N == 1 && numOfCycles == 0) {
    exit(1);
  }
  else {
    printf("Cycle Complete\n");
    exit(1);
  }
}

void main(int arg, char ** argv) {

  struct sigaction temp, vitas, arctic;
  sigemptyset(&killSet);
  sigaddset(&killSet,SIGUSR1);
  N = atoi(argv[1]);
  pid_t process1;
  startProcess = atoi(argv[2]);
  numOfCycles = atoi(argv[3]);

  temp.sa_handler = myHandler1;
  temp.sa_flags = SA_RESTART;

  // vitas.sa_handler = myHandler2;
  // vitas.sa_flags = SA_NODEFER;
  //
  // arctic.sa_handler = myHandler3;
  // arctic.sa_flags = SA_RESTART;

  sigaction(SIGUSR1, &temp, NULL);

  if (N > 1 ) {

    process1 = fork();
    if(process1 == 0) {
      if(N > 2) {
        printf("I am a child with PID=%d, PPID=%d, N =%d\n",getpid(),getppid(),N);
      }
      N--;
      char narg = N+'0';
      char *pnarg = &narg;

      if(startProcess == 0) {
        char nstartProcess[6];
        startProcess=getppid();
        sprintf(nstartProcess,"%d",startProcess);
        char *pstartProcess = &nstartProcess[0];
        execl("circle",argv[0],pnarg,pstartProcess,argv[3],NULL);
      }

      else{
        if(N == 1){
          printf("I am the final child with PID=%d, PPID=%d, N =%d, startProcess=%d\n",getpid(),getppid(),N,startProcess);
          printf("\nSignal Passing start\n\n");
          kill(startProcess, SIGUSR1);

          while(1) {
             sigsuspend(&killSet);
          }

        }
        else {
          execl("circle",argv[0],pnarg,argv[2],argv[3],NULL);
        }
      }

    }

    else {
      printf("I am a parent with PID=%d, PPID=%d, N =%d, startProcess=%d\n",getpid(),getppid(),N,startProcess);
      wait(NULL);
      while(1) {
         sigsuspend(&killSet);
      }

    }
  }
}

推荐答案

停顿了.有人能解释一下原因吗?

stalls out. Can someone explain why?

final child 没有从 sigsuspend(&killSet) 唤醒,因为你做了 sigaddset(&killSet,SIGUSR1);- 您似乎认为必须将要等待的信号添加到集合中,但相反,给定集合中的信号被阻止传递.所以只需删除 sigaddset 调用.

The final child is not woken up from sigsuspend(&killSet) because you did sigaddset(&killSet,SIGUSR1); - you seem to have thought you have to add the signal to be waited for to the set, but on the contrary the signals in the given set are blocked from delivery. So just drop the sigaddset call.

这篇关于如何在特定进程周围发送信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆