Está en la página 1de 3

#include<stdio.

h>
#include<string.h>
#include<conio.h>
struct Process
{
char pname[10];
int at;
int bt;
int tempbt;
int ct;
int tat;
int wt;
}
P[10];
int N;//number of process
int tot_tat;
int tot_wt;
void AcceptProcessInfo()
{
int i;
printf("\n Enter the number of process:");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("\n Enter process name:");
scanf("%s",P[i].pname);
printf("\n Enter AT:");
scanf("%d",&P[i].at);
printf("\n Enter BT:");
scanf("%d",&P[i].bt);
P[i].tempbt=P[i].bt;
}
}
void DisplayProcessInfo()
{
int i; clrscr();
printf("\n PName\tAT\tBT\tTAT\tWT");
printf("\n************************************\n");
for(i=0;i<N;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d",P[i].pname,P[i].at,P[i].bt,P[i].tat,P[i].wt);
}
printf("\n************************************\n");
printf("\nTotal TAT:%d",tot_tat);
printf("\n Total WT:%d",tot_wt);
printf("\n Avg TAT:%f",(float)(tot_tat/N));
printf("\n Avg WT:%f",(float)(tot_wt/N));
}
void Sort_Process_AT()
{
char tname[10];
int temp;
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
if(P[i].at<P[i].at)
{
//swap Process name
strcpy(tname,P[i].pname);
strcpy(P[i].pname,P[j].pname);
strcpy(P[j].pname,tname);
//swap at
temp=P[i].at;
P[i].at=P[j].at;
P[j].at=temp;
//swap BT
temp=P[i].bt;
P[i].bt=P[j].bt;
P[j].bt=temp;
//swap temptbt
temp=P[i].tempbt;
P[i].tempbt=P[j].tempbt;
P[j].tempbt=temp;
}
}
}
void fcfs()
{
int time=0;
int finish=0;
int i,j;
int flag=0;
while(finish !=1)
{
flag=0;
for(i=0;i<N;i++)
{
if(P[i].tempbt !=0 &&P[i].at<=time)
{
flag=1;
printf("|%d%s",time,P[i]);
time=time+P[i].bt;
printf("%d",time);
P[i].ct=time;
P[i].tempbt=0;
}
}
if(flag==0)
{
printf("|%d##",time);
time++;
printf("%d|",time);
}
for(j=0;j<N;j++)
{
if(P[j].tempbt==0)
continue;
else
break;
}
if(j==N)
finish=1;
}
}
void Calculate_TAT_WT()
{
int i;
for(i=0;i<N;i++)
{
P[i].tat=P[i].ct-P[i].at;
tot_tat=tot_tat+P[i].tat;
P[i].wt=P[i].tat-P[i].bt;
tot_wt=tot_wt+P[i].wt;
}
}
int main()
{
AcceptProcessInfo();
Sort_Process_AT();
fcfs();
Calculate_TAT_WT();
DisplayProcessInfo();
return 0;
}

#include<stdio.h>

También podría gustarte