|Download Help (Windows Only)|
This topic explains how the LabVIEW Real-Time Module handles priorities and execution systems when creating and scheduling threads. By understanding the priority-based scheduling model, you can leverage the strengths of the LabVIEW Real-Time Module to create deterministic applications.
LabVIEW is a structured dataflow language that abstracts many of the underlying complexities of computer operation to create an intuitive graphical programming environment. To understand how LabVIEW schedules parallel tasks based on priority, it helps to define some key concepts that underlie the scheduling process in LabVIEW:
The following illustration summarizes the priorities and execution systems available in LabVIEW.
The preceding illustration shows the following facts:
LabVIEW implements priority inheritance to prevent unintended priority reductions. If a subVI is configured with a lower priority than that of the calling VI and both run within the same execution system, the subVI inherits the priority of the calling VI. If a subVI is configured with a priority higher than that of the calling VI, the subVI maintains its configured priority.
|Note If a subVI runs in a different execution system than the calling VI, priority inheritance does not occur.|
The real-time operating system (RTOS) also implements priority inheritance. When a lower-priority thread holds a shared resource needed by a higher-priority thread, the RTOS temporarily increases the priority of the thread holding the shared resource so that the resource can return promptly to the higher-priority thread.
There are two types of time-critical threads:
|Note National Instruments recommends creating no more than one time-critical task per CPU.|
LabVIEW uses two separate but related priority schemes: VI priorities and timed structure priorities. Timed structure priorities are numeric, and a higher value represents a higher priority relative to other timed structures executing on the target. However, all timed structure priorities fall between the high and time-critical VI priority levels.
|Note (Real-Time Linux) Do not use more than 32 timed structure priority levels in a VI. LabVIEW returns an error if you attempt to set more than 32 timed structure priority levels.|
National Instruments recommends using only one priority scheme in your application, as shown in the following examples:
Using a single priority scheme makes the application easier to understand and less error prone.
Scheduling is the process of determining which task to run at a given time. Scheduling is a key task for any modern operating system, but especially for a real-time operating system (RTOS). In addition to threads spawned by your VIs, an RT target runs numerous OS and driver threads at various priorities. To ensure that your tasks execute according to your timing requirements, it helps to understand how LabVIEW Real-Time schedules threads.
The following rules summarize the operation of the LabVIEW Real-Time scheduler when a new thread enters the run queue:
At any given time, each thread in the system is either running or blocked:
When a thread begins executing, it runs until one of the following conditions arises:
When a thread becomes unblocked, it enters the run queue. The run queue is always sorted in priority order, so when a thread enters this queue, it immediately moves ahead of all lower-priority threads. If a thread entering the run queue is higher-priority than a currently-running thread, the higher-priority thread either runs on a different CPU core or interrupts the currently-running thread in a process called preemptive scheduling.
Preemptive scheduling is the process of ordering thread execution based on the relative priorities of the threads. When a thread of higher priority than the current thread enters the run queue, the scheduler interrupts the current thread if necessary so that the higher-priority thread can execute immediately. The interrupted thread then returns to the run queue behind the higher-priority thread.
Round robin scheduling is the process of alternating between threads such that each thread receives roughly equivalent CPU time. During round robin scheduling, the scheduler interrupts and switches between the threads at regular intervals, keeping track of the CPU time given to each thread.
The LabVIEW Real-Time Module performs round robin scheduling among threads of equal priority. However, the LabVIEW Real-Time Module does not perform round-robin scheduling among VIs set to time-critical priority.
Although it seems intuitive to set the priority of each loop based on the perceived importance of the loop, this strategy can cause jitter. Set the priority of each loop not based on how critical the task is within the application but rather based on how important it is that the task fulfill a certain timing guarantee. For example, in a data logging application with a data acquisition loop and a logging loop, you might be tempted to make the logging loop the higher-priority loop. However, data acquisition must occur at regular intervals to avoid missed data points. However, it does not matter when you log each data point to disk, as long as you log each point eventually. In this case, you should assign a higher priority to the data acquisition loop, even though data logging is the primary purpose of the application.