Linux C Programming learning-signal processing and linux programming Signal Processing

Source: Internet
Author: User

Linux C Programming learning-signal processing and linux programming Signal Processing

Signal processing is a special feature of linux programs. Use signal processing to simulate the interrupt function of the operating system. To use the signal processing function, you must enter a signal processing function.

 1 #include <stdio.h>    2 #include <sys/types.h>    3 #include <stdlib.h>     4 #include <signal.h>     5    6 int flag = 1;     7    8 void func(int sig)       9 {   10     printf("I get a signal!\n");   11     flag = 0;     12 }    13   14 int main()     15 {    16     signal(SIGINT, func);   17     printf("pid:%ld\n",(long)getpid());   18   19     while(flag)  20     pause();   21   22     return 0;    23 }    

Run:

# Gcc sig. c-o sig #./sig on another terminal: # kill-INT 333 // 333 is the process number printed by the program

To process a signal, you need to provide the processing function called by the system when the signal occurs. You can register corresponding processing functions for a specific signal (except for SIGKILL and SIGSTOP signals. After registering a signal processing function, when the process receives the signal, regardless of the status of the process, it will stop the current task to execute the processing function of this signal.

1. register the signal function

#include<signal.h>    void(*signal(int signumber,void ((*func)(int))(int)  

Signumber indicates the signal corresponding to the signal processing function. Func is a function pointer. This function has an integer parameter and returns the void type. In fact, func can also take other values, such as SIG_IGN and SIG_DFL. SIG_IGN indicates that the signal specified by signumber is ignored. SIG_DFL indicates that the system's default processing function is called. The Return Value Type of the signal function is the same as the func parameter. It is a function pointer pointing to a return value that is null and has an integer parameter. The correct return value should be the processing function of the previous signal. The error returns SIG_ERR.

Signal example:

 1 #include <stdio.h>   2 #include <sys/types.h>    3 #include <stdlib.h>    4 #include <signal.h>    5  6 void func(int sig)    7 {   8     printf("I get asignal!\n");   9 }   10 11 int main()   12 {    13     charbuffer[100];   14   15    if(signal(SIGINT, func) == SIG_ERR)  16      {  17          printf("signalerror exit now\n");  18          exit(0);  19      }  20      printf("pid:%ld\n",(long)getpid());  21   22    for(;;)  23      {  24          fgets(buffer,sizeof(buffer),stdin);  25          printf("bufferis:%s\n",buffer);  26      }  27      return 0;   28 }   

Generally, a user process needs to process multiple signals. You can register multiple signal processing functions in a program. One signal can correspond to one processing function, and multiple signals can correspond to one processing function. For SIGINT signals, we can use ctrl + c or ctrl + z to interrupt the process and execute the SIGINT registration function.

2. Advanced Signal Processing

In linux, a more powerful system call is provided.

#include <signal.h>    int sigaction(int signumbet,const structsigaction *act,struct sigaction *oldact)

In addition to registering the signal function, this function provides more detailed information about the signal received by the process. Struct sigaction is defined as follows:

struct sigaction    {         void(*sa_handler)(int);         void(*sa_sigaction)(int,siginfo_t *,void *);         sigset_tsa_mask;         intsa_flags;  }  

The values of sa_flags are as follows. If the value is 0, all default options are used.

SA_NOCLDSTOP: used to indicate the signal SIGCHLD. When a sub-process is interrupted, this signal is not generated and is generated only when the sub-process ends.

SA_NOCLDWATI: when the signal is SIGCHLD, the sub-process can be avoided.

SA_NODEFER: when the signal processing function is in progress, the signal functions of the signal processing function are not blocked.

SA_NOMASK: Same as SA_NODEFER

SA_ONESHOT: After a user-registered signal processing function is executed once, the signal processing function is set as the default processing function of the system.

SA_RESETHAND: Same as SA_ONESHOT

SA_RESTART: A system call that cannot be re-run automatically re-run.

SA_SIGINFO: indicates that the signal processing function is specified by SA_SIGACTION, instead of SA_HANDLER. It displays more information about the signal processing function.

 

In fact, sinaction can completely replace the signal function:

 1 #include <stdio.h>    2 #include <sys/types.h>    3 #include <stdlib.h>     4 #include <signal.h>     5      6 void func(int sig)      7 {     8     printf("I get a signal!\n");     9 }    10   11 int main()    12 {   13     char buffer[100];    14   15     struct sigaction act;  16     act.sa_handler=func;  17     sigemptyset(&act.sa_mask);  18     act.sa_flags = 0;  19   20     if(sigaction(SIGINT,&act, NULL) == -1)  21     {  22         printf("sigaction error exit now\n");  23         exit(0);  24     }  25   26     printf("pid:%ld\n",(long)getpid());   27   28     for(;;)  29     {  30         fgets(buffer,sizeof(buffer),stdin);  31         printf("buffer is:%s\n",buffer);  32     }  33   34     return 0;    35 }

 

Related Article

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.