Last active
October 13, 2022 16:12
-
-
Save ayush-0101/e8837607a5844813491f4e8163a4ee77 to your computer and use it in GitHub Desktop.
A simple simulation of Round Robin CPU Scheduling Algorithm in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> | |
struct process | |
{ | |
int pid,arr_t,exec_t,rem_t,end_t; | |
}; | |
typedef struct process P; | |
int front = -1; | |
int rear = -1; | |
int size = -1; | |
int *bsadd = NULL; | |
void swap(P *ptr1,P *ptr2) | |
{ | |
int t; | |
t = ptr1->pid; | |
ptr1->pid = ptr2->pid; | |
ptr2->pid = t; | |
t = ptr1->exec_t; | |
ptr1->exec_t = ptr2->exec_t; | |
ptr2->exec_t = t; | |
ptr1->rem_t = ptr1->exec_t; | |
ptr2->rem_t = ptr2->exec_t; | |
t = ptr1->arr_t; | |
ptr1->arr_t = ptr2->arr_t; | |
ptr2->arr_t = t; | |
} | |
void push_back(int val,int *rearp) | |
{ | |
if(front==-1 && rear==-1){ | |
front = rear = 0; | |
*bsadd = val; | |
} | |
else if(rear==(size-1)){ | |
rear = 0; | |
*bsadd = val; | |
} | |
else{ | |
rear++; | |
rearp++; | |
*rearp = val; | |
} | |
} | |
int pop_front(int *frontp) | |
{ | |
if(front==-1 && rear==-1){ | |
return -1; | |
} | |
else{ | |
int val = *frontp; | |
if(front==rear){ | |
front = rear = -1; | |
*frontp = -1; | |
} | |
else if(front==(size-1)){ | |
front = 0; | |
*frontp = -1; | |
} | |
else{ | |
front++; | |
*frontp = -1; | |
} | |
return val; | |
} | |
} | |
int main() | |
{ | |
int n,slice,i; | |
printf("Enter the total no. of processes: "); | |
scanf("%d",&n); | |
printf("Enter the time slice value: "); | |
scanf("%d",&slice); | |
struct process procs[n]; | |
printf("Now enter process id, burst time and arrival time of the processes respectively:\n"); | |
for(i=0;i<n;i++){ | |
scanf("%d %d %d",&procs[i].pid,&procs[i].exec_t,&procs[i].arr_t); | |
procs[i].rem_t = procs[i].exec_t; | |
} | |
// Sorting... | |
for(i=1;i<n;i++) | |
for(int j=0;j<=n-i-1;j++) | |
if(procs[j].arr_t>procs[j+1].arr_t) | |
swap(&procs[j],&procs[j+1]); | |
// Store PID(s)... | |
int point[n+1]; | |
for(i=0;i<n;i++){ | |
point[procs[i].pid] = i; | |
} | |
// RR scheduling... | |
int queue[1000]; | |
size = 1000; | |
bsadd = &queue[0]; | |
int time = procs[0].arr_t,process_id=0,all_entered=0,count=0,ppsh,pending_push=0,sum_tt=0,sum_wt=0; | |
push_back(procs[0].pid,(rear!=-1)?&queue[rear]:NULL); | |
while(1){ | |
// Collecting processes by their arrival times... | |
if(!all_entered){ | |
while(1){ | |
if(process_id==n-1){ | |
all_entered= 1; | |
break; | |
} | |
else if(procs[process_id+1].arr_t<=time){ | |
process_id++; | |
push_back(procs[process_id].pid,(rear!=-1)?&queue[rear]:NULL); | |
} | |
else | |
break; | |
} | |
} | |
// Pushing incompletely ran process... | |
if(pending_push){ | |
push_back(ppsh,(rear!=-1)?&queue[rear]:NULL); | |
pending_push = 0; | |
} | |
// Running... | |
if(front!=-1){ | |
int indx = point[queue[front]]; | |
int rt = procs[indx].rem_t; | |
if(rt<=slice){ | |
time += rt; | |
procs[indx].rem_t = 0; | |
procs[indx].end_t = time; | |
pop_front((front!=-1)?&queue[front]:NULL); | |
count++; | |
} | |
else{ | |
procs[indx].rem_t -= slice; | |
time += slice; | |
ppsh = queue[front]; | |
pop_front((front!=-1)?&queue[front]:NULL); | |
pending_push = 1; | |
} | |
} | |
if(count==n) | |
break; | |
} | |
// Answer... | |
for(i=0;i<n;i++){ | |
int ct= procs[i].end_t, tt= ct-procs[i].arr_t, wt= tt-procs[i].exec_t; | |
sum_tt+= tt; | |
sum_wt+= wt; | |
printf("Process %d :-\n Completion time = %d , TA time = %d , Waiting time = %d\n",procs[i].pid,ct,tt,wt); | |
} | |
printf("\nAverage Turnaround time = %.3f\nAverage Waiting time = %.3f\n",(float)sum_tt/n,(float)sum_wt/n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment