#include<stdio.h>
#include<stdlib.h>
typedef struct process
{
char p_name[2];
int cpu_time,arrival_time,finish_time,turn_time;
int waiting_time,start_time,cpu_temp,flag;
}process;
int main()
{
int no,i,total_cpu_time=0,j,processes,running_time,process_no=0,current,x,time;
float avg_turn_time=0,avg_waiting_time=0;
process obj[7];
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);
obj[i].cpu_temp=obj[i].cpu_time;
obj[i].flag=0;
total_cpu_time=total_cpu_time+obj[i].cpu_time;
}
printf("\nEnter time quantam for each process");
scanf("%d",&time);
i=0;
x=0;
processes=no;
running_time=0;
printf("\nRunning Time Executig Process\n");
while(running_time<total_cpu_time)
{
for(j=0;j<time;j++)
{
if(obj[i].cpu_temp==obj[i].cpu_time)
{
obj[i].start_time=running_time+1;
}
if(obj[i].cpu_temp!=0)
{
obj[i].cpu_temp=obj[i].cpu_temp-1;
printf("\n\t%d\t%s",running_time,obj[i].p_name);
running_time=running_time+1;
}
if(obj[i].cpu_temp==0 && obj[i].flag!=1)
{
obj[i].flag=1;
obj[i].finish_time=running_time;
printf("\n%d",obj[i].finish_time);
}
}
i++;
if(i==no)
{
i=0;
}
}
//calculates time for each process
for(i=0;i<no;i++)
{
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%d\t%d\t%d\t%d\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