-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsjf.c
72 lines (66 loc) · 1.98 KB
/
sjf.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
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include "headers.h"
/*
* Processes are enqueued in the Shortest Job First manner
* Processes are inserted in the increasing order of their burst times
* The process with the shortest job is pointed to by q->front
* The process with the longest job is pointed to by q->rear
*/
void enqueueSJF(Queue *q, int processID, int burstTime)
{
// create new queue node with priority = 0
node *newNode = newnode(processID, burstTime, 0);
// if queue is empty
if (q->rear == NULL)
{
q->rear = q->front = newNode;
}
else
{
// if the burst time of the new process is less than that of the process currently in the front of the queue
if (newNode->burstTime <= q->front->burstTime)
{
if (newNode->burstTime == q->front->burstTime)
{
newNode->next = q->front->next;
q->front->next = newNode;
}
else
{
newNode->next = q->front;
q->front = newNode;
}
}
// if the burst time of the new process is greater than that of the process currently in the end of the queue
else if (newNode->burstTime > q->rear->burstTime)
{
q->rear->next = newNode;
q->rear = q->rear->next;
}
else
{
node *ptr = q->front, *prev = NULL;
while (ptr->next != NULL && newNode->burstTime > ptr->burstTime)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newNode;
newNode->next = ptr;
}
}
}
void sjf(int numberOfProcesses, int *processIDs, int *burstTimes)
{
printf("Shortest Job First:\n\n");
Queue *q = createQueue();
for (int i = 0; i < numberOfProcesses; i++)
{
enqueueSJF(q, processIDs[i], burstTimes[i]);
}
calculateWaitTime(q);
calculateTurnAroundTime(q);
showQueue(q);
deleteQueue(q);
}