-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsjf with arrival time.cpp
79 lines (65 loc) · 1.6 KB
/
sjf with arrival time.cpp
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
#include<stdio.h>
#include<stdlib.h>
void sort(int* a, int *b,int st,int n){
int k,j,key1,key2;
for(int i=st; i<n; i++){
j=i-1;
k=i-1;
key1=a[i];
key2=b[i];
while(j>=st && key1<a[j]){
a[j+1]=a[j];
b[k+1]=b[k];
j--;
k--;
}
a[j+1]=key1;
b[k+1]=key2;
}
}
int main(){
int no_of_processes;
printf("Enter How many no of processes you have? : ");
scanf("%d",&no_of_processes);
printf("\n");
int* at=(int*)malloc(no_of_processes*sizeof(int));
int* bt=(int*)malloc(no_of_processes*sizeof(int));
printf("at bt\n");
for(int i=0; i<no_of_processes; i++){
scanf("%d%d",&at[i],&bt[i]);
}
printf("\n");
sort(at,bt,0,no_of_processes);
// printing the burst time
for(int j=0; j<no_of_processes; j++){
printf("%d %d\n",at[j],bt[j]);
}
printf("\n");
int* ct=(int*)malloc(no_of_processes*sizeof(int));
ct[0]=at[0]+bt[0];
for(int i=1;i<no_of_processes;i++){
if(ct[i-1]>at[i]){
sort(bt,at,i,no_of_processes);/*
for(int j=i; j<no_of_processes; j++){
printf("%d %d\n",at[j],bt[j]);
}*/
ct[i]=ct[i-1]+bt[i];
}
else{
ct[i]=at[i]+bt[i];
}
printf("%d %d %d\n",at[i],bt[i],ct[i]);
}
int* tat=(int*)malloc(no_of_processes*sizeof(int));
for(int i=0; i<no_of_processes; i++){
tat[i]=ct[i]-at[i];
}
int* wt=(int*)malloc(no_of_processes*sizeof(int));
for(int i=0; i<no_of_processes; i++){
wt[i]=tat[i]-bt[i];
}
printf("at bt ct tat wt\n");
for(int i=0; i<no_of_processes; i++){
printf("%d\t%d\t%d\t%d\t%d\n",at[i],bt[i],ct[i],tat[i],wt[i]);
}
}