Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux signal capture mechanism (signal,kill), (sig)

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

There are 64 public signals under linux. Check kill-l:

It can be seen that 32 and 33 unknown signals are missing. Dividing from here, the first 31 signals are unreliable signals, followed by reliable signals. When a process is blocked (sending a lot of signals at once), unreliable signals are easy to lose. How to verify it? You can constantly send these two signals to a process during the shielding of the 2 (unreliable signal) signal and 34 (reliable process) signal, and observe whether they are lost after the shielding is unblocked. When testing here, it should be noted that the four signals of 9-SIGKILL 19-SIGSTOP 31 32 cannot be captured, and can be found by traversing all of the following signals.

Signal set

First of all, let's talk about the signal set, as the name implies, the set of signals that are stored.

Signal set operation functions have the following, specific use, we will talk about later.

Signal

The first is to use the signal,kill function.

# include

< signal.h>

Typedef void (* sighandler_t) (int); sighandler_t signal (int signum, sighandler_t handler)

Signal () takes two parameters: the signal number and the handler function (sighandler_t is a function pointer), and the return value is also of type sighandler_t, which returns the previous signal handler.

The signal processing function is a function with an int parameter and a return value of void. Handler can also be two special values: SIG_IGN shields the signal and SIG_DFL returns to the default behavior

# include

< sys/types.h>

# include

< signal.h>

Int kill (pid_t pid, int sig)

The function of kill () is to send the signal sig to the process pid.

# include int sigprocmask (int how, const sigset_t * set, sigset_t * oldset)

Function: read the signal mask word of the change process.

Return value: 0 for success and-1 for failure

Parameter: if oset is a non-null pointer, the current signal mask word of the reading process is transmitted through the oset parameter (that is, the original signal mask word is backed up to oset); set is the signal mask word for changing the process, and how indicates how to modify it. The available values for how are as follows:

The code is as follows:

/ / 3. Loss of unreliable signal void handler (int no) {printf ("received a signal:% d\ n", no);} int main () {pid_t pid; sigset_t set; sigset_t oset; int i; sigemptyset (& set); / / clear sigaddset (& set,2); / / add signal No. 2 sigaddset (& set,34) / / add signal 34 for (I = 1; i int sigqueue (pid_t pid, int sig, const union sigval value)

Sigqueue () is similar to the previous kill (). Is used to send signals, mainly for signals with parameters, used in conjunction with sigaction ().

The third parameter is a federated data structure, union sigval, which specifies the parameter for signaling, commonly known as a 4-byte value.

The specific code is as follows:

Void handler (int signo,siginfo_t * resdata,void * unknowp) {printf ("signo=%d\ n", signo); printf ("return data:% d\ n", resdata- > si_value.sival_int);} int main () {int I = 5; pid_t pid = 0; pid = fork (); if (pid = =-1) {perror ("create fork"); return-1 } else if (pid = = 0) {sleep (1); / / send the signal with integer data to the parent process union sigval sigvalue; sigvalue.sival_int = 111; / / send the signal while (iMel -) {sigqueue (getppid (), 2 send signal:2 success sigvalue); printf ("send signal:2 success!\ n") Sigqueue (getppid (), 34 printf sigvalue); printf ("send signal:34 success!\ n");} else {struct sigaction act; / / initialize sa_mask sigemptyset (& act.sa_mask); act.sa_sigaction=handler / / once the sa_sigaction attribute is used, the value of the sa_flags property must be set to the SA_SIGINFO act.sa_flags=SA_SIGINFO; / / registration signal sigaction (2); sigaction (34);} while (1) {}

Running result:

Similarly, it can be seen that signal No. 2 was received only once.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report