Created
May 12, 2019 02:36
-
-
Save phatnguyenuit/b56842d0bfe6574b851b4b8b954994a3 to your computer and use it in GitHub Desktop.
SJF.cpp
This file contains hidden or 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> | |
| typedef struct process | |
| { | |
| int id, at, bt, ct=0, rt=0, tat=0, wt=0,at1=0;/* at=arrival time,bt=burst time,ct=completed time,rt=respond time,tat= turn around time ,wt=waiting time*/ | |
| int pri=0; | |
| }pro; | |
| void swap(pro &a, pro &b) | |
| { | |
| pro temp = a; | |
| a = b; | |
| b = temp; | |
| } | |
| void sort_by_arrival_time(pro a[], int n) | |
| { | |
| for (int i = 0; i < n-1; i++) | |
| for (int j = i + 1; j < n; j++) | |
| { | |
| if (a[i].at1 > a[j].at1) | |
| swap(a[i], a[j]); | |
| else if (a[i].at1 == a[j].at1) | |
| { | |
| if (a[i].bt > a[j].bt) | |
| swap(a[i], a[j]); | |
| } | |
| else{} | |
| } | |
| } | |
| void sort_by_id(pro a[], int n) | |
| { | |
| for (int i = 0; i < n - 1; i++) | |
| for (int j = i + 1; j < n; j++) | |
| if (a[i].id>a[j].id) | |
| swap(a[i], a[j]); | |
| } | |
| void Calculate(pro* a, int n) | |
| { | |
| a[0].at = a[0].at1; | |
| for (int i = 1; i < n; i++) //calculate "at" of every process in gain chart | |
| a[i].at = a[i - 1].at + a[i - 1].bt; | |
| for (int i = 0; i < n; i++) //calculate "ct" and "rt" of every process in gain chart | |
| a[i].ct = a[i].at + a[i].bt; | |
| for (int i = 0; i < n; i++) //calculate "rt" and "rt" of every process in gain chart | |
| a[i].rt = a[i].at - a[i].at1; | |
| for (int i = 0; i < n; i++) //calculate "tat" and "rt" of every process in gain chart | |
| a[i].tat = a[i].ct - a[i].at1; | |
| for (int i = 0; i < n; i++) // //calculate "wt" and "rt" of every process in gain chart | |
| a[i].wt = a[i].tat - a[i].bt; | |
| } | |
| void Table_Process(pro a[], int n) | |
| { | |
| printf("PROCESS\tAT\tBT\tCT\tRT\tTAT\tWT\n"); | |
| for (int i = 0; i < n; i++) | |
| printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", a[i].id, a[i].at1, a[i].bt, a[i].ct, a[i].rt, a[i].tat, a[i].wt); | |
| } | |
| void Gain_Chart(pro a[], int n) | |
| { | |
| printf("Gain chart is : \n"); | |
| for (int i = 0; i < n; i++) | |
| printf("%d______________", a[i].at); | |
| printf("%d", a[n - 1].ct); | |
| printf(" (s)\n"); | |
| printf("\tP%d", a[0].id); | |
| for (int i = 1; i < n; i++) | |
| printf("\t\tP%d", a[i].id); | |
| printf("\n"); | |
| } | |
| void sum_average(pro p[],int n,float &totwt,float &totat) | |
| { | |
| for(int i=0;i<n;i++) | |
| { | |
| totwt=totwt+p[i].wt; | |
| totat=totat+p[i].tat; | |
| } | |
| totwt =totwt/n; | |
| totat =totat/n; | |
| } | |
| void sort_by_priority(pro p[],int n) | |
| { | |
| for(int i=0;i<n-1;i++) | |
| for(int j=i+1;j<n;j++) | |
| { | |
| if(p[i].at >= p[j].at) | |
| if(p[i].pri | |
| } | |
| } | |
| int main() | |
| { | |
| int n; | |
| pro *p; | |
| float totwt=0,totat=0; | |
| printf("How many process in ready queue?"); | |
| scanf("%d", &n); | |
| p = new pro[n]; | |
| for (int i = 0; i < n; i++) | |
| { | |
| printf("Insert arrival time & burst time of process P%d\n",i+1); | |
| scanf("%d%d", &p[i].at1, &p[i].bt); | |
| p[i].id = i + 1; | |
| } | |
| sort_by_arrival_time(p, n); | |
| Calculate(p,n); | |
| Gain_Chart(p, n); | |
| printf("\n"); | |
| sort_by_id(p, n); | |
| Table_Process(p, n); | |
| sum_average(p,n,totwt,totat); | |
| printf("\nAverage Waiting Time = %2.2f (s)",totwt); | |
| printf("\nAverage Turn Around Time = %2.2f(s)\n",totat); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment