Talking about reentrant and non-reentrant functions, talking about Functions

Source: Internet
Author: User

Talking about reentrant and non-reentrant functions, talking about Functions

In the real-time system design, multiple tasks often call the same function. If a function is unfortunately designed to be like this: when different tasks call this function, the data of other tasks calling this function may be modified, resulting in unpredictable consequences.Such a function is an insecure function. It is also called a non-reentrant function..


On the contrary, there must be a secure function, which is also called a reentrant function. So what is a reentrant function? Reentrant refers to a process that can be called by multiple tasks. When a task is called, you do not have to worry about data errors.


A reentrant function is simply a function that can be interrupted. That is to say, it can be interrupted at any time during function execution and transferred to the OS for scheduling to execute another piece of code, the return control function does not produce any errors. The non-reentrant function uses some system resources, such as the global variable area and the interrupt vector table. If it is interrupted, problems may occur. Such functions cannot run in multi-task environments.
  

It can also be understood that re-entry indicates repeated entryFirst, it means that this function can be interrupted. Second, it means that it does not depend on any environment (including static) except the variables on its own stack. Such a function is purecode (pure code) reentrant: multiple copies of the function are allowed to run. Since they use isolated stacks, they do not interfere with each other. If you really need to access global variables (including static), be sure to implement mutex. Reentrant functions are very important in parallel runtime environments, but they generally cost some performance for accessing global variables.


When writing reentrant functions, if global variables are used, they should be protected by means of clearance interruption and semaphores (namely, P and V Operations.


Note: If the global variables used are not protected, this function is not reentrant. That is, when multiple processes call this function, it is very likely that the related global variables are changed to an unknown state.


Example: If Exam is an int type global variable, the function Squre_Exam returns the Exam square value. The following functions are not reentrant.

int Exam = 0;unsigned int example( int para ) {     unsigned int temp;    Exam = para; // (**)    temp = Square_Exam( );    return temp;}

If the function is called by multiple processes, the result may be unknown, because after the (**) Statement is executed, another process that uses this function may be activated. When a newly activated process executes this function, Exam is assigned a different para value, so when the control returns "temp = Square_Exam ()", the calculated temp may not be the expected result. This function should be improved as follows.

Int Exam = 0; unsigned int example (int para) {unsigned int temp; [apply for semaphore operation] // (1) Lock Exam = para; temp = Square_Exam (); [release semaphores] // unlock return temp ;}

If the application fails, the other process is in the process of assigning a value to Exam and calculating its square (that is, the signal is being used). The process must wait for the signal to be released, to continue. If the signal is applied for, you can continue to execute, but other processes must wait for this process to release the semaphore before using this signal.


Methods To ensure the reusability of functions:

1) Try to use local variables (such as variables in registers and stacks) when writing functions );

2) The global variables to be used should be protected (for example, the cross-exclusion method such as Guanzhong disconnection and semaphores). This function must be a reentrant function.


Most functions that meet the following conditions cannot be reentrant (Insecure:

1) The function uses a static data structure;

2) the malloc () or free () function is called in the function body;

3) standard I/O functions are called in the function body.


How can I rewrite a non-reentrant function into a reentrant function? The only way to turn a non-reentrant function into a reentrant is to rewrite it using the reentrant rule. In fact, it is very simple. As long as there are several rules that are easy to understand, the written functions are reentrant:

1) do not use global variables. Because other code may change these variable values.

2) when interacting with the hardware, remember to perform operations like disinterrupt (), that is, disable hardware interruption. When interaction is completed, remember to open the interrupt. In some series, this is called "Enter/exit core ".

3) You cannot call any other function that cannot be reentrant.

4) use the stack with caution.

Common reentrant functions in Linux



References: http://www.embeddedlinux.org.cn

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.