can be re-entry and non-reentrant program distinction

Source: Internet
Author: User

reentrant English keyword for reentrancy, here first to distinguish the concept can be preempted. In the computer, the operation of the program can be said to be a bunch of machine instructions are put into the CPU sequence execution, but the operating system in order to better manage the program, there are a variety of carrier concepts, such as processes, threads, tasks, the essence, are relative to the "dispatch" this operation, they are only the granularity of the scheduling is different. If we run a program under Linux or Windows, it relies on the process or the task carrier, which is dispatched by the operating system's kernel. A preemptive description of the program (Task) You are currently executing can be preempted by other programs, which means that you temporarily lose ownership of the CPU resources.

Once the scheduling is involved, the tasks need to be synchronized and mutually exclusive, such as the common trigger scheduling techniques such as interrupts, task sleep, kernel preemption and so on.

The example I'm holding here is a reentrant function, which is the basis for re-entry, and other concepts or applications are built on it. Reentrant functions can be interrupted at any time, and can be run after a period of time, and the corresponding data is not lost. The Reentrant function uses only local variables, that is, the variables are stored in the CPU registers or stacks.

1     void strcpy (charChar *src)  2    {  3          while (*dest++ = *src++) {  4             (void)0;   5        }  6         *dest = NUL;   7     }  

strcpy () is a reentrant function, and after multiple tasks or threads call it, it does not bring up problems such as synchronization.

If a function uses a variable that involves a global variable or a heap, such a function might be a non-reentrant function, and it might be possible to solve the problem of resource mutex. In the embedded research and development, the problem of non-reentrant can be solved by means of switch interruption, task priority adjustment, signal volume, spin lock and so on. As a common example, a function that uses global variables is not necessarily non-reentrant, for example, the global variable is already assigned and read-only.

To give a practical example, the inside of a function is not all code is not reentrant, if there is a considerable part of the code is not involved in the resource mutex, this time can be locked to unlock the way to synchronize tasks, to achieve a logical sense of reentrant. Of course, the logic of the program has to be the designer's own control, otherwise prone to multiple tasks to invoke the same piece of code, who can not get execution, resulting in a deadlock situation.

Summary: Reentrant is an important concept in multi-tasking and multi-threaded development, especially at the beginning of design. In addition, for many system calls or third-party function libraries, the module can be re-entered should be careful verification.

can be re-entry and non-reentrant program distinction

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.