#include<stdio.h>
#include<malloc.h>
typedef struct process
{
char p_name[2];
int cpu_time,arrival_time,finish_time,turn_time,waiting_time,start_time;
}process;
int main()
{
int no,i,total_cpu_time=0,j;
float avg_turn_time=0,avg_waiting_time=0;
process obj[7];
process small;
printf("\nEnter no of processes:");
scanf("\n%d",&no);
//Accepts input from user
for(i=0;i<no;i++)
{
printf("\nEnter Process ID, Arrival Time and CPU time\n");
scanf("%s%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time);
total_cpu_time=total_cpu_time+obj[i].cpu_time;
}
//Sort according to CPU time
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if(obj[i].cpu_time>obj[j].cpu_time)
{
small=obj[i];
obj[i]=obj[j];
obj[j]=small;
}
}
}
for(i=0;i<no;i++)
{
printf("\n%d",obj[i].cpu_time);
}
//First process time
obj[0].start_time=1;
obj[0].finish_time=obj[0].cpu_time;
obj[0].turn_time=obj[0].cpu_time;
obj[0].waiting_time=0;
avg_turn_time=obj[0].turn_time;
avg_waiting_time=0;
//calculates time for each process
for(i=1;i<no;i++)
{
obj[i].start_time=obj[i-1].finish_time+1;
obj[i].finish_time=obj[i-1].finish_time+obj[i].cpu_time;
obj[i].turn_time=obj[i].finish_time;
obj[i].waiting_time=obj[i].turn_time-obj[i].cpu_time;
avg_turn_time=avg_turn_time+obj[i].turn_time;
avg_waiting_time=avg_waiting_time+obj[i].waiting_time;
}
//Display all times
printf("\n============================================================================================");
printf("\nP_ID\tArrival_Time CPU_time Start_Time Finish_Time Turn_Time Waiting_Time\n");
printf("============================================================================================\n");
for(i=0;i<no;i++)
{
printf("\n%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d",obj[i].p_name,obj[i].arrival_time,obj[i].cpu_time,obj[i].start_time,obj[i].finish_time,obj[i].turn_time,obj[i].waiting_time);
}
printf("\n");
avg_turn_time=avg_turn_time/no;
avg_waiting_time=avg_waiting_time/no;
printf("\nAverage Turnaround Time=%f",avg_turn_time);
printf("\nAverage Waiting Time=%f",avg_waiting_time);
printf("\n");
}
#include<malloc.h>
typedef struct process
{
char p_name[2];
int cpu_time,arrival_time,finish_time,turn_time,waiting_time,start_time;
}process;
int main()
{
int no,i,total_cpu_time=0,j;
float avg_turn_time=0,avg_waiting_time=0;
process obj[7];
process small;
printf("\nEnter no of processes:");
scanf("\n%d",&no);
//Accepts input from user
for(i=0;i<no;i++)
{
printf("\nEnter Process ID, Arrival Time and CPU time\n");
scanf("%s%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time);
total_cpu_time=total_cpu_time+obj[i].cpu_time;
}
//Sort according to CPU time
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if(obj[i].cpu_time>obj[j].cpu_time)
{
small=obj[i];
obj[i]=obj[j];
obj[j]=small;
}
}
}
for(i=0;i<no;i++)
{
printf("\n%d",obj[i].cpu_time);
}
//First process time
obj[0].start_time=1;
obj[0].finish_time=obj[0].cpu_time;
obj[0].turn_time=obj[0].cpu_time;
obj[0].waiting_time=0;
avg_turn_time=obj[0].turn_time;
avg_waiting_time=0;
//calculates time for each process
for(i=1;i<no;i++)
{
obj[i].start_time=obj[i-1].finish_time+1;
obj[i].finish_time=obj[i-1].finish_time+obj[i].cpu_time;
obj[i].turn_time=obj[i].finish_time;
obj[i].waiting_time=obj[i].turn_time-obj[i].cpu_time;
avg_turn_time=avg_turn_time+obj[i].turn_time;
avg_waiting_time=avg_waiting_time+obj[i].waiting_time;
}
//Display all times
printf("\n============================================================================================");
printf("\nP_ID\tArrival_Time CPU_time Start_Time Finish_Time Turn_Time Waiting_Time\n");
printf("============================================================================================\n");
for(i=0;i<no;i++)
{
printf("\n%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d",obj[i].p_name,obj[i].arrival_time,obj[i].cpu_time,obj[i].start_time,obj[i].finish_time,obj[i].turn_time,obj[i].waiting_time);
}
printf("\n");
avg_turn_time=avg_turn_time/no;
avg_waiting_time=avg_waiting_time/no;
printf("\nAverage Turnaround Time=%f",avg_turn_time);
printf("\nAverage Waiting Time=%f",avg_waiting_time);
printf("\n");
}
No comments:
Post a Comment