0% found this document useful (0 votes)
48 views3 pages

Struct Int: #Include #Include

The document describes a Round Robin CPU scheduling algorithm. It defines a process struct containing PID, burst time, wait time, turnaround time and other attributes. It gets input for number of processes and their burst times. A time slice is also input. It then calculates total execution time, executes processes in rounds of time slice duration, and prints the Gantt chart. It finally calculates average wait and turnaround times.

Uploaded by

Ardra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views3 pages

Struct Int: #Include #Include

The document describes a Round Robin CPU scheduling algorithm. It defines a process struct containing PID, burst time, wait time, turnaround time and other attributes. It gets input for number of processes and their burst times. A time slice is also input. It then calculates total execution time, executes processes in rounds of time slice duration, and prints the Gantt chart. It finally calculates average wait and turnaround times.

Uploaded by

Ardra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include <stdlib.h>

struct process { int pid, bursttime , wtime , ttime ,remain ,ftime ,round;}

/*
initialize variable in array process
pid --> process id
bursttime --> burst time
wtime --> wait time
ttime --> turnaround time
remain --> to save the remain of burst time after the number of rounds
*/

p[10] ;

/* assuming all processes the arrive time of them zero then change according if burst
time equal or less or greater than the timeslice */

int main()
{
int i,pno,totwtime,totttime,timeslice ;
double avgW ,avgT;
int time=0;//declare variable of type int to compare it with the total time
int totaltime=0;//declare variable of type int to save the total time

system("clear");

printf(" \n ******* Round Robin Scheduling ******* \n");//print type of scheduling

printf("\n please enter the number of processes (max 9)\t");/*get the number of
processes from user and save it in 'pno'*/
scanf("%d",&pno);

if (pno>9)//check if the number of processes is less than or equal to 9


{
printf("\n the number of processes is greater than 9 \n");
exit(-1);
}

for(i=1;i<=pno;i++)//get the burst time of each process


{
p[i].pid=i;
printf("\n please enter the burst time of p[%d]\t",i);
scanf("%d",&p[i].bursttime);
p[i].remain=p[i].bursttime;
}

printf("\n please enter the Time slice: ");


scanf("%d",&timeslice);

for (i=pno;i>=0;i=i-1)/* to calculate the total needed time to


execute all processes */
{
totaltime=p[i].bursttime+totaltime;
}
printf("\n\t GANT Chart \n");
printf("\n \t | p[i] \t |time \t |round");
printf("\n \t ---------------------------------");
2

for (i=1 ; i<=pno ;i++)/* execute all processes just for a specific period
equal to timeslice & print the GANT Chart*/

{
p[i].round = 1;//initially all processes will enter the first round
p[i].wtime =time;//set the wait time to time
if (p[i].remain<=timeslice)/*when the process has a burst time
less than the timesilce*/
{
time=time+p[i].remain;
p[i].ftime = time;
p[i].remain = 0;
}

else /*when the process has burst time greater than the timeslice so is
will
execute only for a time equal to the timeslice and go back to the queue */
{
time = time+timeslice;
p[i].ftime = time;
p[i].remain=p[i].remain-timeslice;
}
printf("\n \t | p[%d] \t | %d \t | %d ", i ,p[i].ftime , p[i].round );
printf("\n \t ---------------------------------");
}

i=1;
while (time<totaltime)/*check if there is a remain burst time and execute
that process & continue the Gant chart*/
{
for (i=1 ; i<=pno ;i++)
{
if (p[i].remain!=0)
{
p[i].round = p[i].round+1;/*since the process do not finish from the first
round so it will enter again and increase round */
p[i].wtime = (time - p[i].ftime )+ p[i].wtime;
if (p[i].remain<=timeslice)
{
time=time+p[i].remain;
p[i].ftime = time;
p[i].remain= 0;
}

else
{
time = time+timeslice;
p[i].ftime = time;
p[i].remain=p[i].remain-timeslice;
}
printf("\n \t | p[%d] \t | %d \t | %d ", i ,p[i].ftime , p[i].round );
printf("\n \t ---------------------------------");
}
}
}

printf("\n\n\n\t p[i] \t bursttime \t wtime \t ttime");


printf("\n\t ====================================================");
3

totwtime=totttime=0;//declare the total wait time and the

for (i=1;i<=pno;i++)//calculate ttime & totwtime & totttime


{
p[i].ttime = p[i].wtime + p[i].bursttime;
totwtime=p[i].wtime+totwtime;
totttime=p[i].ttime+totttime;
printf("\n\t p[%d] \t\t%d \t\t%d \t\t%d",p[i].pid,
p[i].bursttime,p[i].wtime,p[i].ttime );
}

avgW=((double)totwtime/pno);//calculate the average wait time


avgT=((double)totttime/pno);//calculate the average turnaround time

printf("\n \n \n \t avgW=%f \t avgT=%f \t\n",avgW,avgT);//print the average


wait time
}
/*
******* Round Robin Scheduling *******

please enter the number of processes (max 9) 3

please enter the burst time of p[1] 4

please enter the burst time of p[2] 2

please enter the burst time of p[3] 5

please enter the Time slice: 2

GANT Chart

| p[i] |time |round


---------------------------------
| p[1] | 2 | 1
---------------------------------
| p[2] | 4 | 1
---------------------------------
| p[3] | 6 | 1
---------------------------------
| p[1] | 8 | 2
---------------------------------
| p[3] | 10 | 2
---------------------------------
| p[3] | 11 | 3
---------------------------------

p[i] bursttime wtime ttime


====================================================
p[1] 4 4 8
p[2] 2 2 4
p[3] 5 6 11

avgW=4.000000 avgT=7.666667
*/

You might also like