Linux system programming (23)-signal blocking, linux23

Source: Internet
Author: User
Tags sigint signal

Linux system programming (23)-signal blocking, linux23


The processing action of the actually executed signal is called the Delivery, the State between the signal generation and the Delivery, and the Pending ). A process can Block a signal. When a blocked signal is generated, it remains in the pending state until the process unblocks the signal. Note that blocking and ignoring are different. As long as the signal is blocked, it will not be delivered. Ignoring is an optional action after delivery. The representation of signals in the kernel can be viewed as follows:

 

 

Each signal has two flags indicating blocking and pending, and a function pointer indicating processing actions. When the signal is generated, the kernel sets the pending flag of the signal in the process control block until the signal is delivered. In the example,

The SIGHUP signal is neither congested nor generated. When it is delivered, it performs the default processing action.

The SIGINT signal is generated but is being blocked. Therefore, it cannot be delivered for the time being. Although the process is ignored, the signal cannot be ignored before the process is blocked, because the process still has the opportunity to change the process and then lift the blocking.

The SIGQUIT signal has never been generated. Once the SIGQUIT signal is generated, it will be blocked. The processing action is the User-Defined Function sighandler.

 

What if the signal is generated multiple times before the process blocks a signal? POSIX.1 allows the system to deliver this signal once or multiple times. In Linux, conventional signals are generated multiple times before delivery, and real-time signals are generated multiple times before delivery, which can be placed in one queue in turn. This chapter does not discuss real-time signals. From the point of view, each signal has only one bit pending sign. If it is not 0, it is 1. It does not record how many times the signal has been generated, and the blocking sign is also expressed in this way. Therefore, the pending and blocking signs can be stored using the same data type sigset_t. sigset_t is called a signal set. This type can indicate the "valid" or "invalid" status of each signal, in a blocked signal set, "valid" and "invalid" indicate whether the signal is blocked, in the pending signal set, "valid" and "invalid" indicate whether the signal is in the pending status. The next section describes the operations of the signal set in detail. The blocked Signal set is also called the Signal Mask of the current process. The "blocking" here should be understood as blocking rather than ignoring it.

 

 

Signal Set operation functions

The sigset_t type indicates a "valid" or "invalid" state for each type of signal with a bit. As for how to store these bits in this type, it depends on the system implementation, users do not have to worry about it. Users can only call the following functions to operate the sigset_t variable, instead of interpreting its internal data, for example, printing the sigset_t variable directly with printf is meaningless.

 

#include <signal.h> int sigemptyset(sigset_t *set);int sigfillset(sigset_t *set);int sigaddset(sigset_t *set, int signo);int sigdelset(sigset_t *set, int signo);int sigismember(const sigset_t *set, intsigno);

The sigemptyset function initializes the signal set pointed to by the set, so that the corresponding bits of all signals are cleared, indicating that the signal set does not contain any valid signal. The sigfillset function initializes the signal set pointed to by the set, so that the corresponding bit of all signals is set, indicating that the valid signal of the set includes all signals supported by the system. Note: Before using a variable of the sigset_t type, you must call sigemptyset or sigfillset for initialization so that the signal set is in a definite state. After the sigset_t variable is initialized, you can add or delete a valid signal in the signal set by calling sigaddset and sigdelset. All the four functions return 0 for success and-1 for error. Sigismember is a Boolean function used to determine whether a valid signal of a Signal Set contains a certain signal. if it contains a certain signal, 1 is returned. If it does not contain a certain signal, 0 is returned. If an error is returned,-1 is returned.

 

Each process has a signal set to describe which signals will be blocked when delivered to the process. All signals in the signal set will be blocked after being delivered to the process. The following are functions related to signal blocking:

 

#include <signal.h>int sigprocmask(int  how,  const sigset_t *set, sigset_t *oldset));int sigpending(sigset_t *set));int sigsuspend(const sigset_t *mask));

 

Sigprocmask Function

Call the sigprocmask function to read or change the signal shielding characters of a process.

 

# Include <signal. h>

 

Int sigprocmask (int how, const sigset_t * set, sigset_t * oset); Return Value: 0 if successful,-1 if an error occurs

 

If oset is a non-null pointer, the current signal shielding word of the read process is passed out through the oset parameter. If set is a non-null pointer, the signal shielding character of the process is changed. The parameter "how" indicates how to change it. If both oset and set are non-null pointers, back up the original signal shielding word to oset, and then change the signal shielding word according to the set and how parameters.

 

The sigprocmask () function can operate the signal set based on the parameter how. There are three main operations:

Parameter how process Current Signal Set

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 is lifted.

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

 

 

If sigprocmask is called to remove the congestion on several pending signals, at least one signal is delivered before sigprocmask returns.

 

Sigpending Function

#include <signal.h> int sigpending(sigset_t *set);


Sigpending reads the pending Signal set of the current process and transmits it through the set parameter. If the call is successful, 0 is returned, and-1 is returned if an error occurs.

Sigsuspend (const sigset_t * mask) is used to temporarily replace the signal mask of the process with the mask before receiving a signal, and pause the process until the signal is received. 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.

Struct itimerval:

           struct itimerval {                struct timeval it_interval; /*next value */                struct timeval it_value;    /* current value */           };           struct timeval {                long tv_sec;                /* seconds */                long tv_usec;               /* microseconds */           };


Descriptive description of the second parameter in the three-parameter signal processing function:

Siginfo_t {
Int si_signo;/* signal value, which is meaningful to all signals */
Int si_errno;/* errno value, meaning all signals */
Int si_code;/* cause of signal generation, meaning to all signals */
Pid_t si_pid;/* process ID of the sent signal, which is meaningful to kill (2), real-time signal, and SIGCHLD */
Uid_t si_uid;/* real User ID of the sending signal process, which is meaningful to kill (2), real-time signal, and SIGCHLD */
Int si_status;/* exit status, meaning SIGCHLD */
Clock_t si_utime;/* Time consumed by the user, which is meaningful to SIGCHLD */
Clock_t si_stime;/* Time consumed by the kernel, Which is meaningful to SIGCHLD */
Sigval_t si_value;/* signal value, which is meaningful to all real-time data structures,
/* It can be an integer (indicated by si_int or a pointer, indicated by si_ptr )*/
Void * si_addr;/* the memory address that triggers the fault, which is meaningful to SIGILL, SIGFPE, SIGSEGV, and SIGBUS signals */
Int si_band;/* Meaning SIGPOLL signal */
Int si_fd;/* Meaning SIGPOLL signal */
}


In fact, apart from the first three elements, other elements are organized in a consortium structure. In the consortium data structure, they are organized into different structures based on different signals. The meaning of a signal mentioned in the comment refers to the ability to access these domains in the signal processing function to obtain meaningful information related to the signal, however, a specific signal is only interested in specific information.

The following is an example:

#include <signal.h>#include <stdio.h> void printsigset(const sigset_t *set){         inti;         for(i = 1; i < 32; i++)                   if(sigismember(set, i) == 1)                            putchar('1');                   else                            putchar('0');         puts("");} int main(void){         sigset_ts, p;         sigemptyset(&s);         sigaddset(&s,SIGINT);         sigprocmask(SIG_BLOCK,&s, NULL);         while(1) {                   sigpending(&p);                   printsigset(&p);                   sleep(1);         }         return0;}


When the program runs, the pending status of each signal is printed every second. Because we have blocked the SIGINT signal, pressing Ctrl-C will make the SIGINT signal in the pending status, you can still terminate the program by pressing Ctrl-\ because the SIGQUIT signal is not blocked.

 

 

 

 

 

 

 


Programming for linux

Suggestion:
1. Taking Redhat Enterprise Linux 5 (RHEL5) as an example, the system supports multiple programming languages, mainly because you are used to that programming language. However, I personally recommend that you use C/C ++ for programming;
2. recommended books:
2.1. "Linux system and Network Service Management Technology Daquan (second edition)" Yang Minghua Tan Li compiled a CD containing 79 yuan from the Electronic Industry Press;
2.2. "Linux system management and network management" Yu boshan compiled a CD containing RMB 89 from Tsinghua University Press.
After reading these two books, it is basically enough.
Suggestion:
1. It is best to first install vmwarevm in the system, and then install Redhat Enterprise Linux 5 and other Linux systems in the virtual machine;
2. perform various tests in the Linux System of the virtual machine, so that even if there is a problem, it does not matter.

In linux shell programming, how does one view all the signal information in the system?

What signal do you want to view? Is it the basic information of the system? You can use uname to view system information:
$ Uname-
Linux qunero-Ubuntu-desktop 3.0.0-12-generic # 20-Ubuntu SMP Fri Oct 7 14:50:42 UTC 2011 i686 i686 i386 GNU/Linux

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.