We want to manage a set of processes in a computer system. Each process has a unique id and a priority. It is obvious that the processes having highest priority will be executed first. The processes with equal priority will be executed in FIFO. The maximum priority in the system is a constant $\texttt{N}$: The highest priority is therefore $\texttt{N-1}$, the lowest is 0. Assume that the execution of a process consists of printing his id.

Such system will be represented by the following figure :

 

 

#define N 5
#define M 10
#include<stdio.h>
typedef int element;
typedef element process;
typedef struct
{
      element data[M]; /* queue content */
      int front, rear;
} queue;
typedef queue systemQ[N];

 

 

Write the following functions allowing to:

  1. Create the previously described structure;
  2. Display the content of the structure;
  3. Add a process;
  4. Delete the process with the highest priority ;
  5. Calculate the number of processes for a given priority ;
  6. Modify the priority of processes of priority i to priority j.

Difficulty level
Video recording
This exercise is mostly suitable for students
#include<stdio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 10
#define M 10


typedef int element;
typedef element process;
typedef struct
{
	element data[M]; /* queue content */
	int front, rear;
} queue;

typedef queue systemQ[N];

queue CreateQueue()
{
	queue q;
	q.front = 1; q.rear = 0;
	return q;
}


int isEmptyQueue(queue q)
{
	return (q.front == (q.rear + 1) % N);

}

int isFullQueue(queue q)
{
	return (q.front == (q.rear + 2) % N);
}

int EnQueue(queue *q, element e)
{
	if (isFullQueue(*q)) return 0;
	q->rear = (q->rear + 1) % N;
	q->data[q->rear] = e;
	return 1;
}

int DeQueue(queue *q)
{
	if (isEmptyQueue(*q)) return 0;
	q->front = (q->front + 1) % N;
	return 1;
}

int Front(queue q, element *e)
{
	if (isEmptyQueue(q)) return 0;
	*e = q.data[q.front];
	return 1;
}



void create_system(systemQ t)
{
	int i;
	for (i = 0; i<N; i++)
		t[i] = CreateQueue();
}

void display(systemQ t)
{
	process p;
	int i;
	queue q;

	for (i = N - 1; i >= 0; i--)
	{
		if (!isEmptyQueue(t[i]))
		{
			q = CreateQueue();
			while (!isEmptyQueue(t[i]))
			{
				Front(t[i], &p);
				printf("%d ", p);
				DeQueue(&t[i]);
				EnQueue(&q, p);
			}
			t[i] = q;
		}
	}
}

int add(systemQ t, process e, element priority)
{
	if (priority >= N)
		return 0;
	EnQueue(&t[priority], e);
	return 1;
}

int deletePri(systemQ t)
{
	int i;
	for (i = N - 1; i >= 0; i--)
		if (!isEmptyQueue(t[i]))
		{
			DeQueue(&t[i]);
			return 1;
		}
	return 0;
}

int nb_elements(systemQ t, element e)
{
	int n = 0;
	queue  Q;
	if (e<N)
	{
		Q = t[e];
		while (!isEmptyQueue(Q))
		{
			n++;
			Q.front++;
		}
		return n;
	}
	return 0;
}

void exchange(element p1, element p2, systemQ t)
{
	element e;
	if (p1<N && p2<N)
		while (Front(t[p1], &e))
		{
			EnQueue(&t[p2], e);
			DeQueue(&t[p1]);
		}
}


int main()
{
	int tests, choice;
	char str[100];
	systemQ t;
	process p;
	element e, n;
	int i;

	// read nb of tests
	scanf("%d", &tests);
	create_system(t);
	// each line: an infix expression 
	while (tests--)
	{
		scanf("%d", &choice);
		switch (choice)
		{
		case 1: display(t); printf("\n");
			break;
		case 2:
			scanf("%d", &p);
			scanf("%d", &e);
			if (!add(t, p, e))
				printf("Add failed\n");
			break;
		case 3:
			if (!deletePri(t))
				printf("Delete failed\n");
			break;
		case 4:// read priority
			scanf("%d", &e);
			printf("%d\n", nb_elements(t, e));
			break;
		case 5:// reading 2 priorities
			scanf("%d %d", &e, &n);
			exchange(e, n, t);
			break;
		}

	}
	return 0;
}


/*
void main()
{
systemQ t;
process p;
element e, n;
int choice, i;

create_system(t);

printf("\n**Automatically Filling %d process having %d priorities**\n", M, N);
srand((unsigned)time(NULL));
for (i = 0; i<M; i++)
{
n = rand() % N;
p = rand();
EnQueue(&t[n], p);
}

while (1)
{
printf("\nGive\tfor\n  1\tdisplay the content of the system\n  2\tadd a process\n  3\tdelete the process havng the greatest priority\n  4\tGive the number of processes with priority i \n  5\tExhcnage processes of priority i and j\n  0\tquit\n");
scanf("%d", &choice);

if (choice == 0)
break;
switch (choice)
{
case 1:printf("\n**Displaying the processes**\n");
display(t);
break;
case 2:printf("\n**Add a process**\n");
printf("Give a process number: ");
scanf("%d", &p);
printf("Give a priority: ");
scanf("%d", &e);
if (add(t, p, e))
printf("**Add succeeded**\n");
else
printf("**!!Add Failed **\n**ERROR: Verify the priority**\n");
break;
case 3:printf("\n**Delete the process with the highest priority**\n");
if (deletePri(t))
printf("**Delete succeeded**\n");
else
printf("**!!Delete Failed**\n**ERROR: Empty System**\n");
break;
case 4:printf("\n**Processes having priority i**\n");
printf("Give a priority: ");
scanf("%d", &e);
printf("Number of processes having priority %d is %d\n", e, nb_elements(t[e]));
break;
case 5:printf("\n**Exchange proceesses i and j**\n");
printf("Give priority i: ");
scanf("%d", &e);
printf("Give priority j: ");
scanf("%d", &n);
exchange(e, n, t);
printf("**Exchange terminated\n");
break;
}
}
}
*/

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Series Sum 2