Linux signal mechanism

Source: Internet
Author: User
Signal Classification & #160; unreliable signal & #160; VS. & #160; reliable signal & #160; Linux signal mechanism is basically inherited from UNIX systems. In early UNIX systems, the signal mechanism was relatively simple and original, and some problems were exposed in practice. the main problems were: & #160; & #160; & #160; 1. each time a process processes a signal, it sets the response to the signal as the default action. Unreliable signal in signal classification VS. reliable signal

The Linux signal mechanism is basically inherited from the UNIX system. In early UNIX systems, the signal mechanism was relatively simple and original, and some problems were exposed in practice. the main problems were as follows:

1. each time a process processes a signal, it sets the response to the signal as the default action. In some cases, it may lead to incorrect signal processing. Therefore, if you do not want such an operation, you need to call signal () again at the end of the signal processing function (), reinstall the signal.

2. the unreliable signal in early UNIX mainly refers to the possibility that the process may make a wrong response to the signal and the possible loss of the signal.

 

Linux supports unreliable signals, but makes improvements to unreliable signal machines: After the signal processing function is called, you do not have to call the signal installation function again (the signal installation function is implemented in a reliable mechanism ). Therefore, the unreliable signals in Linux mainly refer to the possible loss of signals.

 

Reliable signal

With the development of time, practice has proved that it is necessary to improve and expand the original signal mechanism. Therefore, various later UNIX versions were studied in this respect to achieve "reliable signals ". Because the originally defined signals have been used in many applications and cannot be modified, we have to add some new signals and define them as reliable signals at the beginning. these signals support queuing, no loss. At the same time, a new version of Signal sending and installation has also emerged: the signal sending function sigqueue () and the signal installation function sigaction ().

Both the sigaction and signal functions call the do_signal function of the kernel service. [kernel service functions, functions that cannot be called by the application system]

 

Real-Time Signal (reliable signal) and non-real-time signal (unreliable signal)

Early UNIX systems defined only 32 types of signals, Linux 3. x supports 64 types of signals, numbered 0-63 (SIGRTMIN = 31, SIGRTMAX = 63), which may be further increased in the future. This requires kernel support. The first 32 signals have predefined values. each signal has a definite purpose and meaning, and each signal has its own default action. If you press CTRL + C on the keyboard, the SIGINT signal is generated. the default reaction to the signal is that the process is terminated. The last 32 signals represent real-time signals, which are equivalent to reliable signals. This ensures that multiple real-time signals are received. Real-time signals are part of the POSIX standard and can be used in application processes.

Non-real-time signals do not support queuing and are all unreliable signals. real-time signals support queuing and are all reliable signals.

 

Signal sending kill

int kill(pid_t pid, int signo); 

Kill can send signals to itself or other processes

 

Description of signo parameter combination:

Pid> 0 send signal sig to pid process

Pid = 0 send the signal sig to the same group of processes

Pid =-1 sends the signal sig to all processes, and the caller process has the permission to send each process (except Process 1)

Pid <-1 sends the signal sig to every process in which the process Group is pid (absolute value ).

 

Lab:

Void onSignalAction (int signalNumber) {switch (signalNumber) {case SIGUSR1: cout <"SIGUSR1 =" <signalNumber <endl; break; default: cout <"Other Signal... "<endl; break ;}int main () {if (SIGUSR1, onSignalAction) == SIG_ERR) {perror (" signal error "); return-1 ;} pid_t pid = fork (); if (pid =-1) {perror ("fork error"); return-1;} else if (pid = 0) {/** send pid_t ppid = getppid (); kill (ppid, SIGUSR1) to all processes in the same group, the sub-process will also receive the kill signal (0, SIGUSR1); * // send the signal to all the processes in this group, which serves the same purpose as above // getpgrp () function to obtain the process Group pid pid_t pgid = getpgrp (); killpg (pgid, SIGUSR1); exit (0) ;}int sleepTime = 3; while (sleepTime> 0) {write (STDOUT_FILENO, "Parent start Sleep... \ n ", sizeof (" Parent start Sleep... \ n "); sleepTime = sleep (sleepTime); write (STDOUT_FILENO," Parent return from Sleep... \ n ", sizeof (" Parent return from Sleep... \ n ");} return 0 ;}

Note: If the signal is installed before fork, the sub-process can inherit the signal.

 

When Sleep encounters signal, the child process sends a signal to the parent process. description of the sleep function

1) sleep function to sleep the process.

2) after the signal can be interrupted and then the signal function is processed, the system will no longer sleep. Execute code directly down

3) The return value of the sleep function is the remaining number of seconds.

Man Manual display:

RETURN VALUE

Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal handler.

// Example: sleep encounters signalvoid onSignalAction (int signalNumber) {switch (signalNumber) {case SIGINT: cout <"SIGINT =" <signalNumber <endl; break; default: cout <"Other Signal... "<endl; break ;}int main () {if (SIGINT, onSignalAction) == SIG_ERR) {perror (" signal error "); return-1 ;} cout <"Main Start Sleeping... "<endl; int returnValue = sleep (100); // cout can interrupt sleep <" Main End Sleeping... returnValue = "<returnValue <endl; return 0 ;}

// Example: sleep enhanced int main (){//... same as cout <"Main Start Sleeping... "<endl; // enhanced sleep version ^ int sleepTime = 20; do {sleepTime = sleep (sleepTime); cout <" continue... "<endl ;}while (sleepTime> 0); cout <" Main End Sleeping... sleepTime = "<sleepTime <endl; return 0 ;}

Raise

int raise(int sig);

Send a signal to yourself. Raise (sig) is equivalent to kill (getpid (), sig );

 

Killpg

int killpg(int pgrp, int sig);
Sends a signal to the process Group. Killpg (pgrp, sig) is equivalent to kill (-pgrp, sig );

 

Sigqueue

int sigqueue(pid_t pid, int sig, const union sigval value);
Sends signals to processes. it supports queuing and can contain additional information.

 

Pause

int pause(void);
Set the process to an interrupted sleep state. Then it calls the kernel function schedule () so that the Linux process scheduler can find another process to run.

Pause suspends the caller process until a signal is captured.

 

Alarm

unsigned int alarm(unsigned int seconds);
The alarm function is used to set an alarm time to send the SIGALRM signal (telling the Linux kernel to send the SIGALRM signal after n seconds );

 

Manual description:

DESCRIPTION

Alarm () arranges for a SIGALRM signal to be delivered to the process in seconds.

If seconds is zero, no new alarm () is scheduled.

In any event any previusly set alarm () is canceled.

// Alarm recursively calls void onSignalAction (int signalNumber) {switch (signalNumber) {case SIGALRM: cout <"SIGALRM =" <signalNumber <endl; alarm (1 ); // continue to call onSignalAction break; default: cout <"Other Signal... "<endl; break ;}int main () {if (SIGALRM, onSignalAction) == SIG_ERR) {perror (" signal error "); return-1 ;} alarm (1); while (true) {pause (); cout <"pause returned... "<endl;} return 0 ;}

Reentrant/non-Reentrant functions

Concept of Reentrant functions

To enhance the stability of the program, the reentrant function should be used in the signal processing function.

A reentrant function is a process that can be called by multiple tasks. when a task is called, you do not have to worry about data errors. After receiving the signal, the process jumps to the signal processing function and runs it. If the non-reentrant function is used in the signal processing function, the signal processing function may modify the data that should not be modified in the original process, in this way, when the process returns and then executes from the signal processing function, unexpected consequences may occur. Non-Reentrant functions are considered unsafe functions in signal processing functions.

Most functions that meet the following conditions cannot be reentrant:

(1) use static data structures, such as getlogin (), gmtime (), getgrgid (), getgrnam (), getpwuid (), and getpwnam;

(2) the malloc () or free () function is called During function implementation;

(3) standard I/O functions are used for implementation.


// Experiment // The X64 system may run normally, but there may be output errors in 32-bit systems. void onSignalAction (int signalNumber) {switch (signalNumber) {case SIGALRM: printGlobalTeacher (); alarm (1); break; default: cout <"Other Signal... "<endl; break ;}int main () {if (SIGALRM, onSignalAction) == SIG_ERR) {perror (" signal error "); return-1 ;} teacher t1, t2; t1.age = 11; t1.number = 11; t2.age = 22; t2.number = 22; alarm (1); while (true) {g_t = t1; g_t = t2 ;} return 0 ;}

Appendix-man manual usage


Man 7 signal search for reentrant and non-Reentrant functions


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.