C-Source Code for CPU Scheduling
#include<stdio.h>
struct pro
{
char name[10];
int a_time;
int b_time;
int w_time;
int t_time;
int f_time;
int prio;
int flag;
}pro[10],temp;
int i,j,n;
void get()
{
printf("Enter no of process");
scanf("%d",&n);
printf("\nProcess :-");
for(i=0;i<n;i++)
{
printf("\n Process Name");
scanf("%s",&pro[i].name);
printf("\n Enter arrival time :");
scanf("%d",&pro[i].a_time);
printf("\n Enter burst time");
scanf("%d",&pro[i].b_time);
}
}
void fcfs()
{
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pro[i].a_time>pro[j].a_time)
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
int t_tot=0,t_wait=0;
float a_tot=0,a_wait=0;
//pro[0].t_time=pro[0].b_time-pro[0].a_time;
//pro[0].w_time=0;
pro[0].f_time=pro[0].b_time;
for(i=1;i<n;i++)
{
pro[i].f_time=pro[i-1].f_time+pro[i].b_time;
}
for(i=0;i<n;i++)
{
pro[i].t_time=pro[i].f_time-pro[i].a_time;
pro[i].w_time=pro[i].t_time-pro[i].b_time;
t_tot=t_tot+pro[i].t_time;
t_wait=t_wait+pro[i].w_time;
}
a_tot=float(t_tot)/n;
a_wait=float(t_wait)/n;
printf("\n Avg tot=%.4f avg waiting time=%.4f",a_tot,a_wait);
}
void prio()
{
int tt=0,tw=0;
float at=0,aw=0;
printf("\nEnter priority for processes ");
for(i=0;i<n;i++)
{
printf("%d",i+1);
scanf("%d",&pro[i].prio);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pro[i].prio>pro[j].prio)
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
int t_tot=0,t_wait=0;
float a_tot=0,a_wait=0;
//pro[0].t_time=pro[0].b_time-pro[0].a_time;
//pro[0].w_time=0;
pro[0].f_time=pro[0].b_time;
for(i=1;i<n;i++)
{
pro[i].f_time=pro[i-1].f_time+pro[i].b_time;
}
for(i=0;i<n;i++)
{
pro[i].t_time=pro[i].f_time-pro[i].a_time;
pro[i].w_time=pro[i].t_time-pro[i].b_time;
t_tot=t_tot+pro[i].t_time;
t_wait=t_wait+pro[i].w_time;
}
a_tot=float(t_tot)/n;
a_wait=float(t_wait)/n;
printf("\n Avg tot=%.4f avg waiting time=%.4f",a_tot,a_wait);
}
void rr()
{
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pro[i].a_time>pro[j].a_time)
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
int t_tot=0,t_wait=0;
float a_tot,a_wait;
int q_time,count=0;
printf("\nEnter q_time=");
scanf("%d",&q_time);
for(i=0;i<n;i++)
pro[i].flag=0;
i=0;
while(1)
{
if(!pro[i].flag)
{
if(pro[i].b_time>q_time)
{
pro[i].b_time=pro[i].b_time-q_time;
count=count+q_time;
for(j=0;j<n;j++)
if(j!=i&&!pro[j].flag)
pro[j].w_time=pro[j].w_time+q_time;
}
else
{
count=count+pro[i].b_time;
for(j=0;j<n;j++)
if(j!=i&&!pro[j].flag)
pro[j].w_time=pro[j].w_time+pro[i].b_time;
pro[i].t_time=count;//-pro[i].a_time;
pro[i].b_time=0;
pro[i].flag=1;
}
}
int x;
for(x=0;x<n;x++)
if(!pro[x].flag)
break;
if(x==n)
break;
i=++i%n;
}
for(i=0;i<n;i++)
{
t_tot=t_tot+pro[i].t_time;
t_wait=t_wait+pro[i].w_time;
printf("\nT_time=%d W_time=%d",pro[i].t_time,pro[i].w_time);
}
a_tot=float(t_tot)/n;
a_wait=float(t_wait)/n;
printf("\nAvg tot=%.4f avg wait=%.4f",a_tot,a_wait);
}
int main()
{
get();
rr();
prio();
fcfs();
return(0);
}
struct pro
{
char name[10];
int a_time;
int b_time;
int w_time;
int t_time;
int f_time;
int prio;
int flag;
}pro[10],temp;
int i,j,n;
void get()
{
printf("Enter no of process");
scanf("%d",&n);
printf("\nProcess :-");
for(i=0;i<n;i++)
{
printf("\n Process Name");
scanf("%s",&pro[i].name);
printf("\n Enter arrival time :");
scanf("%d",&pro[i].a_time);
printf("\n Enter burst time");
scanf("%d",&pro[i].b_time);
}
}
void fcfs()
{
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pro[i].a_time>pro[j].a_time)
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
int t_tot=0,t_wait=0;
float a_tot=0,a_wait=0;
//pro[0].t_time=pro[0].b_time-pro[0].a_time;
//pro[0].w_time=0;
pro[0].f_time=pro[0].b_time;
for(i=1;i<n;i++)
{
pro[i].f_time=pro[i-1].f_time+pro[i].b_time;
}
for(i=0;i<n;i++)
{
pro[i].t_time=pro[i].f_time-pro[i].a_time;
pro[i].w_time=pro[i].t_time-pro[i].b_time;
t_tot=t_tot+pro[i].t_time;
t_wait=t_wait+pro[i].w_time;
}
a_tot=float(t_tot)/n;
a_wait=float(t_wait)/n;
printf("\n Avg tot=%.4f avg waiting time=%.4f",a_tot,a_wait);
}
void prio()
{
int tt=0,tw=0;
float at=0,aw=0;
printf("\nEnter priority for processes ");
for(i=0;i<n;i++)
{
printf("%d",i+1);
scanf("%d",&pro[i].prio);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pro[i].prio>pro[j].prio)
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
int t_tot=0,t_wait=0;
float a_tot=0,a_wait=0;
//pro[0].t_time=pro[0].b_time-pro[0].a_time;
//pro[0].w_time=0;
pro[0].f_time=pro[0].b_time;
for(i=1;i<n;i++)
{
pro[i].f_time=pro[i-1].f_time+pro[i].b_time;
}
for(i=0;i<n;i++)
{
pro[i].t_time=pro[i].f_time-pro[i].a_time;
pro[i].w_time=pro[i].t_time-pro[i].b_time;
t_tot=t_tot+pro[i].t_time;
t_wait=t_wait+pro[i].w_time;
}
a_tot=float(t_tot)/n;
a_wait=float(t_wait)/n;
printf("\n Avg tot=%.4f avg waiting time=%.4f",a_tot,a_wait);
}
void rr()
{
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pro[i].a_time>pro[j].a_time)
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
int t_tot=0,t_wait=0;
float a_tot,a_wait;
int q_time,count=0;
printf("\nEnter q_time=");
scanf("%d",&q_time);
for(i=0;i<n;i++)
pro[i].flag=0;
i=0;
while(1)
{
if(!pro[i].flag)
{
if(pro[i].b_time>q_time)
{
pro[i].b_time=pro[i].b_time-q_time;
count=count+q_time;
for(j=0;j<n;j++)
if(j!=i&&!pro[j].flag)
pro[j].w_time=pro[j].w_time+q_time;
}
else
{
count=count+pro[i].b_time;
for(j=0;j<n;j++)
if(j!=i&&!pro[j].flag)
pro[j].w_time=pro[j].w_time+pro[i].b_time;
pro[i].t_time=count;//-pro[i].a_time;
pro[i].b_time=0;
pro[i].flag=1;
}
}
int x;
for(x=0;x<n;x++)
if(!pro[x].flag)
break;
if(x==n)
break;
i=++i%n;
}
for(i=0;i<n;i++)
{
t_tot=t_tot+pro[i].t_time;
t_wait=t_wait+pro[i].w_time;
printf("\nT_time=%d W_time=%d",pro[i].t_time,pro[i].w_time);
}
a_tot=float(t_tot)/n;
a_wait=float(t_wait)/n;
printf("\nAvg tot=%.4f avg wait=%.4f",a_tot,a_wait);
}
int main()
{
get();
rr();
prio();
fcfs();
return(0);
}
0 comments:
Post a Comment