Examples of signal programming based on C language under Linux

  • 2020-04-02 03:13:04
  • OfStack

This paper illustrates the signal programming method based on C language under Linux. Share with you for your reference. The details are as follows:


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void sig_handler(int sig_no, siginfo_t *info, void *ctext){
  printf("receive sig_no=%dn",sig_no);
  if(sig_no == SIGQUIT){
    printf("haha, Want to quit? ");
  }else{
    printf("si_signo=%dn",info->si_signo);
    printf("si_code =%dn",info->si_code);
    printf("si_pid =%dn",info->si_pid);
    printf("si_uid =%dn",info->si_uid);
    printf("si_status=%dn",info->si_status);
    printf("si_utime =%lldn",info->si_utime);
    printf("si_stime =%lldn",info->si_stime);
    printf("si_value =%dn",info->si_value);
    printf("si_addr =0x%xn",info->si_addr);
    printf("si_fd  =%dn",info->si_fd);
  }
  return ;
}

//SIGINT ctrl+c 
//SIGQUIT ctrl+
//SIGPIPE pipe ruptured
//The SIGKILL process terminates and cannot be caught
//SIGHUP shell out
//SIGCHLD subprocess termination signal
//SIGFPE floating point exception (divided by 0, etc.)
//SIGTERM termination signal (kill pid)
int main(int argc ,char **argv){
  struct sigaction sa;
  sa.sa_flags  = 0;
  sa.sa_sigaction = sig_handler;
  sa.sa_flags  |= SA_SIGINFO;  //Use sa_sigaction as the callback
  //sa.sa_flags |= SA_RESETHAND; // The handler is called only once and then reset 
  //sa.sa_flags |= SA_NOCLDSTOP; // If installed SIGCLD, The child process does not exit normally, but is kill If you drop it, you won't be notified 
  //sa.sa_flags |= SA_NODEFER ;  // Invalidates the masking of the signal, i.e. the signal can still be emitted during the execution of the signal handler 
  //sa.sa_flags |= SA_RESTART ;  // Automatically restarts a system call that is interrupted by a signal 
  //sa.sa_flags |= SA_NOCLDWAIT; // Causes the parent process not to receive when its child processes exit  SIGCHLD  Signal that the child will not become a zombie if it exits 
  //Install the signal
  if(sigaction(SIGINT,&sa,NULL)==-1) printf("Install the signal failure n");
  if(sigaction(SIGQUIT,&sa,NULL)==-1) printf("Install the signal failure n");
  while(1){
    sleep(1);
  }
  return 0;
}

 // struct sigaction {
 //   void (*sa_handler)(int);
 //   void (*sa_sigaction)(int, siginfo_t *, void *);
 //   sigset_t sa_mask;
 //   int sa_flags;
 //   void (*sa_restorer)(void);
 // }
// siginfo_t {
//    int   si_signo; 
//    int   si_errno; 
//    int   si_code;  
//    pid_t  si_pid;  
//    uid_t  si_uid;  
//    int   si_status; 
//    clock_t si_utime;  //User time consumed 
//    clock_t si_stime; 
//    sigval_t si_value; 
//    int   si_int;  
//    void *  si_ptr;  
//    void *  si_addr;  
//    int   si_band;  
//    int   si_fd;   
// }
//Signal value action interpretation
//SIGHUP 1 terminal line hangs up
//SIGINT 2 Term keyboard input interrupt command, occurs when entering ctrl-c from the terminal
//SIGQUIT 3 Core keyboard entry exit command
//SIGILL 4 Core error instruction
//SIGABRT 6 Core abort(3)
//SIGFPE 8 Core floating point number exception
//SIGKILL 9 Term KILL signal
//SIGSEGV 11 Core illegal memory access
//SIGPIPE 13 Term pipe is disconnected
//SIGALRM 14 Term alarm(2) sends an abort signal
//SIGTERM 15 forces the termination of the signal
//SIGUSR1 30,10,16 Term user-defined signal 1
//SIGUSR2 31,12,17 Term user-defined signal 2
//SIGCHLD 20,17,18 Ign subprocess abort signal
//SIGCONT 19,18,25 Cont continues to execute a stopped process
//SIGSTOP 17,19,23 Stop the non-terminal Stop signal
//SIGTSTP 18,20,24 Stop terminal Stop signal
//SIGTTIN 21,21,26 Stop background process read terminal
//SIGTTOU 22,22,27 Stop background process write terminal

Hope that the article described in the C programming language for you to help.


Related articles: