Non-Preemptive Priority Scheduling is a scheduling algorithm that selects the tasks to execute based on priority.
More...
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
|
void | insert (node **root, int id, int at, int bt, int prior) |
| To insert a new process in the queue.
|
|
void | delete (node **root, int id) |
|
void | show_list (node *head) |
| To show the process queue.
|
|
int | l_length (node **root) |
| To length process queue.
|
|
void | update (node **root, int id, int ct, int wt, int tat) |
| To update the completion time, turn around time and waiting time of the processes.
|
|
bool | compare (node *a, node *b) |
| To compare the priority of two processes based on their arrival time and priority.
|
|
float | calculate_ct (node **root) |
| To calculate the average completion time of all the processes.
|
|
float | calculate_tat (node **root) |
| To calculate the average turn around time of all the processes.
|
|
float | calculate_wt (node **root) |
| To calculate the average waiting time of all the processes.
|
|
static void | test () |
| Self-test implementations.
|
|
int | main () |
| Main function.
|
|
Non-Preemptive Priority Scheduling is a scheduling algorithm that selects the tasks to execute based on priority.
In this algorithm, processes are executed according to their priority. The process with the highest priority is to be executed first and so on. In this algorithm, a variable is maintained known as the time quantum. The length of the time quantum is decided by the user. The process which is being executed is interrupted after the expiration of the time quantum and the next process with the highest priority is executed. This cycle of interrupting the process after every time quantum and resuming the next process with the highest priority continues until all the processes have been executed.
- Author
- Aryan Raj
◆ node
for assert
for boolean data type for IO operations (printf
) for memory allocation eg: malloc
, realloc
, free
, exit
Structure to represent a process
◆ calculate_ct()
float calculate_ct |
( |
node ** |
root | ) |
|
To calculate the average completion time of all the processes.
- Parameters
-
root | pointer to the head of the queue |
- Returns
- float average completion time
216{
217
218 node *ptr = *root, *prior, *rpt;
219 int ct = 0, i, time = 0;
221 float avg, sum = 0;
222 node *duproot = NULL;
223
224 while (ptr != NULL)
225 {
228 }
229 ptr = duproot;
231
232 while (rpt != NULL)
233 {
235 {
236 ptr = rpt;
237 }
239 }
240
241 ct = ptr->
AT + ptr->
BT;
242 time = ct;
243 sum += ct;
244
245
246 update(root, ptr->
ID, ct, 0, 0);
247 delete (&duproot, ptr->
ID);
248
249 for (i = 0; i < n - 1; i++)
250 {
251 ptr = duproot;
252 while (ptr != NULL && ptr->
AT > time)
253 {
255 }
257 while (rpt != NULL)
258 {
259 if (rpt->AT <= time)
260 {
262 {
263 ptr = rpt;
264 }
265 }
267 }
270 sum += ct;
271 update(root, ptr->
ID, ct, 0, 0);
272 delete (&duproot, ptr->
ID);
273 }
274 avg = sum / n;
275 return avg;
276}
bool compare(node *a, node *b)
To compare the priority of two processes based on their arrival time and priority.
Definition non_preemptive_priority_scheduling.c:199
int l_length(node **root)
To length process queue.
Definition non_preemptive_priority_scheduling.c:129
void insert(node **root, int id, int at, int bt, int prior)
To insert a new process in the queue.
Definition non_preemptive_priority_scheduling.c:48
Node, the basic data structure in the tree.
Definition binary_search_tree.c:15
struct node * next
List pointers.
Definition bfs.c:24
int priority
Priority of the process node.
Definition non_preemptive_priority_scheduling.c:32
int AT
Arrival Time of the process node.
Definition non_preemptive_priority_scheduling.c:30
int BT
Burst Time of the process node.
Definition non_preemptive_priority_scheduling.c:31
int ID
ID of the process node.
Definition non_preemptive_priority_scheduling.c:29
◆ calculate_tat()
float calculate_tat |
( |
node ** |
root | ) |
|
To calculate the average turn around time of all the processes.
- Parameters
-
root | pointer to the head of the queue |
- Returns
- float average turn around time
283{
284 float avg, sum = 0;
287
289 {
291 }
292
293 while (ptr != NULL)
294 {
298 }
299 avg = sum / n;
300 return avg;
301}
float calculate_ct(node **root)
To calculate the average completion time of all the processes.
Definition non_preemptive_priority_scheduling.c:215
int TAT
Turn Around Time of the process node.
Definition non_preemptive_priority_scheduling.c:35
int CT
Completion Time of the process node.
Definition non_preemptive_priority_scheduling.c:33
◆ calculate_wt()
float calculate_wt |
( |
node ** |
root | ) |
|
To calculate the average waiting time of all the processes.
- Parameters
-
root | pointer to the head of the queue |
- Returns
- float average waiting time
308{
309 float avg, sum = 0;
312
314 {
316 }
317
318 while (ptr != NULL)
319 {
320 ptr->
WT = (ptr->
TAT - ptr->
BT);
323 }
324 avg = sum / n;
325 return avg;
326}
int WT
Waiting Time of the process node.
Definition non_preemptive_priority_scheduling.c:34
◆ compare()
To compare the priority of two processes based on their arrival time and priority.
- Parameters
-
a | pointer to the first process |
b | pointer to the second process |
- Returns
- true if the priority of the first process is greater than the the second process
-
false if the priority of the first process is NOT greater than the second process
200{
202 {
204 }
205 else
206 {
207 return a->AT < b->
AT;
208 }
209}
◆ delete()
void delete |
( |
node ** |
root, |
|
|
int |
id |
|
) |
| |
82{
83 node *ptr = *root, *prev;
84
85 if (ptr == NULL)
86 {
87 return;
88 }
89
91 {
94 return;
95 }
96
97 while (ptr != NULL && ptr->
ID !=
id)
98 {
99 prev = ptr;
101 }
102 if (ptr == NULL)
103 {
104 return;
105 }
108}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
◆ insert()
void insert |
( |
node ** |
root, |
|
|
int |
id, |
|
|
int |
at, |
|
|
int |
bt, |
|
|
int |
prior |
|
) |
| |
To insert a new process in the queue.
- Parameters
-
root | pointer to the head of the queue |
id | process ID |
at | arrival time |
bt | burst time |
prior | priority of the process |
- Returns
- void
49{
50
53 new->ID = id;
54 new->AT = at;
55 new->BT = bt;
56 new->priority = prior;
57 new->next = NULL;
58 new->CT = 0;
59 new->WT = 0;
60 new->TAT = 0;
61
62 if (*root == NULL)
63 {
64 *root = new;
65 return;
66 }
67
68 while (ptr->
next != NULL)
69 {
71 }
73 return;
74}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
◆ l_length()
int l_length |
( |
node ** |
root | ) |
|
To length process queue.
- Parameters
-
root | pointer to the head of the queue |
- Returns
- int total length of the queue
130{
131 int count = 0;
133 while (ptr != NULL)
134 {
135 count++;
137 }
138 return count;
139}
◆ main()
Main function.
- Returns
- 0 on exit
365{
367
368 return 0;
369}
static void test()
Self-test implementations.
Definition non_preemptive_priority_scheduling.c:332
◆ show_list()
void show_list |
( |
node * |
head | ) |
|
To show the process queue.
- Parameters
-
head | pointer to the head of the queue |
- Returns
- void
115{
116 printf("Process Priority AT BT CT TAT WT \n");
117 while (head != NULL)
118 {
119 printf(
"P%d. %d %d %d %d %d %d \n", head->
ID, head->
priority, head->
AT,
120 head->
BT, head->
CT, head->
TAT, head->
WT);
122 }
123}
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Self-test implementations.
- Returns
- void
333{
334
335
336
337
338
339
340
341
343 insert(&root, 1, 0, 5, 1);
344 insert(&root, 2, 1, 4, 2);
345 insert(&root, 3, 2, 3, 3);
346 insert(&root, 4, 3, 2, 4);
347 insert(&root, 5, 4, 1, 5);
351 assert(avgCT == 11);
352 assert(avgTAT == 9);
353 assert(avgWT == 6);
354 printf("[+] All tests have successfully passed!\n");
355
356
357
358}
float calculate_tat(node **root)
To calculate the average turn around time of all the processes.
Definition non_preemptive_priority_scheduling.c:282
float calculate_wt(node **root)
To calculate the average waiting time of all the processes.
Definition non_preemptive_priority_scheduling.c:307
◆ update()
void update |
( |
node ** |
root, |
|
|
int |
id, |
|
|
int |
ct, |
|
|
int |
wt, |
|
|
int |
tat |
|
) |
| |
To update the completion time, turn around time and waiting time of the processes.
- Parameters
-
root | pointer to the head of the queue |
id | process ID |
ct | current time |
wt | waiting time |
tat | turn around time |
- Returns
- void
151{
153
154 if (ptr != NULL && ptr->
ID ==
id)
155 {
156 if (ct != 0)
157 {
159 }
160 if (wt != 0)
161 {
163 }
164 if (tat != 0)
165 {
167 }
168 return;
169 }
170
171 while (ptr != NULL && ptr->
ID !=
id)
172 {
174 }
175 if (ct != 0)
176 {
178 }
179 if (wt != 0)
180 {
182 }
183 if (tat != 0)
184 {
186 }
187 return;
188}