Write a C Program to compute the waiting time and turnaround time of the list of processes using Priority scheduling algorithm given the process names, their burst times and priority.
Note: If more than one process has same priority then execute based on input order.
Input and Output format :
Refer Sample Input and Output for inputs and formatting specifications.
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
5
Enter the priority of process 1
2
Enter the name of process 2
B
Enter the burst time of process 2
6
Enter the priority of process 2
1
Enter the name of process 3
C
Enter the burst time of process 3
3
Enter the priority of process 3
5
Enter the name of process 4
D
Enter the burst time of process 4
2
Enter the priority of process 4
4
Process Details
Process Name CPU Burst Time Priority Waiting Time Turnaround Time
B 6 1 o 6
A 5 2 6 11
D 2 4 11 13
C 3 5 13 16
Answer:
#include<stdio.h>
int main()
{
struct process
{
char name;
int wait,turn,temp,burst,arrival,tat;
float awt,atat;
}p[100];
int pro;
printf("Enter the number of processes\n");
scanf("%d",&pro);
char tempc;
for(int i=0;i<pro;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 priority of process %d\n",i+1);
scanf("%d",&p[i].arrival);
}
for(int i=0;i<pro;i++)
{
for(int j=0;j<pro-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","Priority","Waiting Time","Turnaround Time");
for(int i=0;i<pro;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;
printf("%s%60d%60d%40d%40d\n",&p[i].name,p[i].burst,p[i].arrival,p[i].wait,p[i].turn);
}
return 0;
}
0 Comments