signal function and Sigaction structure understanding

Source: Internet
Author: User
Tags numeric value printf sigint signal signal handler sleep
signal function and Sigaction structure understanding

One, signal function
Detailed Description: http://blog.csdn.net/ta893115871/article/details/7475095

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

/*
Sig_ign Ignore/SIG_DFL default, both macros can also be used as signal processing functions. At the same time Sigstop/sigkill these two signals can not be captured and ignored. Note that the signal function also blocks the signal that is currently being processed, but there is no way to block other signal, such as processing Sig_int, and then a sig_int will clog, but Sig_quit will be interrupted if Sig_ Quit has to deal with, you need to wait sig_quit processing finished, Sig_int will continue to deal with just now.

*/
void Ouch (int sig)
{
printf ("Caught Sigint,i got signal%d\n", SIG);
(void) signal (SIGINT, SIG_DFL);
(void) signal (SIGINT, ouch);
int num = 5;
int i = 0;
while (i++ < num)
{
printf ("SIGINT processing...\n");
Sleep (1);
}
Return


}

void Quitsig (int sig)
{
printf ("Caught sigquit, I got signal%d\n", SIG);
(void) signal (SIGINT, SIG_DFL);
(void) signal (SIGINT, ouch);
int num = 5;
int i = 0;
while (i++ < num)
{
printf ("Sigquit processing...\n");
Sleep (1);
}
Return
}

int main ()
{
(void) signal (SIGINT, ouch);
(void) signal (sigquit, quitsig);
while (1)
{
printf ("Hello world...\n");
Sleep (1);
}
return 0;
}
/*
Results
Hello world ...
Hello world ...
^ccaught Sigint,i got signal 2
SIGINT processing ...
^csigint processing ...
SIGINT processing ...
SIGINT processing ...
SIGINT processing ...
Caught Sigint,i got signal 2
SIGINT processing ...
SIGINT processing ...
SIGINT processing ...
SIGINT processing ...
SIGINT processing ...
Hello world ...
Hello world ...
^ccaught Sigint,i got signal 2
SIGINT processing ...
SIGINT processing ...
^\caught sigquit, I got signal 3
Sigquit processing ...
Sigquit processing ...
Sigquit processing ...
Sigquit processing ...
Sigquit processing ...
SIGINT processing ...
SIGINT processing ...
SIGINT processing ...
Hello world ...
Hello world ...

*/


Two, the sigaction structural body

/*
From:sigaction Baidu Encyclopedia
Correlation function Signal,sigprocmask (), Sigpending,sigsuspend, Sigemptyset
Table header file #include <signal.h>
Defines the function int sigaction (int signum,const struct sigaction *act, struct sigaction *oldact);
The function Description Sigaction () Sets the processing function of the signal according to the signal number specified by the parameter signum. The parameter Signum can specify all signals except Sigkill and sigstop.
As the parameter structure sigaction is defined as follows
struct Sigaction {
void (*sa_handler) (int);
void (*sa_sigaction) (int, siginfo_t *, void *);
sigset_t Sa_mask;
int sa_flags;
void (*sa_restorer) (void);
}
Signal processing functions can take either void (*sa_handler) (int) or void (*sa_sigaction) (int, siginfo_t *, void *). In the end, which depends on whether the sa_siginfo bit is set in Sa_flags, if it is set to use void (*sa_sigaction) (int, siginfo_t *, void *), you can send additional information to the handler function. By default, void (*sa_handler) (int) is used, at which time only the numeric value of the signal can be sent to the processing function.
Sa_handler This parameter is the same as the signal () parameter handler, which represents the new signal processing function, other meanings refer to signal ().
The sa_mask is used to set aside the signal set specified by Sa_mask temporarily when processing the signal.
Sa_restorer This parameter is not used.
Sa_flags is used to set other related operations for signal processing, and the following values are available.
Sa_flags can also set other flags:
Sa_resethand: Resets the signal processing function to the default value when the signal processing function is called SIG_DFL
·· Sa_restart: If the signal interrupts a system call to the process, the system automatically starts the system call
Sa_nodefer: In general, when the signal processing function is running, the kernel will block the given signal. However, if the sa_nodefer tag is set, the kernel will not block the signal when the signal handler is running.

*/
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

void Ouch (int sig)
{
printf ("Caught SIGINT, oh, got a signal%d\n", SIG);


int i = 0;
for (i = 0; i < 5; i++)
{
printf ("Signal func%d\n", i);
Sleep (1);
}
}

void Quitsig (int sig)
{
printf ("Caught Sigquit (ctrl+\) got a signal%d\n", SIG);
Return
}

int main ()
{
struct Sigaction Act;
Act.sa_handler = Ouch;
Sigemptyset (&act.sa_mask);
The sa_mask is used to set aside the signal set specified by Sa_mask temporarily when processing the signal.
The sigquit signal is blocked during signal function processing
Sigaddset (&act.sa_mask, sigquit);//ctrl+\ sends Sigquit,
Act.sa_flags = Sa_resethand; Sa_resethand, when the signal processing function is called, resets the handler function of the signal to the default value.
Act.sa_flags = Sa_nodefer;
/*sa_nodefer, in general, when the signal processing function runs, the kernel will block sigaction incoming signal, here is SIGINT
Only after processing the signal processing function, the subsequent SIGINT will be processed, and subsequent to the number of SIGINT,
Working with only one sigint,sigaction will queue the subsequent SIGINT.


However, if the sa_nodefer tag is set, the kernel will not block the signal when the signal handler is running, for example, when the ouch handler is executed, press CTRL + C again, and
Send SIGINT signal, will suspend the execution of the current ouch, and to perform the signal processing function corresponding to the SIGINT signal
/*
^ccaught SIGINT, oh, got a signal 2 keys CTRL + C
Signal Func 0
^ccaught SIGINT, oh, got a signal 2 press CTRL + C again
Signal Func 0
Signal Func 1
Signal Func 2
Signal Func 3
Signal Func 4
Signal Func 1
Signal Func 2
Signal Func 3
Signal Func 4

*/
If the handler function that does not need to reset the given signal is the default, and the given signal needs to be blocked (that is, Sa_nodeffer and Sa_resethand are not set), the sa_flags must be zeroed
/*
^ccaught SIGINT, oh, got a signal 2
Signal Func 0
Signal Func 1
^csignal Func 2
Signal Func 3
Signal Func 4
Caught SIGINT, oh, got a signal 2
Signal Func 0
Signal Func 1
Signal Func 2
Signal Func 3
Signal Func 4
^ccaught SIGINT, oh, got a signal 2
Signal Func 0
Signal Func 1
Signal Func 2
Signal Func 3
Signal Func 4

*/

Sigaction (SIGINT, &act, 0); CTRL + C sends SIGINT
Signal (Sigquit, quitsig); Ctrl+\ sends Sigquit



while (1)
{
Sleep (1);
}
Return


}

Note:
From:http://www.cnblogs.com/liulipeng/p/3555450.html
The sig_dfl,sig_ign represents a function pointer with no return value, and the pointer values are 0 and 1, which are logically the function address values that are not possible in the actual program.
SIG_DFL: Default Signal Handler
Sig_ign: Ignoring signal handlers
#include <stdio.h>
#define SIG_DFL ((Void (*) (int)) 0)
#define SIG_IGN ((Void (*) (int)) 1)

int main () {
int a = (int) SIG_DFL;
int b = (int) sig_ign;

printf ("A =%d\n", a); 0
printf ("B =%d\n", b); 1


return 0;
}

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.