Linux under C Programming: signal processing potential hazards

Source: Internet
Author: User

As a communication method of asynchronous process, signal is convenient and practical in practical application. However, it should also be noted that there is a potential danger in using the signal. Here is a brief introduction to several specific situations that may cause errors, and we hope to pay special attention to the actual signal processing.

Some system calls may be modified when a signal processing function is registered. Usually, they should be unaffected by the signal, but because a signal processing function is registered, the system may think it needs to interrupt the original system call when a signal arrives. When this occurs, the original system call is terminated, the call failure value is returned, and the errno is set to Eintr. There are times, of course, when you really want the system to handle this, in other cases, however, this is not desirable because the error code returned may cause the subsequent process to fail in the desired direction and may even result in the loss of the data because the call succeeds due to the failure to return the error in subsequent runs. Therefore, it is recommended that you use function sigaction to set the SA_RESTART flag to prevent this from happening.

Alternatively, there may be another signal generated when you perform a signal processing function that is set by the user. This may cause the first call to be interrupted and a second signal-processing function call. The execution of the first call is resumed when the second call is completed. This will affect the operation of the function, especially for programs that use static variables. In this case, you can call the function Sigaction set Sa_nodefer to block the second signal.

In addition, in a busy system, operations that involve system time may be different from the expected effect. For example, the function pause is called after the alarm call, and it is hoped that the pause state will end when the alarm set alarm occurs. However, because the system is too busy to allow this process to be divided into too little time, when the alarm occurs, function pause has not been called, then when the function pause is called will likely cause the process is forever suspended.

-----(above from "Linux Environment C Programming Guide")

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.