P 11
P 11
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");
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
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 |
-----------------------------------------------------