Linux signal processing function

Source: Internet
Author: User
Tags define function

Reference: http://hi.baidu.com/luxey/blog/item/41c802085918a1980a7b8292.html

 # Include  <  Unistd. h  >  
# Include < Signal. h >
Void Handler ()
{
Printf ( " Hello \ n " );
}

Main ()
{
Int I;
Signal (sigalrm, Handler );
Alarm ( 5 );
For (I = 1 ; I < 7 ; I ++ )
{
Printf ( " Sleep % d... \ n " , I );
Sleep ( 1 );
}
}


1. Alarm)
Related functions: signal, sleep
Header file: # include <unistd. h>
Define the function: Unsigned int alarm (unsigned int seconds );
Function Description: Alarm () is used to set the signal sigalrm to be transmitted to the current process after the number of seconds specified by the seconds parameter. If the seconds parameter is 0, the previously set alarm is canceled and the remaining time is returned.
Return Value: returns the remaining seconds of the previous alarm. If no alarm is set, 0 is returned.

Example
# Include <unistd. h>
# Include <signal. h>
Void handler ()
{
Printf ("Hello \ n ");
}

Main ()
{
Int I;
Signal (sigalrm, Handler );
Alarm (5 );
For (I = 1; I <7; I ++)
{
Printf ("Sleep % d... \ n", I );
Sleep (1 );
}
}

Run
Sleep 1...
Sleep 2...
Sleep 3...
Sleep 4...
Sleep 5...
Hello
Sleep 6...


Kill (send a signal to a specified process)
Related functions: Raise, signal

Header file: # include <sys/types. h>
# Include <signal. h>

Define the function: int kill (pid_t PID, int sig );

Function Description: Kill () can be used to send signals specified by the SIG parameter to the process specified by the PID parameter. The PID parameter can be used in the following situations:
PID> 0: send the signal to the process with the pid id.
PID = 0 send the signal to all processes in the same process group as the current process
PID =-1 broadcasts signals to all processes in the system
PID <0 transmits the signal to all processes whose process ID is the absolute value of PID
For the signal numbers represented by the SIG parameter, see Appendix D.

Return Value: if the execution is successful, 0 is returned. If an error exists,-1 is returned.

ErrorCodeThe Sig Val parameter SIG is invalid.
The process or process group specified by the esrch Parameter PID does not exist.
The eperm permission is insufficient to send signals to the specified process.

Example
# Include <unistd. h>
# Include <signal. h>
# Include <sys/types. h>
# Include <sys/Wait. H>
Main ()
{
Pid_t PID;
Int status;
If (! (Pid = fork ()))
{
Printf ("Hi I am child process! \ N ");
Sleep (10 );
Return;
}
Else
{
Printf ("Send signal to child process (% d) \ n", pid );
Sleep (1 );
Kill (PID, SIGABRT );
Wait (& status );
If (wifsignaled (Status ))
Printf ("Chile process receive signal % d \ n", wtermsig (Status ));
}
}

Run
Sen signal to child process (3170)
Hi I am child process!
Child process receive signal 6


Pause (pause the process until the signal appears)
Related functions: Kill, signal, sleep
Header file: # include <unistd. h>
Define the function: int pause (void );
Function Description: Pause () will suspend the current process (into sleep state) until it is interrupted by the signal.
Return Value: only-1 is returned.
Error Code: The function is interrupted when the eintr signal reaches.


sigaction (query or set the signal processing method)
related functions: signal, sigprocmask, sigpending, sigsuspend
header file: # include
define function: In T sigaction (int signum, const struct sigaction * Act, struct sigaction * oldact);
Function Description: sigaction () the signal processing function is set based on the signal number specified by the SIGNUM parameter. The SIGNUM parameter can specify all signals other than sigkill and sigstop.

If the parameter structure sigaction is defined as follows
struct sigaction
{< BR style =" line-Height: normal; "> void (* sa_handler) (INT);
sigset_t sa_mask;
int sa_flags;
void (* sa_restorer) (void);
}

Sa_handler this parameter is the same as the signal () parameter handler, representing a new signal processing function. For other meanings, see signal ().
Sa_mask is used to set the signal specified by sa_mask to be temporarily shelved when processing this signal.
Sa_restorer is not used.
Sa_flags is used to set other operations related to signal processing. The following values are available.
Or operation (|) Combination
A_nocldstop: If the SIGNUM parameter is sigchld, the parent process is not notified when the child process is paused.
Sa_oneshot/sa_resethand: Before calling a new signal processing function, change the Signal Processing Method to the system preset method.
Sa_restart: system calls that are interrupted by signals will be restarted on their own.
Sa_nomask/sa_nodefer: Ignore the re-arrival of the signal before processing the signal.
If the oldact parameter is not a null pointer, the original signal processing method will be returned from the sigaction structure.

Return Value: if the execution is successful, 0 is returned. If an error exists,-1 is returned.

Error Code: The sigval parameter SIGNUM is invalid, or an attempt to intercept the sigkill/sigstopsigkill Signal
The efault parameter Act and the oldact pointer address cannot be accessed.
Eintr this call is interrupted

Example
# Include <unistd. h>
# Include <signal. h>
Void show_handler (struct sigaction * Act)
{
Switch (Act-> sa_flags)
{
Case sig_dfl:
Printf ("default action \ n"); break;
Case sig_ign:
Printf ("ignore the signal \ n"); break;
Default:
Printf ("0x % x \ n", act-> sa_handler );
}
}
Main ()
{
Int I;
Struct sigaction act, oldact;
Act. sa_handler = show_handler;
Act. sa_flags = sa_oneshot | sa_nomask;
Sigaction (SIGUSR1, & act, & oldact );
For (I = 5; I <15; I ++)
{
Printf ("sa_handler of signal % 2D =". I );
Sigaction (I, null, & oldact );
}
}

RUN
sa_handler of signal 5 = default action
sa_handler of signal 6 = default action
sa_handler of signal 7 = default action
sa_handler of Signal 8 = default action
sa_handler of signal 9 = default action
sa_handler of Signal 10 = 0x8048400
sa_handler of signal 11 = default action
sa_handler of signal 12 = default action
sa_handler of Signal 13 = default action
sa_handler of signal 14 = default action

Sigaddset (add a signal to the signal set)
Related functions: sigemptyset, sigfillset, sigdelset, sigismember
Header file: # include <signal. h>
Define the function: int sigaddset (sigset_t * Set, int SIGNUM );
Function Description: sigaddset () is used to add signals represented by the SIGNUM parameter to the set Signal Set parameter.
Return Value: if the execution is successful, 0 is returned. If an error exists,-1 is returned.
Error Code: the set pointer address of the efault parameter cannot be accessed.
The sigval parameter SIGNUM is invalid.

Sigdelset (delete a signal from the signal set)
Related functions: sigemptyset, sigfillset, sigaddset, sigismember
Header file: # include <signal. h>
Define the function: int sigdelset (sigset_t * Set, int SIGNUM );
Function Description: sigdelset () is used to delete signals represented by the SIGNUM parameter from the set signal set.
Return Value: if the execution is successful, 0 is returned. If an error exists,-1 is returned.
Error Code: the set pointer address of the efault parameter cannot be accessed.
The sigval parameter SIGNUM is invalid.

Sigemptyset (initialize Signal Set)
Related functions: sigaddset, sigfillset, sigdelset, sigismember
Header file: # include <signal. h>
Define the function: int sigemptyset (sigset_t * Set );
Function Description: sigemptyset () is used to initialize and clear the set Signal Set parameter.
Return Value: if the execution is successful, 0 is returned. If an error exists,-1 is returned.
Error Code: the set pointer address of the efault parameter cannot be accessed.

sigfillset (add all signals to the signal set)
Functions: sigempty, sigaddset, sigdelset, sigismember
header file: # include
defined function: int sigfillset (sigset_t * Set);
Function Description: sigfillset () is used to initialize the set Signal Set parameter, and then add all signals to this signal set.
return value: 0 if execution is successful, and-1 if any error occurs.
note: the set pointer address of the efault parameter cannot be accessed


Sigismember (test whether a signal has been added to the signal set)
Related functions: sigemptyset, sigfillset, sigaddset, sigdelset
Header file: # include <signal. h>
Define the function: int sigismember (const sigset_t * Set, int SIGNUM );
Function Description: sigismember () is used to test whether the signal represented by the SIGNUM parameter has been added to the set Signal Set parameter. If the signal already exists in the signal set, 1 is returned; otherwise, 0 is returned.
Return Value: if the signal set already exists, 1 is returned. If no, 0 is returned. If an error exists,-1 is returned.
Error Code: the set pointer address of the efault parameter cannot be accessed.
The sigval parameter SIGNUM is invalid.


signal (set Signal Processing Method)
related functions: sigaction, kill, raise
header file: # include
defined function: void (* signal (int signum, void (* Handler) (INT );
Function Description: Si Gnal () sets the signal processing function based on the signal number specified by the SIGNUM parameter. When the specified signal arrives, the function execution specified by handler is redirected. If handler is not a function pointer, it must be one of the following two constants:
sig_ign ignores the signal specified by SIGNUM.
sig_dfl resets the signal specified by the SIGNUM parameter to the preset signal processing method.
for signal numbers and descriptions, see Appendix D
return value: returns the previous signal processing function pointer. If an error exists, returns sig_err (-1 ).
Note: after the signal jumps to the Custom Handler processing function, the system automatically switches the processing function back to the preset processing method. If you want to change this operation, use sigaction ().

For an example, see alarm () or raise ().

Sigpending (query signals shelved)
Related functions: signal, sigaction, sigprocmask, sigsuspend

Header file: # include <signal. h>
Define the function: int sigpending (sigset_t * Set );
Function Description: sigpending () returns the shelved signal set by the set pointer.
Returned value: If the row is successful, 0 is returned. If an error exists,-1 is returned.
Error Code: the set pointer address of the efault parameter cannot be accessed.
Eintr this call is interrupted.


Sigprocmask (query or set the signal mask)
Related functions: signal, sigaction, sigpending, sigsuspend
Header file: # include <signal. h>
Define the function: int sigprocmask (INT how, const sigset_t * Set, sigset_t * oldset );
Function Description: sigprocmask () can be used to change the current signal mask. The operation is determined by the parameter "how ".
The new sig_block signal mask is set by the current signal mask and the signal mask specified by the set parameter.
Sig_unblock deletes the current signal mask from the signal mask specified by the set parameter.
Sig_setmask sets the current signal mask to the signal mask specified by the set parameter.
If the oldset parameter is not a null pointer, the current signal mask will return this pointer.
Return Value: if the execution is successful, 0 is returned. If an error exists,-1 is returned.
Error Code: The efault parameter set cannot be accessed by the oldset pointer address.
Eintr this call is interrupted


Sleep (pause the process for a period of time)
Related functions: signal, alarm
Header file: # include <unistd. h>
Define the function: Unsigned int sleep (unsigned int seconds );
Function Description: Sleep () will suspend the current process until the time specified by seconds is reached or the signal is interrupted.
Return Value: if the process is paused to the time specified by seconds, 0 is returned. If a signal is interrupted, the remaining seconds are returned.

End

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.