#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;
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);
}
//first porocess 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].arrival_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("\nP_ID\tArrival_Time CPU_time Start_Time Finish_Time Turn_Time Waiting_Time\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");
}
//-------------------Output-------------------
/*
Enter no of processes:5
Enter Process ID, Arrival Time and CPU time
p1
0
6
Enter Process ID, Arrival Time and CPU time
p2
3
9
Enter Process ID, Arrival Time and CPU time
p3
5
2
Enter Process ID, Arrival Time and CPU time
p4
8
10
Enter Process ID, Arrival Time and CPU time
p5
11
6
P_ID Arrival_Time CPU_time Start_Time Finish_Time Turn_Time Waiting_Time
p1 0 6 1 6 6 0
p2 3 9 7 15 12 3
p3 5 2 16 17 12 10
p4 8 10 18 27 19 9
p5 11 6 28 33 22 16
Average Turnaround Time=14.200000
Average Waiting Time=7.600000
*/
#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;
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);
}
//first porocess 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].arrival_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("\nP_ID\tArrival_Time CPU_time Start_Time Finish_Time Turn_Time Waiting_Time\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");
}
//-------------------Output-------------------
/*
Enter no of processes:5
Enter Process ID, Arrival Time and CPU time
p1
0
6
Enter Process ID, Arrival Time and CPU time
p2
3
9
Enter Process ID, Arrival Time and CPU time
p3
5
2
Enter Process ID, Arrival Time and CPU time
p4
8
10
Enter Process ID, Arrival Time and CPU time
p5
11
6
P_ID Arrival_Time CPU_time Start_Time Finish_Time Turn_Time Waiting_Time
p1 0 6 1 6 6 0
p2 3 9 7 15 12 3
p3 5 2 16 17 12 10
p4 8 10 18 27 19 9
p5 11 6 28 33 22 16
Average Turnaround Time=14.200000
Average Waiting Time=7.600000
*/
No comments:
Post a Comment