Given the process names, their burst times and their arrival order, write a C Program to compute the waiting time and turnaround time of n processes using the FCFS Algorithm and display them. Also, compute and print the average waiting time and average turnaround time. The names of the processes are of type character array, burst time and arrival order are of the type integers.
Input and Output format :
Refer Sample Input and Output for inputs and formatting specifications.
Use printf("%s%60d%60d%40d%40d\n",p1.name, p1.cbt, p1.arrt, p1.wt, p1.tat"); to display the details in the specified format.[same for process 2]
Use "%s%40s%40s%30s%29s" to print the table header in the specified format.
Display the floating point numbers corrected to two decimal places.
Sample Input and Output:
[Note: All the Text in Bold corresponds to the input and the rest correspond to the output]
Enter the number of processes
4
Enter the name of process 1
A
Enter the burst time of process 1
10
Enter the arrival order of process 1
3
Enter the name of process 2
B
Enter the burst time of process 2
20
Enter the arrival order of process 2
1
Enter the name of process 3
C
Enter the burst time of process 3
30
Enter the arrival order of process 3
4
Enter the name of process 4
D
Enter the burst time of process 4
40
Enter the arrival order of process 4
2
Process Details
Process Name CPU Burst Time Arrival Order Waiting Time Turnaround Time
B 20 1 0 20
D 40 2 20 60
A 10 3 60 70
C 30 4 70 100
Average waiting time is 37.50
Average turn around time is 62.50
Answer:
#include<stdio.h> #define max30 int main() { struct process{ char name; int wait,turn,arrival,burst,temp,tat; }p[100]; float awt,atat; char tempc; int pcs; printf("Enter the number of processes\n"); scanf("%d",&pcs); for(int i=0;i<pcs;i++) { printf("Enter the name of process %d\n",i+1); scanf("%s",&p[i].name); printf("Enter the burst time of process %d\n",i+1); scanf("%d",&p[i].burst); printf("Enter the arrival order of process %d\n",i+1); scanf("%d",&p[i].arrival); } for(int i=0;i<pcs;i++) { for(int j=0;j<pcs-i-1;j++) { if(p[j].arrival>p[j+1].arrival) { p[i].temp=p[j].arrival; p[j].arrival=p[j+1].arrival; p[j+1].arrival=p[i].temp; tempc=p[j].name; p[j].name=p[j+1].name; p[j+1].name=tempc; p[i].temp=p[j].burst; p[j].burst=p[j+1].burst; p[j+1].burst=p[i].temp; } } } printf("Process Details\n"); printf("%s%40s%40s%30s%30s\n","Process Name","CPU Burst Time","Arrival order","Waiting Time","Turnaround Time"); for(int i=0;i<pcs;i++) { p[i].wait=0; p[i].turn=0; for(int j=0;j<i;j++) { p[i].wait=p[i].wait+p[j].burst; } p[i].turn=p[i].wait+p[i].burst; awt=awt+p[i].wait; atat=atat+p[i].turn; printf("%s%60d%60d%40d%40d\n",&p[i].name,p[i].burst,p[i].arrival,p[i].wait,p[i].turn); } printf("Average waiting time is %0.2f\n",awt/pcs); printf("Average turnaround time is %0.2f\n",atat/pcs); }
0 Comments