Queue Selection Using a Switch

The model below directs entities to the shortest of three queues. It uses an Output Switch block to create the paths to the different queues. To implement the choice of the shortest queue, a Atomic Subsystem block called Findmin that contains a MATLAB Function block queries each queue for its current length. The MATLAB Function block determines which queue or queues achieve the minimum length, and provides that information to the Output Switch block. To ensure that the information is up to date when the Output Switch block attempts to output the arriving entity, the block uses the Store entity before switching option; for details, see Use the Storage Option to Prevent Latency Problems.

For simplicity, the model omits any further processing of the entities after they leave their respective queues.

Although the block diagram shows signals at the #n signal output ports from the queue blocks and another signal at the p signal input port of the Output Switch block, the block diagram does not indicate how to compute p from the set of #n values. That computation is performed inside the atomic subsystem that contains the MATLAB Function block.

Subsystem Contents

If you double-click the MATLAB Function block in a model, an editor window shows the MATLAB® function that specifies the block. In this example, the MATLAB function computes the index of a queue having the shortest length, where the individual queue lengths are n1, n2, and n3. If more than one queue achieves the minimum, then the computation returns the smallest index among the queues that minimize the length.

function p = findmin(n1, n2, n3)

% p is the index of a queue having the shortest length.
[~, p] = min([n1 n2 n3]);

    Note:   For visual simplicity, the model uses Goto and From blocks to connect the #n signals to the computation.

The figure below shows a sample plot. Each stem corresponds to an entity departing from the switch block via one of the three entity output ports.

Was this topic helpful?