Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
non_preemptive_priority_scheduling.c File Reference

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>
Include dependency graph for non_preemptive_priority_scheduling.c:

Data Structures

struct  node
 Node, the basic data structure in the tree. More...
 

Typedefs

typedef struct node node
 for assert
 

Functions

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.
 

Detailed Description

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

Typedef Documentation

◆ node

typedef struct node node

for assert

for boolean data type for IO operations (printf) for memory allocation eg: malloc, realloc, free, exit

Structure to represent a process

Function Documentation

◆ calculate_ct()

float calculate_ct ( node **  root)

To calculate the average completion time of all the processes.

Parameters
rootpointer to the head of the queue
Returns
float average completion time
216{
217 // calculate the total completion time of all the processes
218 node *ptr = *root, *prior, *rpt;
219 int ct = 0, i, time = 0;
220 int n = l_length(root);
221 float avg, sum = 0;
222 node *duproot = NULL;
223 // create a duplicate queue
224 while (ptr != NULL)
225 {
226 insert(&duproot, ptr->ID, ptr->AT, ptr->BT, ptr->priority);
227 ptr = ptr->next;
228 }
229 ptr = duproot;
230 rpt = ptr->next;
231 // sort the queue based on the arrival time and priority
232 while (rpt != NULL)
233 {
234 if (!compare(ptr, rpt))
235 {
236 ptr = rpt;
237 }
238 rpt = rpt->next;
239 }
240 // ptr is the process to be executed first.
241 ct = ptr->AT + ptr->BT;
242 time = ct;
243 sum += ct;
244 // update the completion time, turn around time and waiting time of the
245 // process
246 update(root, ptr->ID, ct, 0, 0);
247 delete (&duproot, ptr->ID);
248 // repeat the process until all the processes are executed
249 for (i = 0; i < n - 1; i++)
250 {
251 ptr = duproot;
252 while (ptr != NULL && ptr->AT > time)
253 {
254 ptr = ptr->next;
255 }
256 rpt = ptr->next;
257 while (rpt != NULL)
258 {
259 if (rpt->AT <= time)
260 {
261 if (rpt->priority < ptr->priority)
262 {
263 ptr = rpt;
264 }
265 }
266 rpt = rpt->next;
267 }
268 ct += ptr->BT;
269 time += ptr->BT;
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
Here is the call graph for this function:

◆ calculate_tat()

float calculate_tat ( node **  root)

To calculate the average turn around time of all the processes.

Parameters
rootpointer to the head of the queue
Returns
float average turn around time
283{
284 float avg, sum = 0;
285 int n = l_length(root);
286 node *ptr = *root;
287 // calculate the completion time if not already calculated
288 if (ptr->CT == 0)
289 {
290 calculate_ct(root);
291 }
292 // calculate the total turn around time of all the processes
293 while (ptr != NULL)
294 {
295 ptr->TAT = ptr->CT - ptr->AT;
296 sum += ptr->TAT;
297 ptr = ptr->next;
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
Here is the call graph for this function:

◆ calculate_wt()

float calculate_wt ( node **  root)

To calculate the average waiting time of all the processes.

Parameters
rootpointer to the head of the queue
Returns
float average waiting time
308{
309 float avg, sum = 0;
310 int n = l_length(root);
311 node *ptr = *root;
312 // calculate the completion if not already calculated
313 if (ptr->CT == 0)
314 {
315 calculate_ct(root);
316 }
317 // calculate the total waiting time of all the processes
318 while (ptr != NULL)
319 {
320 ptr->WT = (ptr->TAT - ptr->BT);
321 sum += ptr->WT;
322 ptr = ptr->next;
323 }
324 avg = sum / n;
325 return avg;
326}
int WT
Waiting Time of the process node.
Definition non_preemptive_priority_scheduling.c:34
Here is the call graph for this function:

◆ compare()

bool compare ( node a,
node b 
)

To compare the priority of two processes based on their arrival time and priority.

Parameters
apointer to the first process
bpointer 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{
201 if (a->AT == b->AT)
202 {
203 return a->priority < b->priority;
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 // if the root is null, return
85 if (ptr == NULL)
86 {
87 return;
88 }
89 // if the root is the process to be deleted, make the next node the root
90 if (ptr->ID == id)
91 {
92 *root = ptr->next;
93 free(ptr);
94 return;
95 }
96 // else traverse the queue and delete the process
97 while (ptr != NULL && ptr->ID != id)
98 {
99 prev = ptr;
100 ptr = ptr->next;
101 }
102 if (ptr == NULL)
103 {
104 return;
105 }
106 prev->next = ptr->next;
107 free(ptr);
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
rootpointer to the head of the queue
idprocess ID
atarrival time
btburst time
priorpriority of the process
Returns
void
49{
50 // create a new node and initialize it
51 node *new = (node *)malloc(sizeof(node));
52 node *ptr = *root;
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 // if the root is null, make the new node the root
62 if (*root == NULL)
63 {
64 *root = new;
65 return;
66 }
67 // else traverse to the end of the queue and insert the new node there
68 while (ptr->next != NULL)
69 {
70 ptr = ptr->next;
71 }
72 ptr->next = new;
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
rootpointer to the head of the queue
Returns
int total length of the queue
130{
131 int count = 0;
132 node *ptr = *root;
133 while (ptr != NULL)
134 {
135 count++;
136 ptr = ptr->next;
137 }
138 return count;
139}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
365{
366 test(); // run self-test implementations
367
368 return 0;
369}
static void test()
Self-test implementations.
Definition non_preemptive_priority_scheduling.c:332
Here is the call graph for this function:

◆ show_list()

void show_list ( node head)

To show the process queue.

Parameters
headpointer 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);
121 head = head->next;
122 }
123}

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
333{
334 // Entered processes
335 // printf("ID Priority Arrival Time Burst Time \n");
336 // printf("1 0 5 1 \n");
337 // printf("2 1 4 2 \n");
338 // printf("3 2 3 3 \n");
339 // printf("4 3 2 4 \n");
340 // printf("5 4 1 5 \n");
341
342 node *root = NULL;
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);
348 float avgCT = calculate_ct(&root);
349 float avgTAT = calculate_tat(&root);
350 float avgWT = calculate_wt(&root);
351 assert(avgCT == 11);
352 assert(avgTAT == 9);
353 assert(avgWT == 6);
354 printf("[+] All tests have successfully passed!\n");
355 // printf("Average Completion Time is : %f \n", calculate_ct(&root));
356 // printf("Average Turn Around Time is : %f \n", calculate_tat(&root));
357 // printf("Average Waiting Time is : %f \n", calculate_wt(&root));
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
Here is the call graph for this function:

◆ 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
rootpointer to the head of the queue
idprocess ID
ctcurrent time
wtwaiting time
tatturn around time
Returns
void
151{
152 node *ptr = *root;
153 // If process to be updated is head node
154 if (ptr != NULL && ptr->ID == id)
155 {
156 if (ct != 0)
157 {
158 ptr->CT = ct;
159 }
160 if (wt != 0)
161 {
162 ptr->WT = wt;
163 }
164 if (tat != 0)
165 {
166 ptr->TAT = tat;
167 }
168 return;
169 }
170 // else traverse the queue and update the values
171 while (ptr != NULL && ptr->ID != id)
172 {
173 ptr = ptr->next;
174 }
175 if (ct != 0)
176 {
177 ptr->CT = ct;
178 }
179 if (wt != 0)
180 {
181 ptr->WT = wt;
182 }
183 if (tat != 0)
184 {
185 ptr->TAT = tat;
186 }
187 return;
188}