Shortest Job First SJF Scheduling Algorithm in C and C++ with Gantt Chart .
C++ Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include <bits/stdc++.h> using namespace std; const int N=100005; int n; struct process { int id; int burst_time; int arrival_time; int waiting_time; int finishing_time; int turn_around_time; }; process P[N]; void SJF() { int complete,current_time,index,minimum; double total_waiting_time = 0.0; double total_turn_around_time = 0.0; index = -1; complete = 0; current_time = 0; minimum = INT_MAX; while(complete < n) { for(int i=0; i<n; i++) { if(P[i].arrival_time <= current_time) { if(P[i].burst_time < minimum && P[i].finishing_time == 0) { index = i; minimum = P[i].burst_time; } } } if(index >= 0) { complete++; minimum = INT_MAX; current_time += P[index].burst_time; P[index].finishing_time = current_time; P[index].turn_around_time = P[index].finishing_time - P[index].arrival_time; P[index].waiting_time = P[index].turn_around_time- P[index].burst_time; total_waiting_time += P[index].waiting_time; total_turn_around_time += P[index].turn_around_time; index = -1; } else { current_time++; } } cout<<fixed<<setprecision(2); cout<<"Average Waiting Time: "<<(total_waiting_time/n)<<"\n"; cout<<"Average Turn Around Time: "<<(total_turn_around_time/n)<<"\n"; } int main() { cout<<"Number of Processes: "; cin>>n; cout<<"Process Ids:\n"; for(int i=0; i<n; i++) cin>>P[i].id; cout<<"Process Burst Times:\n"; for(int i=0; i<n; i++) cin>>P[i].burst_time; cout<<"Process Arrival Times:\n"; for(int i=0; i<n; i++) cin>>P[i].arrival_time; SJF(); return 0; } /** 4 1 2 3 4 6 8 7 3 0 0 0 0 */ |
C Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
#include <stdio.h> #include <stdlib.h> #define MAX_PROCESS 100 struct process { int pid; int burst_time; int waiting_time; }; typedef struct process Process; double average_waiting_time; int total_waiting_time; void sort_process_by_burst_time(Process p[], int n); void calculate_waiting_time(Process p[], int n); void print_gantt_chart(Process p[], int n); int main() { Process p[MAX_PROCESS]; int n, i, j; puts("SHORTEST JOB FIRST SCHEDULING ALGORITHM"); puts("======================================="); printf("Enter total process: "); scanf("%d", &n); printf("Enter burst time for each process:\n"); for(i=0; i<n; i++) { printf("P[%d]: ", i+1); scanf("%d", &p[i].burst_time); p[i].pid = i+1; } sort_process_by_burst_time(p, n); calculate_waiting_time(p, n); average_waiting_time = (double) ( (double)total_waiting_time / (double) n ); puts(""); printf("Average Waiting Time: %.2lf\n",average_waiting_time); printf("Gantt Chart:\n"); print_gantt_chart(p, n); return 0; } void sort_process_by_burst_time(Process p[], int n) { int i, j; Process temp; for(i=0; i<n-1; i++) { for(j=0; j<n-1-i; j++) { if(p[j].burst_time > p[j+1].burst_time) { temp = p[j]; p[j] = p[j+1]; p[j+1] = temp; } } } } void calculate_waiting_time(Process p[], int n) { int i; total_waiting_time = 0; p[0].waiting_time = 0; for(i=1; i<n; i++) { p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time; total_waiting_time += p[i].waiting_time; } } void print_gantt_chart(Process p[], int n) { int i, j; int last = p[n-1].burst_time + ( n== 1 ? 0 : p[n-1].waiting_time); // printing top bar printf(" "); for(i=0; i<n; i++) { for(j=0; j<p[i].burst_time; j++) printf("--"); printf(" "); } printf("\n|"); // middle position for(i=0; i<n; i++) { for(j=0; j<p[i].burst_time-1; j++) printf(" "); printf("p%d", p[i].pid); for(j=0; j<p[i].burst_time-1; j++) printf(" "); printf("|"); } printf("\n "); // bottom bar for(i=0; i<n; i++) { for(j=0; j<p[i].burst_time; j++) printf("--"); printf(" "); } printf("\n"); // printing waiting time int minus = 0; for(i=0; i<n; i++) { if(p[i].waiting_time>9) printf(" "); printf("%d", p[i].waiting_time); if(p[i+1].waiting_time>9){ minus = 1; } if(i+1 == n ) if (last>9) minus = 1; for(j=0; j<p[i].burst_time-minus; j++) printf(" "); } if(last>9) printf(" "); printf("%d\n", last); } |
Output and Gantt Chart: