Advanced Programming for the UNIX environment-10. Signals-Reading notes

Source: Internet
Author: User
Tags terminates

First, the basic concept

1, the signal is software interruption. Signals provide a way to handle events asynchronously.

2. Each signal has a name: These names begin with the sig.

For example: SIGABRT is the aborted signal, call the Abort function, SIGALRM is the alarm clock signal, when the timer set by the alarm function time out after the signal is generated;

3. Linux supports 31 different signals, but it also supports the additional signals defined by the application as a real-time extension.

4, the head file is: <signal.h>

5, there is no signal numbered 0, the KILL function uses a signal numbered 0 as a test process of the existence of the role.

7. The Sigint:ctrl+c key usually generates an interrupt signal (SIGINT), which is a way to stop a program that has been out of control.

8, SIGSEGV: hardware abnormal produce signal: divisor is 0, invalid memory reference, etc., these are detected by hardware, produce SIGSEGV signal

9, Sigurg: When the network connection came out of out-of-band data

10, Sigpipe: After the pipe reading process terminates, the process is written to this pipeline, the Sock_stream type of socket closure will also produce

11. There are two types of signals that cannot be ignored and captured:

SIGKILL:

SIGSTOP:

The reason why they cannot be ignored is that they provide a reliable way for the superuser to terminate or stop the process

12, capture the signal: to notify the kernel in the event of a signal to invoke a user function, in the user function, the executable user wants to deal with such events.

For example:

If a sigchld signal is captured, it indicates that a child process has terminated, so the capture function of this signal can call Waitpid to get the process ID of the child process and its terminating state.


Second, the type of signal:

1. Important Excerpt:

SIGINT: When the user presses the break key (typically with delete or CTRL + C), the terminal driver generates this signal and sends it to each process in the foreground process group. This signal is used to terminate a process when it is running out of control, especially when a large amount of output is generated.

SIGTERM: System default termination signal sent by the KILL command

Sigquit: This signal is generated when the user presses the exit key on the terminal (typically with ctrl+\) and is sent to all processes in the foreground process group. This signal not only terminates the foreground process group, but also produces a core file.

SIGKILL: This is one of two signals that can't be ignored and captured.

SIGSTOP: This is one of two signals that cannot be ignored and captured: This is a job control signal that is used to stop a process.

SIGCHLD: When a process terminates or stops, the SIGCHLD signal is sent to its parent process, which is ignored by default on the system. This signal should be captured if the parent process wishes to be informed of this state change in its child processes. The signal capture function typically calls a waitpid function to get the child process ID and its terminating state.

SIGHUP: If the terminal interface detects a broken link, this signal is sent to the terminal-related control process (session first process).


2. All signals:

1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill

  5)  SIGTRAP       6)  sigabrt      7)  SIGBUS        8 )  sigfpe

  9)  SIGKILL   &NBSP;&NBSP;&NBSP;10)  sigusr1     11)  SIGSEGV      ) &NBSP;SIGUSR2

)  sigstkflt

) &NBSP;SIGTSTP

)  sigxcpu

)  sigwinch

)  sigrtmin

(sigrtmin+1) (sigrtmin+2) sigrtmin+3) sigrtmin+4

sigrtmin+5) sigrtmin+6 sigrtmin+7) sigrtmin+8

sigrtmin+9) (sigrtmin+10) sigrtmin+11) sigrtmin+12

sigrtmin+13) (sigrtmin+14) sigrtmin+15) SIGRTMAX-14

SIGRTMAX-13) SIGRTMAX-12-SIGRTMAX-11) SIGRTMAX-10

SIGRTMAX-9) SIGRTMAX-8 () SIGRTMAX-7) SIGRTMAX-6

SIGRTMAX-5) SIGRTMAX-4 () SIGRTMAX-3) SIGRTMAX-2

SIGRTMAX-1) Sigrtmax



Three, signal function: Signal processing program, signal capturing function


#include <signal.h>

void (*signal (int signo, void (*func) (int))) (int)

If an error returns SIG_ERR

Note: Because the semantics of signal are related to implementations, it is best to use the Sigaction function instead of the signal function.


Signo:


The value of func: (1) constant Sig_ign, constant Sig_del (2) The address of the function to invoke when snapping to this signal.


If you specify sig_ign: Indicates that this signal is ignored to the kernel

If you specify Sig_del: Indicates that the system default action is taken when snapping to this signal



IV. Testing the Signal function

The program can run:

#include <pthread.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include < string.h> #include <string>pthread_mutex_t g_mut;pthread_cond_t g_cond;std::string msg;void sig_capture (int  Signo) {pthread_mutex_lock (&g_mut);    Switch (signo) {case sigterm:printf ("Debug:now received SIGTERM (\ n");    msg = "SIGTERM";  Break    Case sigquit:printf ("Debug:now received sigquit\n");    msg = "Sigquit";  Break    Case sigint:msg = "SIGINT";    printf ("Debug:now received sigint\n");  Break  default:printf ("Dbeug:can ' t identy this signal\n");}  Pthread_mutex_unlock (&g_mut); Pthread_cond_signal (&g_cond);}  int main (int argc, char *argv[]) {pthread_mutex_init (&g_mut, NULL);  Pthread_cond_init (&g_cond, NULL);    if (Signal (SIGTERM, sig_capture) = = Sig_err) {printf ("Debug:can ' t catch signal (SIGTERM) \ n");  Exit (100); } if (Signal (sigquit, sig_capture) = = Sig_err) {printf ("DebuG:can ' t catch signal (sigquit) \ n ");  Exit (200);    } if (Signal (SIGINT, sig_capture) = = Sig_err) {printf ("Debug:can ' t catch signal (SIGINT) \ n");  Exit (300);  } pthread_mutex_lock (&g_mut);    while (Msg.empty ()) {printf ("Debug:now wait signal...\n");  Pthread_cond_wait (&g_cond, &g_mut);  } pthread_mutex_unlock (&g_mut);  printf ("Debug:now string:%s\n", Msg.c_str ()); return 0;}










































Advanced Programming for the UNIX environment-10. Signals-Reading notes

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.