Linux signal Programming

Source: Internet
Author: User
Tags sigalarm

 

Signal, an event notification that the OS sends to proc. Each signal corresponds to a system event. The signal is transmitted to proc, Which is captured and executed when the kernel state is switched back to the user State. The specific execution mechanism is that the kernel state jumps to the processing function corresponding to the signal. After the signal processing function is executed, it switches back to the user State. Therefore, while the signal processing function is executed, the task of proc itself will be in sleep state and will not run. In user mode, proc does not capture processing signals. Note This in actual programming.

In Linux, CITIC numbers are classified into two types: Unreliable signals and reliable signals. All signals whose signal values are smaller than SIGRTMIN (32) are unreliable. The signal between SIGRTMIN (32) and SIGRTMAX (64) is reliable. Reliability and reliability mainly apply to signal loss. (In many cases, the signal will be changed to the default value after processing, which is no longer meaningful because it does not need to be re-installed .) Reliable Signals support signal queues without signal loss. unreliable signals do not support queues. For more information, see the relevant documentation.

The signal programming steps are mainly divided:

1. signal capturing process:

SIGNAL INSTALLATION

Implement the signal processing function.

2. signal sending process:

Signal sending

Applications can process signals in three modes: Ignore, default, and capture.

 

Function for sending signals:

Kill ()

# Include <sys/types. h>

# Include <signal. h>

Int kill (pid_t pid, int signo)

Note: pid> proc where the 0 process is pid

Pid = 0 proc of the same pg

Pid =-1 all processes except sending processes whose IDs are greater than 1

Pid <0 & pid! =-1 All proc whose pgid is pid

Signo is the signal value. When the value is 0, only routine checks are performed if no signal is sent. Checks whether the target process exists and whether the current process has the permission to send signals to the target process.

If the call is successful, 0 is returned. Otherwise,-1 is returned.

Raise ()

# Include <signal. h>

Int raise (int signo)

Sends a signal to the current process. If the call is successful, 0 is returned. Otherwise,-1 is returned.

Sigqueue ()

# Include <sys/types. h>

# Include <signal. h>

Int sigqueue (pid_t pid, int sig, const union sigval val)

 

Typedef union sigval {

Int sival_int;

Void * sival_ptr

} Sigval_t;

 

This function is a new function for installing signals. Compared with kill, the parameter val is used to transmit additional information to the signal processing function. Its disadvantage is that it can only send signals to one process. If the sig is zero, its working mechanism is consistent with that of kill, which is a routine check.

 

Alarm ()

# Include <unistd. h>

Unsigned int alarm (unsigned int seconds );

A timer specially set for the SIGALARM signal, that is, triggered after the specified seconds, sends the SIGALARM signal to the current process itself. After a process calls this method, it directly overwrites the previous call. That is, multiple calls are only valid for the last time. The returned value is related to whether the method has been called. If the alarm time is set before, returns the remaining time of the previous alarm time. Otherwise, 0 is returned. After the alarm time is triggered, the setting will be automatically canceled. Therefore, when multiple calls are required, you need to reset the alarm time after processing. This function is rarely used currently.

Setitimer ()

# Include <sys/time. h>

Int setitimer (int which, const struct itimerval * value, struct itimerval * ovalue ));

Struct itimerval {

Struct timeval it_interval;/* next value */

Struct timeval it_value;/* current value */

};

 

Struct timeval {

Long TV _sec;/* seconds */

Long TV _usec;/* microseconds */

};

Functions are similar to those of alarm, but they are much more powerful. Its working mechanism is that value automatically declines its member it_value to zero, trigger the signal SIGALARM, and then fill it_value with it_interval to continue periodic execution. When both it_interval and it_value are zero, the timer is canceled. Therefore, if you set a one-time timer, you only need to set the it_value value, and set it_interval to zero. If you need to set a periodic timer, you must set the it_interval value.

The which parameter has three optional values: ITIMER_REAL sets the absolute time, corresponding to the signal SIGALARM, ITIMER_VIRTUAL program execution time, corresponding to the signal SIGVALARM, ITIMER_PROF process execution, and Kernel Time consumed by this process and, corresponding signal SIGVALARM.

Abort ()

# Include <stdlib. h>

Void abort (void );

Sends a SIGABORT signal to the process itself. After a process calls its corresponding processing function, it can still receive the SIGABORT signal.

Signal installation:

Signal ()

# Include <signal. h>

Typedef void (* sighandler_t) (int );

Sighandler_t signal (int signum, sighandler_t handler );

The signum parameter is the signal value, and the handler is the processing function handle. If this parameter is ignored, use SIG_IGN. If it is ignored by default, use SIG_DFL. If it is captured, use the entry address of the custom processing function. If the call is successful, the processing function handle is returned. Otherwise, the SIG_ERR is returned.

Sigaction ()

# Include <signal. h>

Int sigaction (int signum, const struct sigaction * act, struct sigaction * oldact ));

 

Struct sigaction {

Union {

_ Sighandler_t _ sa_handler;

Void (* _ sa_sigaction) (int, struct siginfo *, void *);

} _ U

Sigset_t sa_mask;

Unsigned long sa_flags;

Void (* sa_restorer) (void );

}

 

Signum corresponds to the signal to be installed. The act member sa_handler corresponds to the entry address of the processing function of _ sa_sigaction. Only one sa_handler and _ sa_sigaction can be specified. Here, _ sa_handler only has one parameter, which is a signal value. If function _ sa_sigaction is used, three parameters exist, and the third parameter is invalid. The first parameter is the signal value, and the second parameter is a struct. Its structure is as follows:

Struct siginfo_t {

Int si_signo;/* Signal number */

Int si_errno;/* An errno value */

Int si_code;/* Signal code */

Int si_trapno;/* Trap number that caused

Hardware-generated signal

(Unused on most ubuntures )*/

Pid_t si_pid;/* Sending process ID */

Uid_t si_uid;/* Real user ID of sending process */

Int si_status;/* Exit value or signal */

Clock_t si_utime;/* User time consumed */

Clock_t si_stime;/* System time consumed */

Sigval_t si_value;/* Signal value */

Int si_int;/* POSIX.1b signal */

Void * si_ptr;/* POSIX.1b signal */

Int si_overrun;/* Timer overrun count; POSIX.1b timers */

Int si_timerid;/* Timer ID; POSIX.1b timers */

Void * si_addr;/* Memory location which caused fault */

Int si_band;/* Band event */

Int si_fd;/* File descriptor */

}

The void * si_ptr is 4 bytes, which corresponds to the third parameter of the previously sent signal function sigqueue. This item is highly scalable in practical applications.

Sa_mask identifies the signals that will be blocked during the execution of the signal processing function. By default, the current signal is blocked to prevent nested transmission of signals. Signal Set processing functions include:

# Include <signal. h>

Int sigemptyset (sigset_t * set); // initializes the signal set specified by the set, and all signals in the signal set are cleared;

Int sigfillset (sigset_t * set); // after calling this function, the signal set points to will contain 64 types of signals supported by linux;

Int sigaddset (sigset_t * set, int signum); // Add the signum signal to the signal set;

Int sigdelset (sigset_t * set, int signum); // Delete the signum signal in the set Signal set;

Int sigismember (const sigset_t * set, int signum);) // determines whether the signal signum is in the set signal set.

Sa_flags contains many flag BITs, including the previously mentioned SA_NODEFER and SA_NOMASK. Another important flag is SA_SIGINFO. When this flag is set, the parameters attached to the signal can be passed to the signal processing function. Therefore, you should specify a processing function for sa_sigaction in the sigaction structure instead of a signal processing function for sa_handler. Otherwise, setting this flag becomes meaningless. Even if a signal processing function is specified for sa_sigaction, if SA_SIGINFO is not set, the signal processing function cannot obtain the data transmitted from the signal, access to the information in the signal processing function will cause a segment error.

 

Process-related signal operation functions

// Description of the parameter "how:

// Add the signal set pointing to the signal set in the current blocked Signal set of the Process in SIG_BLOCK

// SIG_UNBLOCK if the process blocking signal contains the signal set points to the signal set, the blocking of the signal will be lifted.

// SIG_SETMASK updates the signal set pointed to by set as the blocked Signal set of the process

Int sigprocmask (int how, const sigset_t * set, sigset_t * oldset ));

Int sigpending (sigset_t * set); // obtain all signals that have been delivered to the process but are blocked, and return results in the signal set to which the set points.

Int sigsuspend (const sigset_t * mask); // used to temporarily replace the signal mask of the process with the mask before receiving a signal, and pause the process until it receives the signal. After sigsuspend is returned, the signal mask before the call is restored. After the signal processing function is complete, the process continues to run. The system always returns-1 and sets errno to EINTR.

Void pause (void); process blocking waits until a signal is triggered.

 

 

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.