sig关不掉那种

int kill (pid_t pid, int sig)

if pid > 0 send sig to pid

if pid == 0 会发送信号到进程同组的每个进程,包括自身

if pid < -1 向组ID等于该pid 绝对值的组内所有进程发送信号

if pid == -1 除了init pid=1 和自身, 发给所有人

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>

/*
我就想试试 SIGINT 以后再重新 cont,这样的话,进程用 ctrl-C 就停不掉了,必须使用其他方法,

SIGQUIT ---> ctrl + \ 目前之前这样退出
SIGTSTP -->ctrl + z SIGTSTP 挂起,从ps的结果可以看到
SIGINT --> type-c 
SIGCONT

*/

void handler(int sig)
{
   if(sig == SIGINT)
   {
      printf("type -C pessed,so I will send SIGCONT to me\n") ;
      kill (0, SIGCONT);  //发给该进程组里的所有进程,包括自身
   }
   if(sig == SIGCONT)
   {
     printf("SIGCONT received\n");
   }
   else
   {
     printf("sig %d", sig);
   }
   

}

int main(void)
{
  struct sigaction sa;
  sigemptyset(&sa.sa_mask); //注意用法
  sa.sa_flags = 0;
  sa.sa_handler  = handler;
  if (sigaction(SIGINT, &sa, NULL) < 0) //添加想测试的函数
           printf("sigactionfail");  
  if (sigaction(SIGCONT, &sa, NULL) < 0)
           printf("sigactionfail"); 
  if (sigaction(SIGHUP, &sa, NULL) < 0)
          printf("sigactionfail"); 
  if (sigaction(SIGSTOP, &sa, NULL) < 0)
          printf("sigactionfail");       
  while(1)
    pause();
}

猜你喜欢

转载自blog.csdn.net/qq_24328911/article/details/85918021