Main Content

Task Preemption in a Multitasking Processor

This example shows how to force service completion in an Entity Server block using functionality available on the block Preemption tab.

Example Model for Task Preemption

The example shows preemption(replacement) of low priority tasks by a high priority task in a multitasking processor. In the model, the Entity Server block represents the task processor presented with a capacity to process multiple concurrent tasks.

Model Behavior and Results

The following graphic shows how the model generates both low and high priority tasks.

Snapshot of part of a Simulink model that shows two Entity Generator blocks connected to an Entity Input Switch block with three input ports. The Entity Input Switch block connects to an Entity Queue block that stores entities by priority. The Entity Queue block, in turn, connects to an Entity Server block whose output loops back to the third input port of the Entity Input Switch block. The model also contains a Simulink Function block with function interface, t = getCurrentTime().

  • The top and bottom Entity Generator randomly generate entities that represent high and low priority tasks, respectively. Both blocks use the exprnd function to generate random entities. The top block uses exprnd(3), the bottom uses exprnd(1), which requires the Statistics and Machine Learning Toolbox™ license.

  • The Entity Input Switch block merges the paths of the new low priority tasks with previously preempted tasks that are returning from the task processor (server).

  • The Simulink Function block runs the getCurrentTime function to start a timer on the low priority tasks. When preemption occurs, a downstream Simulink Function block determines the remaining service time of the preempted tasks.

  • The Entity Output Switch block merges the paths of the high and low priority tasks. Tasks on the merged path proceed for processing.

An Entity Server block represents a multitasking processor with capacity for multiple tasks.

Snapshot of part of the same Simulink model that shows a second output port of the Entity Server block connecting to an Entity Output Switch block. The two output ports of the Entity Output Switch block connect to two Entity Terminator blocks. The model also contains two Simulink Function blocks, Simulink Function1 and Simulink Function2. Simulink Function1 has the function interface, recordPreferredWaitTimes(starttime) and Simulink Function2 has the function interface, recordNonPreferredWaitTimes(starttime).

When preemption occurs, causing the Entity Server block to complete immediately service of all low priority tasks, one of the two Simulink Function blocks calculates the elapsed time of each departing task using the recordPreferredWaitTimes and recordNonPreferredWaitTimes functions. The two Entity Terminator blocks calls these Simulink Function to calculate the elapsed times.

If the elapsed time of a departing task is less than the service time of the Entity Server block, meaning that preemption forced the task to depart the server early, the Output Switch block feeds the task back to reenter the server. If the elapsed time in the Simulink Function getCurrentTime block is equal to the service time of the Entity Server block, the server has completed the full service time on the task. The entity terminates in the Entity Terminator block.

The Dashboard Scope block shows the simulation results.

The Dashboard Scope graphically represents an increasing wait time for low priority tasks, and a uniform, lower wait time for high priority tasks.

The plot displays wait time for high an low priority tasks. It can be observed that wait time of high priority tasks is significantly decreased.

See Also

|

Related Examples

More About