#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct process
{
char p_name[2];
int cpu_time,arrival_time,finish_time,turn_time,waiting_time,start_time,prior;
}process;
int main()
{
int no,i,total_cpu_time=0,j,processes,running;
float avg_turn_time=0,avg_waiting_time=0;
process obj[7],temp[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, CPU time and Priority\n");
scanf("%s%d%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time,&obj[i].prior);
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].prior>obj[j].prior)
{
small=obj[i];
obj[i]=obj[j];
obj[j]=small;
}
}
}
for(i=0;i<no;i++)
{
printf("\n%d",obj[i].prior);
}
//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 Priority\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\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,obj[i].prior);
}
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-------------------//
/*
sainath@ubuntu: ./a.out
Enter no of processes:5
Enter Process ID, Arrival Time, CPU time and Priority
p1
0
14
2
Enter Process ID, Arrival Time, CPU time and Priority
p2
3
8
0
Enter Process ID, Arrival Time, CPU time and Priority
p3
5
5
1
Enter Process ID, Arrival Time, CPU time and Priority
p4
9
12
0
Enter Process ID, Arrival Time, CPU time and Priority
p5
16
6
2
0
0
1
2
2
============================================================================================================
P_ID Arrival_Time CPU_time Start_Time Finish_Time Turn_Time Waiting_Time Priority
==============================================================================================================
p2 3 8 1 8 8 0 0
p4 9 12 9 20 20 8 0
p3 5 5 21 25 25 20 1
p1 0 14 26 39 39 25 2
p5 16 6 40 45 45 39 2
Average Turnaround Time=27.400000
Average Waiting Time=18.400000
*/
#include<malloc.h>
#include<stdlib.h>
typedef struct process
{
char p_name[2];
int cpu_time,arrival_time,finish_time,turn_time,waiting_time,start_time,prior;
}process;
int main()
{
int no,i,total_cpu_time=0,j,processes,running;
float avg_turn_time=0,avg_waiting_time=0;
process obj[7],temp[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, CPU time and Priority\n");
scanf("%s%d%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time,&obj[i].prior);
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].prior>obj[j].prior)
{
small=obj[i];
obj[i]=obj[j];
obj[j]=small;
}
}
}
for(i=0;i<no;i++)
{
printf("\n%d",obj[i].prior);
}
//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 Priority\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\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,obj[i].prior);
}
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-------------------//
/*
sainath@ubuntu: ./a.out
Enter no of processes:5
Enter Process ID, Arrival Time, CPU time and Priority
p1
0
14
2
Enter Process ID, Arrival Time, CPU time and Priority
p2
3
8
0
Enter Process ID, Arrival Time, CPU time and Priority
p3
5
5
1
Enter Process ID, Arrival Time, CPU time and Priority
p4
9
12
0
Enter Process ID, Arrival Time, CPU time and Priority
p5
16
6
2
0
0
1
2
2
============================================================================================================
P_ID Arrival_Time CPU_time Start_Time Finish_Time Turn_Time Waiting_Time Priority
==============================================================================================================
p2 3 8 1 8 8 0 0
p4 9 12 9 20 20 8 0
p3 5 5 21 25 25 20 1
p1 0 14 26 39 39 25 2
p5 16 6 40 45 45 39 2
Average Turnaround Time=27.400000
Average Waiting Time=18.400000
*/
No comments:
Post a Comment