-
Notifications
You must be signed in to change notification settings - Fork 0
/
prioritybased.c
65 lines (58 loc) · 1.82 KB
/
prioritybased.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include "headers.h"
/*
* Processes are enqueued based on the priorities assigned to them
* Processes are inserted in the decreasing order of their priorities
* The process with the highest priority is pointed to by q->front
* The process with the lowest priority is pointed to by q->rear
*/
void enqueuePriority(Queue *q, int processID, int burstTime, int priority)
{
// create new queue node
node *newNode = newnode(processID, burstTime, priority);
// if queue is empty
if (q->rear == NULL)
{
q->rear = q->front = newNode;
}
else
{
// if the priority of the new process is greater than that of the process currently in the front of the queue
if (newNode->priority > q->front->priority)
{
newNode->next = q->front;
q->front = newNode;
}
// if the priority of the new process is less than that of the process currently in the end of the queue
else if (newNode->priority <= q->rear->priority)
{
q->rear->next = newNode;
q->rear = q->rear->next;
}
else
{
node *ptr = q->front, *prev = NULL;
while (ptr->next != NULL && newNode->priority < ptr->priority)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newNode;
newNode->next = ptr;
}
}
}
void priority(int numberOfProcesses, int *processIDs, int *burstTimes, int *priorities)
{
printf("Priority Based Scheduling:\n\n");
Queue *q = createQueue();
for (int i = 0; i < numberOfProcesses; i++)
{
enqueuePriority(q, processIDs[i], burstTimes[i], priorities[i]);
}
calculateWaitTime(q);
calculateTurnAroundTime(q);
showQueue(q);
deleteQueue(q);
}