• 双进程struct sigaction中mask阻塞信号


    #include "public.h"

    void myaction(int sig,siginfo_t *info,void *reverse)
    {
     int i=0;
     while (i<5)
     {
      /*printf("i = %d  sig=%d\n",i,sig);
      printf("recive value=%d\n",info->si_value.sival_int);
      printf("info->si_int=%d\n",info->si_int);
      printf("info->si_pid=%d\n",info->si_pid);
      */
      printf("myaction %d\n",i);
      sleep(1);
      i++;
     }
    }
    void myaction1(int sig,siginfo_t *info,void *reverse)
    {
     for (int i=0;i<5;i++)
     {
      printf("myaction1 = %d\n",i);
      sleep(1);
     }
    }

    void myaction2(int sig,siginfo_t *info,void *reverse)
    {
     for (int i=0;i<5;i++)
     {
      printf("myaction2 = %d\n",i);
      sleep(1);
     }
    }


    int main()
    {
     int num=0;
     pid_t pid;
     
     //在这里写信号
     union sigval sival_data;
     struct sigaction action;
     action.sa_sigaction=myaction;
     action.sa_flags=SA_SIGINFO|SA_RESTART;
     sigemptyset(&action.sa_mask);
     sigaddset(&action.sa_mask,SIGUSR2);
     sigaddset(&action.sa_mask,SIGINT);
     sigaction(SIGUSR1,&action,NULL);
     
     action.sa_sigaction=myaction1;
     action.sa_flags=SA_SIGINFO|SA_RESTART;
     sigaction(SIGUSR2,&action,NULL);

     action.sa_sigaction=myaction2;
     action.sa_flags=SA_SIGINFO|SA_RESTART;
     sigaction(SIGINT,&action,NULL);
     sigqueue(getpid(),SIGUSR1,sival_data);
     pid=fork();
     if (pid==-1)
     {
      perror("fork fail\n");
      exit(1);
     }
     if (pid>0)
     {
      //父进程负责创建子进程,新建的进程会从上一个逻辑层开始
      if (num<2)
      {
       pid=fork();
       num++;
      }
     }
     else if (pid ==0)
     {
      
      if (num==0)
      {
       sival_data.sival_int=num;
       sigqueue(getpid(),SIGUSR2,sival_data);
      }
      if (num==1)
      {
       sival_data.sival_int=num;
       sigqueue(getpid(),SIGINT,sival_data);
      }
     }

     return 0;
    }

  • 相关阅读:
    3秒后按按钮
    成为编程高手的八大奥秘
    实现页面的简繁体的切换
    推荐优秀的css的编辑器
    Javascript 常用方法示例
    CSS 滤镜属性(图片透明)
    javaScript 验证
    JavaScript中substr和substring的区别
    setInterval和clearInterval 讲解
    TBody 的解释及用法
  • 原文地址:https://www.cnblogs.com/newlist/p/2255594.html
Copyright © 2020-2023  润新知