Task Preemption in a Multitasking Processor

This example shows how to force service completion in an N-Server block using functionality available on the block Service control tab. For more information, see Service control Tab.

The example shows preemption—replacement—of low priority tasks by a high priority task in a multitasking processor. The task processor is represented by an N-Server block with a capacity to process N concurrent tasks.

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

  • The top Time-Based Entity Generator block randomly generates entities that represent high priority tasks. The bottom generator block generates a low priority task every 1 second.

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

  • The Start Timer block starts a timer on the low priority tasks. When preemption occurs, a downstream Read Timer block determines the remaining service time of the preempted tasks.

  • The second Path Combiner block merges the paths of the high and low priority tasks. Tasks on the merged path proceed for processing.

An N-Server block represents a multitasking processor with capacity for N tasks.

The N-Server block accepts both low and high priority tasks— entities—from the merged path. Whenever the top generator produces a high priority task, the pe signal value of the generator block (which connects to the complete port of the N-Server block) changes from 0 to 1. The N-Server block immediately completes service of low priority tasks that exist in the block. At the same time instant, the high priority task enters the N-Server block, replacing—preempting— the original low priority tasks. The high priority task enters the first available server in the N-Server block. New low priority tasks occupy the remaining servers. For more information about using the complete port of the N-Server block, see Service control Tab.

When preemption occurs, causing the N-Server block to immediately complete service of all low priority tasks, a Read Timer block calculates the elapsed time of each departing task. A MATLAB Function block compares this elapsed time with the service time of the N-Server block.

If the elapsed time of a departing task is less than the service time of the N-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 Read Timer block is equal to the service time of the N-Server block, the server has completed the full service time on the task. The entity terminates in the Entity Sink block.

The scope plots show the simulation results.

The top plot shows the times when the first generator block produces high priority tasks. The next three plots show the occupancy status of each server in the N-Server block. When the first generator block generates a new high priority task, each server immediately completes service of its current task. In each server, the occupancy status, so, changes from 1 to 0. At the same time instant, the high priority task occupies the first available server. New low priority tasks occupy the remaining servers. The occupancy status of each server changes from 0 to 1.

Was this topic helpful?