Created
May 13, 2020 16:28
-
-
Save projjal1/bfe9cc1f29f121304c72324d80d760e4 to your computer and use it in GitHub Desktop.
SJF scheduling algorithm in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
//Sorting in increasing order of arrival time | |
void arrangeArrival(int num, int mat[][6]) | |
{ | |
for(int i=0; i<num; i++) | |
{ | |
for(int j=0; j<num-i-1; j++) | |
{ | |
if(mat[j][1] > mat[j+1][1]) | |
{ | |
for(int k=0; k<5; k++) | |
{ | |
int tmp=mat[j][1]; | |
mat[j][1]=mat[j+1][1]; | |
mat[j][1]=tmp; | |
} | |
} | |
} | |
} | |
} | |
void completionTime(int num, int mat[][6]) | |
{ | |
int temp, val; | |
mat[0][3] = mat[0][1] + mat[0][2]; | |
mat[0][5] = mat[0][3] - mat[0][1]; | |
mat[0][4] = mat[0][5] - mat[0][2]; | |
for(int i=1; i<num; i++) | |
{ | |
temp = mat[i-1][3]; | |
int low = mat[i][2]; | |
for(int j=i; j<num; j++) | |
{ | |
if(temp >= mat[j][1] && low >= mat[j][2]) | |
{ | |
low = mat[j][2]; | |
val = j; | |
} | |
} | |
mat[val][3] = temp + mat[val][2]; | |
mat[val][5] = mat[val][3] - mat[val][1]; | |
mat[val][4] = mat[val][5] - mat[val][2]; | |
for(int k=0; k<6; k++) | |
{ | |
int tmp=mat[val][k]; | |
mat[val][k]=mat[i][k]; | |
mat[i][k]=tmp; | |
} | |
} | |
} | |
int main() | |
{ | |
int num, temp; | |
//Number of processes | |
num=4; | |
//Matrix in order of process_id, arrival_time, burst_time | |
int mat[][6]={{1,2,3,0,0,0},{2,0,4,0,0,0},{3,4,2,0,0,0},{4,5,4,0,0,0}}; | |
printf("Before arranging \n"); | |
for(int i=0; i<num; i++) | |
{ | |
printf("Process id: %d \n",mat[i][0]); | |
printf("Arrival time: %d \n",mat[i][1]); | |
printf("Burst time: %d \n",mat[i][2]); | |
} | |
arrangeArrival(num, mat); | |
completionTime(num, mat); | |
printf("\n\nAfter arranging \n"); | |
for(int i=0; i<num; i++) | |
{ | |
printf("Process id: %d \n",mat[i][0]); | |
printf("Arrival time: %d \n",mat[i][1]); | |
printf("Burst time: %d \n",mat[i][2]); | |
printf("Waiting time: %d \n",mat[i][4]); | |
printf("Turnaround time: %d \n",mat[i][5]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment