-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsjfs.c
73 lines (66 loc) · 2.05 KB
/
sjfs.c
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
#include<stdio.h>
#include<string.h>
void getData();
void sjfs();
void calculate();
void display();
int burstTime[20], waitingTime[20], exitTime[20], turnAroundTime[20];
int totalWaitingTime, totalTurnAroundTime, n, temp1;
char pName[20][20], temp[20];
float averageWaitingTime, averageTurnAroundTime;
void main() {
getData();
sjfs();
calculate();
display();
}
void getData() {
printf("Enter the no Process: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("\nEnter the Process Name: ");
scanf("%s", &pName[i]);
printf("Enter the BurstTime: ");
scanf("%d", &burstTime[i]);
}
}
void sjfs() {
for(int i = 0; i < n; i++) {
for(int j = i + 1; j <=n; j++ ) {
if(burstTime[i] > burstTime[j]) {
temp1 = burstTime[i];
burstTime[i] = burstTime[j];
burstTime[j] = temp1;
strcpy(temp, pName[i]);
strcpy(pName[i], pName[j]);
strcpy(pName[j], temp);
}
}
}
}
void calculate() {
waitingTime[0] = 0;
turnAroundTime[0] = burstTime[0] + waitingTime[0];
exitTime[0] = burstTime[0];
totalTurnAroundTime = turnAroundTime[0];
totalWaitingTime = waitingTime[0];
for(int i = 1; i <= n; i++) {
waitingTime[i] = exitTime[i-1];
turnAroundTime[i] = waitingTime[i] + burstTime[i];
exitTime[i] = exitTime[i-1] + burstTime[i];
totalTurnAroundTime = totalTurnAroundTime + turnAroundTime[i];
totalWaitingTime = totalWaitingTime + waitingTime[i];
}
averageTurnAroundTime = (float) totalTurnAroundTime / n;
averageWaitingTime = (float) totalWaitingTime / n;
}
void display() {
printf("\nGantt Chart");
for(int i = 0; i <= n; i++ )
printf("|\t%s\t", pName[i]);
printf("|\t\n");
printf("\n------------------------------------------------------------\n");
printf("\n");
for(int i = 0; i <= n; i++)
printf("%d\t\t", exitTime[i]);
}