Asynchronous signal Security (reentrant) and thread security

Source: Internet
Author: User

Looking for a job is not good. I will review the thread and signal sections of apue (the third edition) during the 11th holiday. I will finish the review in two days. The sections 10.6 and 12.5 in the book are the re-entry introduction of signals and threads. However, it is hard to fully understand the concept of asynchronous signal security, thread security, and reentrant. Write down this article.

Reentrant

Reusability (Reentrant)For functionsIt has two meanings:

1. Concurrent/concurrent access: When a function is called by a task, other tasks can call the function at the same time without generating an error; it is also called the effect produced by the execution of the reentrant function under the same input, and it is not different because of its concurrent calls,Also known as concurrency Security.

2. Re-entry after interruption: the re-entry function can be interrupted at any time. When the interrupted execution is completed, the returned breakpoint can continue to run correctly; or the results produced by the function execution that can be reloaded with the same input are not different because of the interrupted calls during function execution,Also known as security interruption.

Reentrant)Functions can be used concurrently by more than one task without worrying about data errors. On the contrary,Non-reentrant)A function cannot be shared by more than one task, unless it can ensure that the function is mutually exclusive (or use a semaphore, orCodeTo disable the interruption ). The reentrant function can be interrupted at any time and run later without data loss. The reentrant function either uses local variables or protects its own data when using global variables.

Reentrant functions generally meet the following requirements:

    • Do not hold static data for continuous calls.
    • No pointer to static data is returned. All data is provided by the function caller.
    • Use local data or make local copies of global data to protect global data.
    • Never call any non-reentrant function.

For any of the following reasons, other functions cannot be reentrant:

    • They calledMallocOrFree.
    • We all know that they use static data structures.
    • They are standard I/OProgramA part of the database.

Thread Security

Thread Security (MT-safe)Not only for FunctionsIt mainly refers to the result of data or program security, so there are different levels of thread security: such as thread-safe functions and thread-safe libraries. This article also introduces the thread security class concept. Generally, a thread-safe function refers to a function with the first meaning of A reentrant function, that is, a function with concurrent Security. However, it should be noted that, even if a function is not secure at the function level, if the insecure factor does not exist in a specific application, this function is also thread-safe for the application. For example, for access to global variables, untitled synchronous access is certainly not thread-safe, however, if all accesses that may occur simultaneously are read-only, the results are thread-safe.

Do not confuse reentrant with thread security. In the programmer's opinion, this is two independent concepts: functions can be reentrant, thread-safe, or both, or both. Non-reentrant functions cannot be used by multiple threads. In addition, it may be impossible for a function to be reentrant to be thread-safe.Whether the standard Io library is thread-safe depends on the actual situation.Different Libraries have different implementations. Generally, to make it easier for users, thread-safe libraries are provided in systems that support multithreading, but they are not unsafe, because,The signal must beDifferencesStep, not the thread locking mechanism (essentially synchronous) can solve.

Asynchronous signal Security

Essentially, the Soft Interrupt of a signal is asynchronous in nature. The so-called asynchronous signal security, like thread security, is also considered in the result. It refers to the security of the signal interrupt processing process. Generally, the previous asynchronous signal security function is a function that can be called in the asynchronous information processing function without exception. You also need to note that even if a function at the function level is not asynchronous information security, if it is called in the information processing function, it may not necessarily produce unsafe results.

Similarities and Differences

For the security of a multi-threaded program, thread security and asynchronous signal security are usually included. At the function level, we can find that the reentrant function must be a thread-safe function and an asynchronous signal-safe function. The multi-thread security function is much weaker, it is not necessarily a reentrant function. It only requires no errors in concurrency. Although asynchronous Signal Functions and reentrant functions have different descriptions, they are completely consistent in terms of implementation, orThe two concepts of the reentrant function and the asynchronous signal security function are equivalent..

 

For instance instructions, refer to "using reentrant functions for safer signal processing".

 

 

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.