APUE 3, APUE

Source: Internet
Author: User

APUE 3, APUE

Signals can be understood as software interruptions. It provides an asynchronous way to process events. Each signal has a corresponding signal name with a SIG prefix, such as SIGABRT and SIGALARM. The header file signal. h defines all the signal names whose values are Positive Integer constants. In fact, the Implementation defines individual signals in different header files, but these header files are included in signal. h; this is because the kernel cannot contain header files applied to user-level programs! Therefore, when a user program and the kernel need a definition of information at the same time, the general practice is to put this definition into the kernel header file and then include this kernel header file in the user header file.

Unix System Signal list:

When the default action in the signal list is "terminal + core", it means that the memory image of the process will remain in the core file under the process directory. Core files can help most UNIX system debugs check the State at the time of Process Termination. However, if a) the process has set-user-ID and the current user is not the owner of the process file; or B) the process has set-group-ID and the current user is not the user group owner of the process file; or c) the current user has no write permission; or d) this file already exists and the current user has no write permission; or e) if the file is too large, the core file will not be created.

 

Program startup

When a program is executed, all signal loading is either handled by default or ignored. Generally, signals are set as their default processing method unless the exec process is called to ignore this signal. Specifically, the exec function changes the status of the signal captured by the exec process to the default Processing Method of the signal, while retaining the Processing Methods of other signals, because the new program executed by exec does not contain the function address for capturing signals, these processing methods are meaningless in the new program.

 

Process Creation

When a process calls fork, the child process inherits the signal processing method of the parent process. Here, the child process starts with a copy of the memory image of the parent process, so the address of the signal capture function is meaningful.

 

Reentrant function

When a signal is captured by the signal processing function of a process, the normal command execution sequence of the process will be interrupted for a short time by the signal processing, after the signal is processed, the process continues from where it was interrupted. However, in the signal processing function, we cannot identify which step the process has executed when the signal is captured. What should we do if the process is allocating an additional memory from the heap through malloc when receiving the signal? Do we call malloc in the signal processing function? Or when we receive a signal, we are calling a function, such as getpwnam, which stores its return value in a static region, in this case, should we call the same function in the signal processing function? In the above malloc scenario, doing so will have disastrous consequences for the process, because malloc usually contains a chain table of all its allocated regions, maybe it was updating the chain table. In the getpwnam scenario, the returned value of getpwnam stored in the static zone is overwritten by the call results in the signal processing function!

Therefore, The Single UNIX Specification requires that function calls in The signal processing function must be safe and Reentrant functions can be re-imported ). These functions are called async-signal safe by The Single Unix Specification ). Apart from reentrant, they will block any signals that disrupt continuity during function operation. The list of asynchronous signal security functions is as follows:

Some functions are not called asynchronous signal security for the following reasons:

Most of the implementations of standard I/O libraries use static data structures, none of which are reentrant functions. Note that even if we use the asynchronous signal security function in the signal processing function, each thread only has one errno variable (in a multi-threaded environment, multiple threads share the process address space. Each thread needs its own errno copy to prevent mutual interference between threads), and we may potentially modify the errno value. Therefore, there is a general rule that errno should be stored before calling the asynchronous signal security function in the signal processing function.

 

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.