Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 please enter the number of processes (max 9)\t");/*get the number of
processes from user and save it in 'pno'*/
scanf("%d",&pno);
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 ---------------------------------");
}
}
}
GANT Chart
avgW=4.000000 avgT=7.666667
*/