The signal mechanism in Linux

Source: Internet
Author: User
Tags arithmetic define function emit posix terminates

Signal is a very important part of Linux programming, this article will detail the basic concept of signal mechanism, the approximate realization method of Linux to signal mechanism, how to use signal, and several system calls about signal.

Signal mechanism is a method of transmitting messages between processes, which is called soft interrupt signal, and some people call it soft interrupt. It can be seen from its nomenclature that its substance and use are much like interruptions. Therefore, the signal can be said to be part of the process control.

First, the basic concept of the signal

This section first introduces some basic concepts of the signal and then gives some basic signal types and events corresponding to the signals. The basic concepts are particularly important for understanding and using signals in the comprehension of signal mechanisms. Here's a look at what the signal is.

1. Basic Concepts

A soft interrupt signal (signal, also referred to as a signal) is used to notify the process that an asynchronous event has occurred. Processes can send soft interrupt signals to each other through system call kill. The kernel can also send a signal to the process because of an internal event, notifying the process that an event has occurred. Note that the signal is only used to notify a process of what has happened and does not pass any data to the process.

The process of receiving signals has different processing methods for various signals. The processing method can be divided into three categories: the first is similar to the interrupt handler, for the signal to be processed, the process can specify a handler function, which is handled by the function. The second method is to ignore a signal and do nothing about it, as if it had not happened. The third method is that the processing of the signal retains the default value of the system, and the default operation for most signals is to cause the process to terminate. The process calls signal through the system to specify the processing behavior of a process for a signal.

There is a soft interrupt signal field in the table entry for the process table, where each bit of the field corresponds to a signal that corresponds to the position bit when a signal is sent to the process. As you can see, the process can keep the different signals at the same time, but for the same signal, the process does not know how many have come before processing.

2. Type of signal

There are many reasons for signaling, which are simply categorized by the cause of the signal to understand the various signals:

(1) Signals related to the termination of the process. This type of signal is emitted when the process exits, or when the child process terminates.

(2) Signals related to process exception events. such as a process that is out of bounds, or an attempt to write a read-only memory area (such as a program body area), or perform a privileged instruction and various other hardware errors.

(3) A signal associated with an unrecoverable condition encountered during a system call. If the system calls exec, the original resources have been freed, and the system resources are now exhausted.

(4) A signal related to the non-predictive error condition encountered while executing the system call. such as executing a system call that does not exist.

(5) A signal sent by the process in the user state. As the process calls the system, call kill to send a signal to other processes.

(6) signal associated with the terminal interaction. If the user closes a terminal, or presses the Brea

K-Key and other conditions.

(7) Trace the signal of the process execution.

The list of supported Linux signals is as follows. Many of the signals are related to the architecture of the machine, and the signals listed in Posix.1 are listed first:

Signal value processing action causes the signal

----------------------------------------------------------------------

SIGHUP 1 A terminal hangs or the control process terminates

SIGINT 2 A Keyboard interrupt (if the break key is pressed)

Sigquit 3 C Keyboard exit key is pressed

Sigill 4 C Illegal Instruction

SIGABRT 6 C Exit instruction issued by abort (3)

SIGFPE 8 C Floating-point exception

SIGKILL 9 AEF Kill signal

SIGSEGV-C Invalid memory reference

Sigpipe a pipeline rupture: write a pipeline without a read port

SIGALRM a signal sent by alarm (2)

SIGTERM A Stop Signal

SIGUSR1 30,10,16 A User-defined signal 1

SIGUSR2 31,12,17 A user-defined signal 2

SIGCHLD 20,17,18 B Sub-process end signal

Sigcont 19,18,25 Process continues (a process that has been stopped)

SIGSTOP 17,19,23 DEF Terminating process

SIGTSTP 18,20,24 D Control Terminal (TTY) press the STOP key

Sigttin 21,21,26 D Background process attempts to read from the control terminal

Sigttou 22,22,27 D Background process attempts to write from the control terminal

The following signals are not listed in Posix.1, but are listed in SUSv2

Signal value processing action causes the signal

--------------------------------------------------------------------

Sigbus 10,7,10 C Bus error (bad memory access)

Sigpoll a Sys v-defined pollable event, synonymous with Sigio

Sigprof 27,27,29 A Profiling timer to

Sigsys,-, C Invalid system call (SVID)

SIGTRAP 5 C Trace/Breakpoint Capture

Sigurg 16,23,21 B Socket Emergency condition (4.2 BSD)

SIGVTALRM 26,26,28 A Real time alarm clock signal (4.2 BSD)

SIGXCPU 24,24,30 C exceeds the set CPU time limit (4.2 BSD)

Sigxfsz 25,25,31 C exceeds the set file size limit (4.2 BSD)

(For Sigsys,sigxcpu,sigxfsz, and the Sigbus,linux default action for some machine architectures is a (terminate), SUSv2 is C (Terminate and dump core).

Here are some other signals.

Signal value processing action causes the signal

----------------------------------------------------------------------

Sigiot 6 C io capture instruction, synonymous with SIGABRT

SIGEMT 7,-, 7

Sigstkflt-,16,-A coprocessor Stack Error

SIGIO 23,29,22 A I/O operation is now available (4.2 BSD)

SIGCLD-,-, A and SIGCHLD synonymous

SIGPWR 29,30,19 A power failure (System V)

Siginfo,-,-A and SIGPWR synonymous

Siglost-,-,-A file lock is missing

Sigwinch 28,28,20 B window size change (4.3 BSD, Sun)

sigunused-,31,-A not used signal ('ll be Sigsys)

(Here,-indicates that the signal is not implemented; There are three values given

The meaning is that the first value is usually valid on Alpha and SPARC, the median value corresponds to i386 and PPC, and SH, and the last value corresponds to MIPS. The Signal 29 is SIGINFO/SIGPWR on Alpha and siglost on SPARC. )

The letters in an item of a processing action have the following meanings

A The default action is to terminate the process

B The default action is to ignore this signal

C The default action is to terminate the process and kernel image dumps (dump core)

D The default action is to stop the process

E-signal cannot be captured

F signal cannot be ignored

The signals described above are supported by common systems. The names and functions of various signals and their processing actions by default are presented in tabular form. The meaning of the various default processing actions is that the terminating program refers to the process exiting; ignoring the signal is to discard the signal, not to do the processing, the Stop program is the program hangs, enter the stop condition can be restarted, usually in the process of debugging (such as Ptrace system call) Kernel image dump refers to the process data in the memory of the image and process stored in the kernel structure of the contents of a certain format dump to the file system, and the process exits execution, the advantage is to provide programmers with convenience, so that they can get the process at the time of execution of data values, allowing them to determine the reason for the dump, And can debug their programs.

Note that signals Sigkill and sigstop can neither be captured nor ignored. Signal Sigiot and SIGABRT are a signal. It can be seen that the same signal may not be the same in different systems, so it is advisable to use the name defined for the signal rather than using the value of the signal directly.

Third, the system call about the signal

Most of the information about the signal is covered in the previous two sections. Let's take a look at these system calls in this section. wherein, the system call signal is the process used to set a signal processing method, system call Kill is used to send a signal to the specified process. These two calls can form the basic operation of the signal. The latter two calls to pause and alarm are implemented by signaling process pauses and timers, and calling alarm is signaled by signaling the process timer to the time. So here we are going to cover both of these calls.

1. Signal system call

The system calls signal to set a signal processing method. The invocation declaration is in the following format:

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

Add the following header file to the process that uses the call:

#include <signal.h>

The above declaration format is complex and can be used in a format defined by the following type if it is not clear how to use it (POSIX definition):

typedef void (*sighandler_t) (int);

sighandler_t signal (int signum, sighandler_t handler);

However, this format has different type definitions in different systems, so it is best to refer to the online manual for this format.

In the call, the parameter Signum indicates the signal to set the processing method. The second parameter, handler, is a handler function, or

S

Ig_ign: Ignores the signal indicated by the parameter signum.

SIG_DFL: The default value is the processing method of the signal Signum by the recovery parameter.

The integer parameter passed to the signal processing routine is the signal value, which allows a signal processing routine to process multiple signals. The system call signal return value is the specified signal Signum the previous processing routine or error when the error code Sig_err is returned. Let's look at a simple example:

#include <signal.h>

#include <unistd.h>

#include <stdio.h>

void sigroutine (int dunno) {/* signal processing routine where dunno will get the value of the signal */

Switch (dunno) {

Case 1:

printf ("Get a signal--SIGHUP");

Break

Case 2:

printf ("Get a signal--SIGINT");

Break

Case 3:

printf ("Get a signal--sigquit");

Break

}

Return

}

int main () {

printf ("Process ID is%d", getpid ());

Signal (SIGHUP, sigroutine); * The following three signal processing methods are set

Signal (SIGINT, sigroutine);

Signal (Sigquit, sigroutine);

for (;;);

}

wherein the signal SIGINT from the press of Ctrl-c issued, the signal sigquit by pressing Ctrl-out. The results of the program execution are as follows:

localhost:~$./sig_test

Process ID is 463

Get a Signal-sigint//press CTRL-C to get results

Get a Signal-sigquit//press CTRL to get results

Press Ctrl-z to place the process in the background

[1]+ Stopped./sig_test

localhost:~$ BG

[1]+./sig_test &

localhost:~$ kill-hup 463//Send sighup signal to process

localhost:~$ Get a Signal–sighup

Kill-9 463//Send Sigkill signal to process, terminate process

localhost:~$

2. Kill system Call

System call Kill is used to send a signal to the process. The invocation declaration is in the following format:

int Kill (pid_t pid, int sig);

Add the following header file to the process that uses the call:

#include <sys/types.h>

#include <signal.h>

This system call can be used to send any signal to any process or process group. If the parameter PID is a positive number, the call sends the signal SIG to a process with a PID. If the PID is equal to 0, then the signal sig is sent to all processes in the process group to which the current process belongs. If the parameter PID equals-1, the signal sig is sent to all processes except process 1 and itself. If the parameter PID is less than-1, the signal sig is sent to all processes that belong to the process group-pid. If the parameter sig is 0, no signal will be sent. When the call executes successfully, the return value is 0, the error returns 1, and the corresponding error code errno is set. Here are some of the error codes that may be returned:

EINVAL: The specified signal sig is not valid.

Esrch: Parameter pid The specified process or process group does not exist. Note that the process that exists in the process table entry may be a zombie process that has not been recovered by wait, but has terminated execution.

Eperm: The process has no power

This signal is sent to the process that specifies the receiving signal. Because a process is allowed to send a signal to the process PID, it must either have root authority, or the UID or euid of the process that issued the call is the same as the UID or save user ID (Savedset-user-id) of the process that you specify to receive. If the parameter PID is less than-1, that is, the signal is sent to a group, the error indicates that a member process in the group cannot receive the signal.

3. Pause system Call

The function of the system call to pause is to wait for a signal. The invocation is declared in the following format:

int pause (void);

Add the following header file to the process that uses the call:

#include <unistd.h>

This call causes the process that makes the call to sleep until a signal is received. The call always returns-1 and sets the error code to EINTR (a signal is received). The following is a simple example:

#include <unistd.h>

#include <stdio.h>

#include <signal.h>

void Sigroutine (int unused) {

printf ("Catch a Signal SIGINT");

}

int main () {

Signal (SIGINT, sigroutine);

Pause ();

printf ("Receive a signal");

}

In this example, the program begins to execute as if it had entered a dead loop, because the process is waiting for the signal, and when we press CTRL-C, the signal is captured and the pause exits the wait state.

4, Alarm and Setitimer system calls

The function of the system call alarm is to set a timer that will emit a signal to the process when the timer arrives. The invocation is declared in the following format:

unsigned int alarm (unsigned int seconds);

Add the following header file to the process that uses the call:

#include <unistd.h>

The system calls alarm to schedule the kernel to emit a SIGALRM signal for the calling process after the specified seconds seconds. If the specified parameter seconds is 0, the SIGALRM signal is no longer sent. The next setting cancels the previous setting. The call returns a value that is the time remaining between the last scheduled call to the send or 0 because there is no previous scheduled call.

Note that when used, the alarm is only set to send a signal once, and if it is to be sent multiple times, it will be called multiple times using alarm.

For alarm, here is no longer an example. Many programs in the current system no longer use the alarm call, but instead use the Setitimer to set the timer, using the Getitimer to get the state of the timer, the declaration format of the two calls is as follows:

int Getitimer (int which, struct itimerval *value);

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

Add the following header file to the process that uses the two calls:

#include <sys/time.h>

The system calls the process to provide three timers, each with its own unique timing domain, when any one arrives, sends a corresponding signal to the process, and causes the timer to start over again. The three timers are specified by the parameter which, as follows:

Timer_real: According to the actual time, counting

The SIGALRM signal is sent to the process when it arrives.

Itimer_virtual: Timing occurs only when the process is executing. The timed arrival will send a SIGVTALRM signal to the process.

Itimer_prof: Timed when the process executes and when the system performs an action for the process. As with itimer_vir-tual, this timer is often used to count the time spent by the process in the user state and kernel state. The timed arrival will send a sigprof signal to the process.

The parameter value in the timer is used to indicate the time of the timer, and its structure is as follows:

struct Itimerval {

struct Timeval it_interval; /* Next time value */

struct Timeval it_value; /* Set value for this time */

};

The structure of the TIMEVAL structure is defined as follows:

struct Timeval {

Long tv_sec; /* sec */

Long tv_usec; /* microseconds, 1 seconds = 1000000 microseconds */

};

In the Setitimer call, the parameter Ovalue if it is not empty, it retains the value of the last invocation setting. The timer decrements the It_value to 0 o'clock, generates a signal, sets the value of the It_value to It_interval value, and then restarts the chronograph, again and again. When It_value is set to 0 o'clock, the timer stops, or when it time expires, and It_interval stops at 0 o'clock. When the call succeeds, return 0; error, return-1, and set the corresponding error code errno:

Efault: The parameter value or Ovalue is an invalid pointer.

EINVAL: Parameter which is not one of itimer_real, Itimer_virt, or itimer_prof.

Here is a simple demonstration of the Setitimer call, in which a sigalrm is emitted every 0.5 seconds and a SIGVTALRM signal is emitted:

#include <signal.h>

#include <unistd.h>

#include <stdio.h>

#include <sys/time.h>

int sec;

void Sigroutine (int signo) {

Switch (signo) {

Case SIGALRM:

printf ("Catch a Signal-sigalrm");

Break

Case SIGVTALRM:

printf ("Catch a Signal-sigvtalrm");

Break

}

Return

}

int main () {

struct Itimerval value,ovalue,value2;

SEC = 5;

printf ("Process ID is%d", getpid ());

Signal (SIGALRM, sigroutine);

Signal (SIGVTALRM, sigroutine);

Value.it_value.tv_sec = 1;

value.it_value.tv_usec = 0;

Value.it_interval.tv_sec = 1;

value.it_interval.tv_usec = 0;

Setitimer (Itimer_real, &value, &ovalue);

value2.it_value.tv_sec = 0;

Value2.it_value.tv_usec = 500000;

value2.it_interval.tv_sec = 0;

Value2.it_interval.tv_usec = 500000;

Setitimer (Itimer_virtual, &value2, &ovalue);

for (;;);

}

The screen copy of this example is as follows:

localhost:~$./timer_test

Process ID is 579

Catch a SIGNAL–SIGVTALRM

Catch a SIGNAL–SIGALRM

Catch a SIGNAL–SIGVTALRM

Catch a SIGNAL–SIGVTALRM

Catch a SIGNAL–SIGALRM

Catch

A SIGNAL–GVTALRM

$ kill-l 1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill 5) SIGTRAP 6) SIGABRT 7) Sigbus 8) SIGFPE 9) SIGKILL) SIGUSR1) SI GSEGV) (SIGUSR213) sigpipe) sigalrm) SIGTERM) SIGCHLD18) sigcont) SIGSTOP) SIGTSTP Sigurg) sigxcpu) SIGXFSZ26) sigvtalrm sigprof) sigwinch) SIGIO30) SIGPWR Sigsys) Sigrtmin 35) sigrtmin+136) sigrtmin+2 (Panax Notoginseng) sigrtmin+3 () sigrtmin+4 () sigrtmin+540) sigrtmin+6) sigrtmin+7) sigrtmin+8 tmin+944) (sigrtmin+10) sigrtmin+11 () sigrtmin+12) sigrtmin+1348) sigrtmin+14) sigrtmin+15) SIGRTMAX-14) S IGRTMAX-1352) (SIGRTMAX-12) (SIGRTMAX-11) (SIGRTMAX-10) SIGRTMAX-956) SIGRTMAX-8 () SIGRTMAX-7) SIGRTMAX-6) S IGRTMAX-560) SIGRTMAX-4 (SIGRTMAX-3) (SIGRTMAX-2) SIGRTMAX-164) Sigrtmax list, signals of 1 to 31 are signals that are supported by traditional UNIX and are unreliable signals (not real-time ), the signal number 32 ~ 63 is later expanded, called a reliable signal (real-time signal). The difference between unreliable and reliable signals is that the former does not support queueing and may cause signal loss, while the latter does not.

Below we discuss a signal with a number less than sigrtmin.

1) SIGHUP

This signal is issued at the end of the user terminal connection (normal or abnormal), usually at the end of the control process of the terminal to notify the same session of the various jobs, when they are no longer associated with the control terminal.

When you log in to Linux, the system is assigned to a terminal (Session) of the logged-on user. All programs running at this terminal, including the foreground process group and the background process group, usually belong to this Session. When the user exits the Linux login, the foreground process group and the background will receive a sighup signal to the terminal output process. The default action for this signal is to terminate the process, so the foreground process group and the process that has terminal output in the background will be aborted. However, this signal can be captured, such as wget can capture the sighup signal, and ignore it, so even if you quit the Linux login, wget can continue to download.

In addition, for daemons with terminal disengagement, this signal is used to notify it to reread the configuration file.

2) SIGINT

A program termination (interrupt) signal, issued when the user types the intr character (usually ctrl-c), to inform the foreground process group that the process is terminated.

3) Sigquit

Similar to SIGINT, but controlled by the quit character (usually ctrl-\). The process generates a core file when it receives a sigquit exit, similar to a program error signal in this sense.

4) Sigill

An illegal instruction was executed. This is usually due to an error in the executable file itself or an attempt to execute a data segment. This signal can also be generated when a stack overflows.

5) SIGTRAP

Generated by a breakpoint instruction or other trap instruction. Used by debugger.

6) SIGABRT

Invokes the signal generated by the abort function.

7) Sigbus

Illegal address, including memory address alignment (alignment) error. For example, a four-word integer is accessed, but its address is not a multiple of 4. It differs from SIGSEGV in that the latter is due to the

(such as Access does not belong to its own storage space or read-only storage).

8) SIGFPE

Emitted when a fatal arithmetic operation error occurs. This includes not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and divisor 0.

9) SIGKILL

Used to immediately end the run of the program. This signal cannot be blocked, processed, or ignored. If an administrator discovers that a process is not terminating, try sending this signal.

) SIGUSR1

Leave it to the user

One) SIGSEGV

An attempt was made to access memory that is not assigned to itself or to write data to a memory address that does not have write permissions.

SIGUSR2)

Leave it to the user

Sigpipe)

Pipe rupture. This signal is usually generated between interprocess communication, such as two processes with FIFO (pipeline) communication, the read pipeline is not opened or terminated unexpectedly to the pipeline to write, the writing process will receive sigpipe signal. In addition, two processes with the socket communication, the write process when the socket is written, the read process has been terminated.

SIGALRM)

A clock timing signal that calculates the actual time or clock time. The alarm function uses this signal.

SIGTERM)

The end of program (terminate) signal, unlike Sigkill, is that the signal can be blocked and processed. Typically used to require the program to exit gracefully, the shell command kill generates this signal by default. If the process does not stop, we will try to Sigkill.

SIGCHLD)

The parent process receives this signal at the end of the child process.

If the parent process does not process the signal and does not wait for the (wait) child process, although the child process terminates, it also occupies the table entry in the kernel process table, when the child process is called a zombie process. In this case we should avoid (the parent process either ignores the sigchild signal, or catches it, or wait for its derived child process, or the parent process terminates first, and the termination of the child process is automatically taken over by the Init process).

) Sigcont

Let a stop (stopped) process continue execution. This signal cannot be blocked. You can use a handler to get the program to do certain work when the stopped state changes to continue. For example, to re-display the prompt

SIGSTOP)

Stop (stopped) the execution of the process. Notice the difference between it and terminate and interrupt: The process is not over yet, just pause execution. This signal cannot be blocked, processed or ignored.

SIGTSTP)

Stops the process from running, but the signal can be processed and ignored. This signal is emitted when the user types Susp characters (usually ctrl-z)

) Sigttin

When a background job wants to read data from the user terminal, all processes in the job receive a sigttin signal. By default, these processes stop executing.

Sigttou)

Similar to Sigttin, but received when writing a terminal (or modifying terminal mode).

Sigurg)

When there is "emergency" data or Out-of-band data arrives at the socket.

) sigxcpu

CPU time resource limit exceeded. This restriction can be read/changed by Getrlimit/setrlimit.

SIGXFSZ)

When the process attempts to enlarge the file so that it exceeds the file size resource limit.

SIGVTALRM)

Virtual clock signal. is similar to SIGALRM, but calculates the CPU time that is consumed by the process.

SIGPROF)

Similar to SIGALRM/SIGVTALRM, but includes the CPU time used by the process and the time of the system call.

Sigwin)

Ch

emitted when the window size changes.

) SIGIO

The file descriptor is ready to start the input/output operation.

SIGPWR)

Power failure

Sigsys)

An illegal system call.

In the signals listed above, the program can not capture, block or ignore the signal is: sigkill,sigstop

Signals that cannot be restored to the default action are: Sigill,sigtrap

The default causes the process to abort the signal is: Sigabrt,sigbus,sigfpe,sigill,sigiot,sigquit,sigsegv,sigtrap,sigxcpu,sigxfsz

The signal that causes the process to exit by default is: SIGALRM,SIGHUP,SIGINT,SIGKILL,SIGPIPE,SIGPOLL,SIGPROF,SIGSYS,SIGTERM,SIGUSR1,SIGUSR2,SIGVTALRM

The signal that causes the process to stop by default is: Sigstop,sigtstp,sigttin,sigttou

The default process ignores signals such as: Sigchld,sigpwr,sigurg,sigwinch

In addition, Sigio in SVR4 is exited, is ignored in 4.3BSD, Sigcont is continued when the process hangs, otherwise it is ignored and cannot be blocked.

Linux signal Processing related functions

Alarm (Set signal transfer alarm)

Correlation function Signal,sleep

Table header file #include <unistd.h>

Defines the function unsigned int alarm (unsigned int seconds);

The function Description Alarm () is used to set the signal SIGALRM to the current process after the number of seconds specified by the parameter seconds. If the parameter seconds is 0, the previously set alarm is canceled and the remaining time is returned.

The return value returns the number of seconds left before the alarm, or 0 if no alarm was previously set.

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);

}

}

Perform sleep 1 ...

Sleep 2 ...

Sleep 3 ...

Sleep 4 ...

Sleep 5 ...

Hello

Sleep 6 ...

Kill (transmits the signal to the specified process)

Correlation function Raise,signal

Table header file #include <sys/types.h>

#include <signal.h>

define function int Kill (pid_t pid,int SIG);

The function description Kill () can be used to send the parameter sig-specified signal to the parameter PID specified by the process. There are several conditions for parameter PID:

Pid>0 the process of transmitting the signal to the process identification code as PID.

Pid=0 transmit the signal to all processes of the same process group as the current process

PID=-1 transmit the signal broadcast to all processes within the system

Pid<0 all processes that transmit the signal to the process group ID as the PID absolute value

The signal number represented by the parameter sig can be referenced in Appendix D

The return value returns 0 if successful execution returns 1 if there is an error.

Error code EINVAL parameter sig is not legal

Esrch parameter PID the process or group of processes specified does not exist

Eperm not enough permission to transmit signal 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);

printf ("end\n"); Found this sentence does not print, the process was terminated prematurely

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));

}

}

Execute SEN signal to child

Process (3170)

Hi I am Child process!

Child process receive signal 6

Pause (Let the process pause until the signal appears)

Correlation function Kill,signal,sleep

Table header file #include <unistd.h>

Defines the function int pause (void);

The function description Pause () pauses the current process (goes to sleep) until it is interrupted by the signal (signal).

The return value returns only-1.

Error code EINTR This function is interrupted by a signal arrival.

Sigaction (query or Set signal processing mode)

Correlation function Signal,sigprocmask,sigpending,sigsuspend

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);

}

Sa_handler and Sa_sigaction, which represent new signal processing functions, refer to signal () for other meanings.

The sa_mask is used to set the Sa_mask specified signal to be temporarily shelved 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.

The OR operation (|) combination

A_nocldstop: If the parameter Signum is SIGCHLD, the parent process is not notified when the child process pauses

Sa_oneshot/sa_resethand: This signal processing mode is changed to the system preset before the new signal processing function is called.

Sa_restart: A system call that is interrupted by a signal will restart itself

Sa_nomask/sa_nodefer: Ignore this signal again before processing this signal is not over.

Sa_siginfo: Indicates that the siginfo_t supplied with the signal can be transmitted to the signal processing function. This flag bit should be set when using the Sa_sigaction () function.

If the parameter oldact is not a null pointer, the original signal processing method is returned by this structure sigaction.

The return value returns 0 if successful execution returns 1 if there is an error.

Sigemptyset (Initialize signal set)

Correlation function Sigaddset,sigfillset,sigdelset,sigismember

Table header file #include <signal.h>

define function int Sigemptyset (sigset_t *set);

The function Description Sigemptyset () is used to initialize and empty the parameter set signal sets.

The return value returns 0 if successful execution returns 1 if there is an error.

Error code Efault parameter set pointer address cannot be accessed

Signal mechanism in Linux (RPM)

Related Article

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.