Unix programming learning notes (25) -- sigaction function for advanced learning of Signal Processing

Source: Internet
Author: User

Lien000034
2014-11-05

Similar to the signal function, the sigaction function is used to set the signal processing function. This function is used to replace signal functions used in earlier versions of UNIX. In earlier versions of UNIX, after the signal function captures a signal, it automatically restores the processing of the signal to the default processing action. Therefore, if we want the registered signal processing function to take effect for a long time, we need to call the signal function again in the signal processing program for registration. This operation is too troublesome, and the signal may be generated before the signal processing function is called again in the signal processing function, and this signal will trigger the default processing action.

In addition, the signal function has another limit: the current processing method of the signal cannot be determined without modifying the signal processing method. This restriction does not exist in the sigaction function.

The Declaration of the sigaction function is as follows,

# Include <signal. h>

Int sigaction (INT signo, const struct sigaction * restrict act, struct sigaction * restrict oact );

Returned value: if the request is successful, 0 is returned. If the request is incorrect,-1 is returned.

The signo parameter indicates the Signal Number of the processing action to be detected or modified. If the act pointer is not empty, modify the processing action of the signal. If the oact pointer is not null, The oact pointer returns the previous action of the signal.
The Declaration of the struct sigaction used in parameters of the sigaction function is as follows,

struct sigaction {    void (*sa_handler)(int);    sigset_t sa_mask;    int sa_flags;    void (*sa_sigaction)(int, siginfo_t *, void *);};

The fields of the struct are described as follows:

  • Sa_handler: pointer of the signal processing function.
  • Sa_mask: the signal set to be blocked by the process during the process of calling the processing function of the signal.
  • Sa_flags: specifies various options for signal processing. For specific options, refer to the sigaction function manual.
  • Sa_sigaction: An Alternative signal processing function. If sa_flags sets sa_siginfo, this function is called; otherwise, sa_handler is called.

Let's look at an example,

#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <signal.h>voidprint_mask(const char *str){  sigset_t sigset;  int errno_save;  errno_save = errno;  if (sigprocmask(0, NULL, &sigset) < 0) {    printf("sigprocmask error: %s\n", strerror(errno));    exit(-1);  }  printf("%s", str);  if (sigismember(&sigset, SIGALRM)) {    printf("SIGALRM ");  }  if (sigismember(&sigset, SIGINT)) {    printf("SIGINT ");  }  printf("\n");  errno = errno_save;}static voidsig_alrm(int signo){  printf("received SIGALRM\n");  print_mask("in sig_alrm: ");}intmain(void){  struct sigaction sact;  sact.sa_handler = sig_alrm;  sact.sa_flags = 0;  sigemptyset(&sact.sa_mask);  sigaddset(&sact.sa_mask, SIGINT);  if (sigaction(SIGALRM, &sact, NULL) < 0) {    printf("sigaction error: %s\n", strerror(errno));    exit(-1);  }  print_mask("in main before alarm: ");  alarm(3);  pause();  print_mask("in main after alarm: ");  exit(0);}

In the above sigactiondemo. in the C program, we call the sigaction function to set the processing function of the signal sigalrm, and add the signal sigint to the sa_mask field of the Act parameter of the sigaction function, indicates the process blocking signal SIGINT during the process of calling the processing function of the signal sigalrm. Compile the program and generate and execute the executable file sigactiondemo,

lienhua34:demo$ gcc -o sigactiondemo sigactiondemo.clienhua34:demo$ ./sigactiondemoin main before alarm:received SIGALRMin sig_alrm: SIGALRM SIGINTin main after alarm:

From the above running results, we can see that before the sigalrm signal is generated, the process does not shield the signal SIGINT and sigalrm, but in the processing function of the signal sigalrm, the signal shielding characters of the process include SIGINT and sigalrm. After the signal processing function of sigalrm is called, the signal shielding characters of the process are restored to the called Signal Processing shielding characters. (Note: During the call of the signal processing function registered by the sigaction function, the system automatically adds the signal to the signal shielding word unless sa_flags of the Act parameter of the sigaction function is set to sa_nodefer .)

(Done)

Unix programming learning notes (25) -- sigaction function for advanced learning of Signal Processing

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.