Operating system-reentrant and non-reentrant functions

Source: Internet
Author: User

Reentrant function is mainly used in a multitasking environment, which is simply a function that can be interrupted, that is, at any moment of execution of the function interrupt it, go to the OS Scheduler to execute another piece of code, return control without error It also means that it does not depend on any environment (including static) other than the variables on its own stack, such functions as Purecode (pure code) can be re-entered, allowing multiple copies of the function to run, because they use a separate stack, so they do not interfere with each other. The non-reentrant function can cause problems if it is interrupted because of the use of some system resources, such as the global variable area, the interrupt vector table, etc., which cannot be run in a multitasking environment.

reentrant functions do require access to global variables (including static), so be sure to implement mutually exclusive means. It is important in a parallel runtime environment, but it is generally necessary to pay some performance cost to access global variables. When you write a reentrant function, if you use a global variable, you should protect it by turning off interrupts, semaphores (that is, p, v operations), and so on. If the global variable being used is not protected, then this function is not reentrant, that is, when multiple processes call this function, it is likely that the global variable becomes unknowable.

Example: Suppose exam is a global variable of type int, the function squre_exam returns the exam squared value. Then the following function does not have reentrant nature.

    1. int Exam = 0;
    2. unsigned int example ( int para)
    3. {
    4. unsigned int temp;
    5. Exam = para; //(* *)
    6. temp = Square_exam ();
    7. return temp;
    8. }


If this function is called by more than one process, the result may be unknown, because when the (*) statement has just been executed, another process using this function may be just activated, then when the newly activated process executes to this function, the exam will be assigned a different para value, so when the control returns to the "Temp = Square_exam () ", the computed temp is probably not the expected result. This function should be improved as follows.

    1. int Exam = 0;
    2. unsigned int example ( int para)
    3. {
    4. unsigned int temp;
    5. [Request semaphore Operation] //(1) Locking
    6. Exam = para;
    7. temp = Square_exam ();
    8. [Release semaphore operation] //Unlock
    9. return temp;
    10. }

The request is not "semaphore", stating that another 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 it to release the signal before it can continue execution. If a signal is requested, it can continue, but the other process must wait for the signal to be released by the process before it can be used again. A method to guarantee the reentrant nature of a function:
1. Try to use local variables (such as registers, variables in the stack) when writing functions;

2, for the global variables to be used to protect (such as the use of off-interrupt, semaphore, etc.), so that the composition of the function must be a reentrant function.
Most of the functions that meet the following conditions are non-reentrant:
1, the function body uses the static data structure;
2. The function body calls the malloc () or free () function;
3, the function body calls the standard I/O functions.

How do you rewrite a non-reentrant function into a reentrant function? The only way to turn a non-reentrant function into reentrant is to rewrite it with reentrant rules. In fact it is very simple, as long as you follow a few very easy to understand the rules, then the written function is reentrant:
1. Do not use global variables. Because other code is likely to overwrite these variable values.
2), when interacting with the hardware, remember to perform operations like Disinterrupt (), that is, to turn off the hardware interrupt. Complete the interaction remember to open the interrupt, on some series, this is called "Enter/Exit Core".
3. You cannot call any other function that is not reentrant.
4, use the stack with caution. It is best to os_enter_kernal first before use.

Operating system-reentrant and non-reentrant functions

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.