#include<stdio.
h>
struct process{
int pid,a_time,b_time,c_time,r_time,t_time,w_time;
};
void main()
{
//Declaring variables
int n,i,j,total_ta_time = 0,total_wa_time = 0,CPU;
float avg_ta_time,avg_wa_time,throughput;
//Asking user to enter no of process
printf("Enter no of process: ");
scanf("%d",&n);
struct process p[n],temp;//declaring array of process
//asking user to enter arrival time for each process
for(i = 0; i < n; i++){
printf("Enter Arrival Time for process P%d: ",i+1);
scanf("%d",&p[i].a_time);
p[i].pid = i+1;
}
//asking user to enter burst time for each process
for(i = 0; i < n; i++){
printf("Enter Burst Time for process P%d: ",i+1);
scanf("%d",&p[i].b_time);
p[i].r_time = p[i].b_time;
}
//Sorting the process on basis of arrival time
for(i = 0; i < n; i++){
for(j = i+1; j < n; j++){
if((p[i].a_time > p[j].a_time) || (p[i].a_time == p[j].a_time &&
p[i].pid > p[j].pid)){
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}
//Declaring variables for while loop
int m = 2*n;
int current_time = 0,completed = 0,k = 0,a[m],b[m];
printf("\n SRTN Scheduling Algoritham\n");
printf("\nGantt Chart\n");
//Main logic of srtn scheduling
while(completed < n){
int min_r_time = -1, index = -1;
if(k < n){
for(i = 0; i < n; i++){
if(p[i].a_time <= current_time && p[i].r_time > 0){
if(min_r_time == -1 || p[i].r_time < min_r_time){
min_r_time = p[i].r_time;
index = i;
}
}
}
p[index].r_time--;
current_time++;
a[k] = p[index].pid;
b[k] = current_time;
k++;
if(p[index].r_time == 0){
p[index].c_time = current_time;
completed++;
}
}
else{
for(i = 0; i < n; i++){
for(j = i+1; j < n; j++){
if((p[i].r_time > p[j].r_time) || (p[i].r_time == p[j].r_time
&& p[i].a_time > p[j].a_time)){
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}
for(i = 0; i < n; i++){
if(p[i].r_time == 0){
continue;
}
else{
a[k] = p[i].pid;
current_time += p[i].r_time;
b[k] = current_time;
k++;
p[i].c_time = current_time;
completed++;
}
}
}
}
//Printing gantt chart
printf(" ");
for(i = 0; i < k; i++){
printf("------");printf(" ");
}printf("\n|");
for(i = 0; i < k; i++){
printf(" P%d |",a[i]);//Printing process
}printf("\n ");
for(i = 0; i < k; i++){
printf("------");printf(" ");
}
printf("\n0");
for(i = 0; i < k; i++)
{
printf(" ");
if(b[i] > 99){printf("\b\b");}
else if(b[i] > 9){printf("\b");}
printf("%d",b[i]);//Displaying time
}
//Sorting the process on basis of completion time
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(p[i].c_time > p[j].c_time)
{
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}
//calculating turnaround time and waiting time for each process and their total
for(i = 0; i < n; i++)
{
p[i].t_time = p[i].c_time - p[i].a_time;
p[i].w_time = p[i].t_time - p[i].b_time;
}
for(i = 0; i < n; i++)
{
total_ta_time = total_ta_time + p[i].t_time;
total_wa_time = total_wa_time + p[i].w_time;
}
//Printing the table
printf("\n\n Process Table\n");
for (i = 0; i < 53; i++)
{
printf("-");
}
printf("\n");
printf("| Time | Process | Turnaround Time | Waiting Time |\n");
printf("| | Completed | (P.C-P.S) | (T.T-B.T) |\n");
for (i = 0; i < 53; i++)
{
printf("-");
}
printf("\n");
printf("| 0 | - | - | - |\n");
for (i = 0; i < n; i++)
{
printf("| %3d | P%d | %3d | %3d |\n",
p[i].c_time, p[i].pid, p[i].t_time, p[i].w_time);
}
for (i = 0; i < 53; i++)
{
printf("-");
}
printf("\n\n");
printf("Average turnaround time=Total turnaround time/No. of processes\n= ");
//printf("\nAverage turnaround time = ");
for(i=0; i<n; i++)
{
printf("TT(P%d)", i+1);
if(i<(n-1))
printf("+");
}
printf("/%d\n",n);
printf("= ");
for(i=0; i<n; i++)
{
printf("%d" , p[i].t_time);
if(i<(n-1))
printf("+");
}
printf("/%d",n);
avg_ta_time = ((float)total_ta_time)/n;
printf("\n= %.2fms\n",avg_ta_time);
printf("\nAverage waiting time=Total waiting time/No. of processes\n= ");
for(i=0; i<n; i++)
{
printf("WT(P%d)", i+1);
if(i<(n-1))
printf("+");
}
printf("/%d\n",n);
printf("= ");
for(i=0; i<n; i++)
{
printf("%d" , p[i].w_time);
if(i<(n-1))
printf("+");
}
printf("/%d",n);
avg_wa_time = ((float)total_wa_time)/n;
printf("\n= %.2f ms",avg_wa_time);
printf("\n\nThroughput = No.of processes/Total time");
printf("\n= %d/%d",n,current_time);
throughput = (float)n/current_time;
printf("\n= %.2fms\n",throughput);
printf("\nCPU utilization = (Process busy time/(Process busy time+Process
ideal time))*100");
printf("\n= (%d/(%d+0))*100",current_time,current_time);
printf("\n= (1/1)*100");
CPU = (current_time/(current_time+0))*100;
char d = '%';
printf("\n= %d%c",CPU,d);
}
OUTPUT :
Enter no of process: 6
Enter Arrival Time for process P1: 2
Enter Arrival Time for process P2: 3
Enter Arrival Time for process P3: 4
Enter Arrival Time for process P4: 5
Enter Arrival Time for process P5: 1
Enter Arrival Time for process P6: 2
Enter Burst Time for process P1: 12
Enter Burst Time for process P2: 3
Enter Burst Time for process P3: 11
Enter Burst Time for process P4: 4
Enter Burst Time for process P5: 5
Enter Burst Time for process P6: 8
SRTN Scheduling Algoritham
Gantt Chart
------------------------------------------------------------------------------
| P0 | P5 | P5 | P5 | P5 | P5 | P2 | P4 | P6 | P3 | P1 |
------------------------------------------------------------------------------
0 1 2 3 4 5 6 9 13 21 32 44
Process Table
-----------------------------------------------------
| Time | Process | Turnaround Time | Waiting Time |
| | Completed | (P.C-P.S) | (T.T-B.T) |
-----------------------------------------------------
| 0 | - | - | - |
| 6 | P5 | 5 | 0 |
| 9 | P2 | 6 | 3 |
| 13 | P4 | 8 | 4 |
| 21 | P6 | 19 | 11 |
| 32 | P3 | 28 | 17 |
| 44 | P1 | 42 | 30 |
-----------------------------------------------------
Average turnaround time=Total turnaround time/No. of processes
= TT(P1)+TT(P2)+TT(P3)+TT(P4)+TT(P5)+TT(P6)/6
= 5+6+8+19+28+42/6
= 18.00 ms
Average waiting time=Total waiting time/No. of processes
= WT(P1)+WT(P2)+WT(P3)+WT(P4)+WT(P5)+WT(P6)/6
= 0+3+4+11+17+30/6
= 10.83 ms
Throughput = No.of processes/Total time
= 6/44
= 0.14ms
CPU utilization = (Process busy time/(Process busy time+Process ideal time))*100
= (44/(44+0))*100
= (1/1)*100
= 100%