[Apue] Chapter 10 Signal

Source: Internet
Author: User

Signal Concept

There is no signal numbered 0.

Signal generation method:

1. When a user presses a terminal key, the terminal generates a signal.

2. Hardware exceptions generate signals, such as SIGSEGV signals.

3. A process can call the kill function to send signals to another process or process group.

4. When a certain condition is detected, it should also generate a signal when it notifies the relevant process. For example, sigpipe signal.

The application can process the signals in three ways.

1. Ignore the signal

2 system default

3. Install the signal processing function to enable the signal processing function to process the function.

 

Kill-l can view the signal numbers in the system

Ubuntu Signal Set

 

root@LeoK:~/APUE/8_test# kill -l 1)SIGHUP      2) SIGINT        3) SIGQUIT     4) SIGILL         5)SIGTRAP 6)SIGABRT    7) SIGBUS       8) SIGFPE        9) SIGKILL       10)SIGUSR111) SIGSEGV     12)SIGUSR2     13) SIGPIPE      14) SIGALRM   15) SIGTERM16) SIGSTKFLT  17) SIGCHLD    18) SIGCONT    19) SIGSTOP     20)SIGTSTP21) SIGTTIN      22)SIGTTOU    23) SIGURG      24) SIGXCPU    25) SIGXFSZ26) SIGVTALRM        27) SIGPROF     28)SIGWINCH 29) SIGIO 30) SIGPWR31) SIGSYS        34)SIGRTMIN 35) SIGRTMIN+1      36) SIGRTMIN+2      37) SIGRTMIN+338) SIGRTMIN+4      39) SIGRTMIN+5      40)SIGRTMIN+6      41) SIGRTMIN+7      42) SIGRTMIN+843) SIGRTMIN+9      44) SIGRTMIN+10    45)SIGRTMIN+11    46) SIGRTMIN+12    47) SIGRTMIN+1348) SIGRTMIN+14    49) SIGRTMIN+15    50)SIGRTMAX-14    51) SIGRTMAX-13    52) SIGRTMAX-1253) SIGRTMAX-11    54) SIGRTMAX-10    55)SIGRTMAX-9      56) SIGRTMAX-8      57) SIGRTMAX-758) SIGRTMAX-6      59) SIGRTMAX-5      60)SIGRTMAX-4      61) SIGRTMAX-3      62) SIGRTMAX-263) SIGRTMAX-1      64) SIGRTMAX

 

In the following cases

1. The process sets the user ID, and the current user is not the owner of the program file.

2. The process sets the group ID, and the current user is not the owner of the program file.

3. the user has no permission to write to the current user.

4. The file is too large.

 

Sigpipe signal generation

 

TIPS:

All the signal processing functions that call the execl sub-process will be washed out, making the signal processing function a default action.

When a process calls fork, its child process inherits the signal processing method of the parent process.

 

 

Signal signal installation Function

 

typedef void sig_dispose(int sig_no);sig_dispose signal(sig_dispose func1);

 

The function address can specify the following macro

 

#ifndef __ASSEMBLY__typedef void __signalfn_t(int);typedef __signalfn_t *__sighandler_t;typedef void __restorefn_t(void);typedef __restorefn_t *__sigrestore_t;#define SIG_DFL      ((__sighandler_t)0) /* default signal handling */#define SIG_IGN      ((__sighandler_t)1) /* ignore signal */#define SIG_ERR      ((__sighandler_t)-1)         /* error return from signal */#endif

 

Unreliable signal (this blog mainly describes the difference between reliable and unreliable signals)

 

Interrupt System Call:

When a low-speed system call is being executed, the process will capture the signal, interrupt the low-speed system call, and set errno to eintr to indicate that the signal is interrupted.

Reentrant function

The reason for the non-reentrant function is that the API uses a Global static data structure. When each process calls the function, it will write a global data structure, or the API calls malloc or free internally to modify the linked list.

 

Reliable signal terms and Semantics

During the interval between signal generation and delivery, the signal is called pending.

Each process has a signal shielding word, which specifies the signal set to be blocked and delivered to the process. For each possible signal, the shielding word corresponds to one, for a signal, if its corresponding bit has been set, it is currently blocked.

 

 

int kill(pid_t pid, int signo)int raise(int signo);

 

PID is

> 0 sends signals to the process PID

<0 sends a signal to a process whose process is | PID |

= 0 sends a signal to each process in the called process group. You have the permission to send signals to these processes.

=-1 send signals to all processes. You must have the permission.

The permission defines that the actual or valid user ID of the sender must be the actual or valid user ID of the recipient.

 

Alarm and pause new haipi

 

unsigned int  alarm(unsigned int seconds);

 

Sends a sigalrm signal to a process. The default action is to terminate the process.

When setting the alarm time, if another alarm time is set by the previous process, alarm returns the remaining values.

If seconds is 0, the previous alarm is canceled.

 

 

int pause()

 

The pause function allows the calling process to suspend and capture a signal. Pause will only return a signal when a signal processing program is executed and returned from it.

 

Signal Set

Sigset_t indicates the Signal Set

Function

 

int sigemptyset(sigset_t *set);int sigfillset(sigset_t* set);int sigaddset(sigset_t *set, int signo);int sigdeleteset(sigset_t* set, int signo);int sigismember(const sigset_t* set, intsigno):

 

Set signal shielding characters

 

int sigprocmask(int how, constsigset_t  *set, sigset_t  *oset);

 

How: used to specify the signal modification method. Three methods may be selected.
Sig_block // Add a signal to process blocking.
Sig_unblock // remove the signal from the Process Shield.
Sig_setmask // set the set value to a new process mask.

 

View pending signals in the current process

Int sigpending (sigset_t * Set) to obtain the signal set types pending for the current process in the set.

 

The sigaction function is used to check or modify the processing actions associated with the specified signal (two operations can be performed at the same time ).

It is a POSIX signal interface, while signal () is a standard C signal interface (this interface should be used if the program must run on a non-POSIX System)

Set a new signal processing function act for the signal SIGNUM, and retain the original signal processing function oldact of the signal.

Int sigaction (INT signo, const struct sigaction * restrict act,

Struct sigaction * restrict oact );

The structure sigaction is defined as follows:

Struct sigaction {
Void (* sa_handler) (INT );
Sigset_t sa_mask;
Int sa_flag;
Void (* sa_sigaction) (INT, siginfo_t *, void *);
};

The sa_handler field contains the address of a Signal capture function.

The sa_mask field specifies a signal set. before calling the signal capturing function, this signal set must be added to the process's signal shielding characters. Only when the function is returned from the signal capture function, the signal shielding character of the process is reset to the original value.

Sa_flag is an option, mainly understanding two

Sa_interrupt the system calls that are interrupted by the signal will not be restarted automatically.
Sa_restart the system calls that are interrupted by the signal will be automatically restarted.

Sa_siginfo provides additional information, a pointer to the siginfo structure and a pointer to the process context identifier

 

 

Sigsetjmp and siglongjmp

1. prototype:

 

#include <setjmp.h>int sigsetjmp(sigjmp_buf env, int savemask);

If the call is direct, 0 is returned. If the call is returned from siglongjmp, a non-0 value is returned.
Void siglongjmp (sigjmp_buf ENV, int Val );

 

It can be seen that sigsetjmp has a parameter savemask more than setjmp. If it is not 0, sigsetjmp saves the current signal shielding word of the Process in env.

 

Sigsuspend Function

 

      #include <signal.h>       int sigsuspend(const sigset_t *sigmask)

 

That is to say,After sigsuspend, the process hangs there, waiting for the open signal to wake up. After receiving the signal, the system immediately restores the current signal set to the original one, and then calls the processing function.

 

The void abot () function sends a SIGABRT signal to the process.

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.