C-language based signal programming instance _c language under Linux

Source: Internet
Author: User
Tags posix

This article illustrates the C-language based signal programming method under Linux. Share to everyone for your reference. 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=%d\n", sig_no); if (sig_no = = sigquit) {printf ("Haha, do you want to quit?")
  ");
    }else{printf ("si_signo=%d\n", Info->si_signo);
    printf ("Si_code =%d\n", Info->si_code);
    printf ("Si_pid =%d\n", info->si_pid);
    printf ("Si_uid =%d\n", info->si_uid);
    printf ("si_status=%d\n", info->si_status);
    printf ("Si_utime =%lld\n", info->si_utime);
    printf ("Si_stime =%lld\n", info->si_stime);
    printf ("Si_value =%d\n", info->si_value);
    printf ("Si_addr =0x%x\n", info->si_addr);
  printf ("Si_fd =%d\n", INFO-&GT;SI_FD);
} return; /*--------------------Common Signal list----------------------------/*//sigint CTRL + C//sigquit ctrl+\//sigpipe pipe rupture//sigkill Process terminated, cannot be captured//sighup shell exits//sigchld terminating signal//SIGFPE floating-point number exception (divided by 0)//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 callback//sa.sa_flags |= Sa_resethand; The handler function is only invoked once, and then the//sa.sa_flags |= sa_nocldstop is reset;  If the SIGCLD is installed, the subprocess does not exit normally, but is killed, the//sa.sa_flags |= Sa_nodefer is not notified;  The shielding of the signal is invalid, that is, the signal processing function can still be emitted during the execution of this signal//sa.sa_flags |= Sa_restart; //sa.sa_flags |= sa_nocldwait is automatically restarted by a system call that is interrupted by a signal;
  Causes the parent process to not receive a SIGCHLD signal when its child process exits, and the subprocess does not become a zombie//install signal if (sigaction (sigint,&sa,null) ==-1) printf ("Failed to install signal");
  if (Sigaction (sigquit,&sa,null) ==-1) printf ("Failed to install signal \ n");
  while (1) {sleep (1);
return 0; }/*--------------------------signal programming-related structure----------------------------------//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/* Signal number *//int si_errno; * An errno value *//int si_code;  /* Signal code *//pid_t si_pid;  /* Sending Process ID *//uid_t si_uid; /* Real user ID of sending process *//int si_status;  /* Exit value or signal *///clock_t si_utime; User time consumed//clock_t si_stime; /* System Time consumed *//sigval_t si_value;  /* Signal value *//int si_int;  /* POSIX.1B signal *//void * SI_PTR;  /* POSIX.1B signal *//void * SI_ADDR;  /* Memory location which caused fault *//int si_band;   /* Band Event *//int si_fd;       -C occurs//Sigquit 3 core Keyboard input Exit command//Sigill 4 core error directive//SIGABRT 6 Core abort (3) signaled abort//SIGFPE 8 Core floating point anomaly//SIGKILL 9 Term Kill signal//SIGSEGV core illegal memory access//sigpipe Term Pipe disconnect//SIGALRM Abort signal//sigterm Term forced stop signal issued by Term Alarm (2)/SIGUSR1 30,10,16 Term user-defined signal 1//SIGUSR2 31,12,17 Term user-defined signal 2//SIGCHLD 20,17,18 IGN Sub Process abort signal//Sigcont 19,   18,25 Cont continues to execute a stopped process//SIGSTOP 17,19,23 stop signal from terminal stops//SIGTSTP stop signal from end 18,20,24//Sigttin 21,21,26

 Stop background process read Terminal//Sigttou 22,22,27 Stop background process write terminal

I hope this article will help you with your C language program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.