MCS-208 · Unit 5

Queues

Section 4 of 8

Block 2 · Unit 5Queues7 min · Section 4 of 8

5.4Multiple Queues in One Array

Covers IGNOU MCS-208 §5.4

Operating systems keep several queues at once, for example one per priority level of processes. As with multiple stacks (Unit 4), these queues can share one array instead of each having its own.

Segments

An array mqueue[0..m-1] is divided into kk equal segments of n=m/kn = m/k cells; queue ii (numbered from 0) uses cells i⋅ni \cdot n to (i+1)⋅n−1(i + 1) \cdot n - 1. Each queue has its own indices:

  • front[i] = one position before the first element of queue ii;
  • rear[i] = position of the last element;
  • queue ii is empty when front[i] == rear[i] (initially both are i⋅n−1i \cdot n - 1);
  • queue ii is full when rear[i] reaches the last cell of its segment.

Add x to queue i: if rear[i] is at the end of the segment, report “full”; otherwise increase rear[i] once and store x there.

Delete from queue i: if front[i] == rear[i], report “empty”; otherwise increase front[i] and return mqueue[front[i]]. When the queue becomes empty, both indices can be reset to the start of the segment, so that the segment is reused.

Key takeaways

  • kk queues in mm cells: segment ii = cells i⋅ni \cdot n to (i+1)n−1(i + 1)n - 1 with n=m/kn = m/k.
  • front[i] is one before the first element, rear[i] the last; empty when they are equal.
  • Add: check full, rear[i]++ once, store. Delete: check empty, front[i]++, read.
  • Fixed segments may be full while the array still has free space.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

15 cells are shared equally by 3 queues (numbered 0–2). Which cells belong to queue 2?

Show answer

Answer: C. 10 to 14

Each queue gets 15 / 3 = 5 cells: queue 0 uses 0–4, queue 1 uses 5–9, queue 2 uses 10–14.

Question 2 · multiple choice

In the scheme above, front[i] is one position before the first element. When is queue ii empty?

Show answer

Answer: A. When front[i] == rear[i]

If front[i] is one before the first element and rear[i] is the last, they are equal only when there are no elements. rear[i] == limit[i] is the full test.

Question 3 · multiple choice

Which direction does front[i] move when an item is deleted from queue ii?

Show answer

Answer: D. Forwards (increases), towards rear[i]

The front of a queue follows the items towards the rear. Decreasing it, as Program 5.5 does, moves into another queue’s cells.

Question 4 · multiple choice

What does this program print?

C · q5-multi.c
#include <stdio.h>

int main(void)
{
    int mq[8], front[2] = {-1, 3}, rear[2] = {-1, 3};
    mq[++rear[0]] = 5;
    mq[++rear[1]] = 7;
    mq[++rear[1]] = 8;
    int x = mq[++front[1]];
    printf("%d rear0=%d rear1=%d front1=%d\n", x, rear[0], rear[1], front[1]);
    return 0;
}
Show answer

Answer: B. 7 rear0=0 rear1=5 front1=4

Queue 1 holds 7 (cell 4) and 8 (cell 5); deleting moves front[1] from 3 to 4 and returns 7, its first item. Queue 0 holds one item, so rear[0] is 0.

Question 5 · multiple choice

“It is not possible to implement multiple queues in an array.” This statement is:

Show answer

Answer: D. False; any number of queues can share an array using segments

Each queue gets its own segment with its own front and rear (Check Your Progress 2, Q2).

Question 6 · short answer

Write the algorithms to add an element to and delete an element from queue ii in a multiqueue array.

Show worked answer

Let queue ii occupy cells from start[i] to limit[i], with front[i] = rear[i] = start[i] - 1 initially. ADD(i, x): (1) if rear[i] == limit[i], print “queue i is full” and stop; (2) rear[i] = rear[i] + 1; (3) mqueue[rear[i]] = x. DELETE(i): (1) if front[i] == rear[i], print “queue i is empty” and stop; (2) front[i] = front[i] + 1; (3) x = mqueue[front[i]]; (4) if front[i] == rear[i], reset front[i] = rear[i] = start[i] - 1; (5) return x. Both are O(1)O(1).

↑ ↓ move↵ openTab switch type