-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-ByArrayofStructures.c
134 lines (126 loc) · 4.87 KB
/
1-ByArrayofStructures.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Submitted By: Krishna Kanhaiya
Submitted To: Dr. Aditi Sharan
Course: M.Tech. Data Science (Semester-1)
Subject: Data Structures & Algorithms
Timestamp: January 06, 2023 17.38.05 IST
Help: Julfikaraehmad
Objective: Assignment
*/
#include<stdio.h>
struct Process{
int processID;
int burstTime;
int arrivalTime;
};
struct Process processQueue[5];
int print(int numberOfProcesses) {
int i;
printf("Process ID\tBurst Time\tArrival Time\n");
for(i = 0; i < numberOfProcesses; i++){
printf("%d\t\t%d\t\t%d\n", processQueue[i].processID, processQueue[i].burstTime, processQueue[i].arrivalTime);
}
return 0;
}
int insert(int processList[][2], int numberOfProcesses){
int i;
for(i = 0; i < numberOfProcesses; i++){
processQueue[i].processID = i;
processQueue[i].burstTime = processList[i][0];
processQueue[i].arrivalTime = processList[i][1];
}
return 0;
}
//This function is not finished
int deleteWithoutShifting(int numberOfProcesses){
int i, j, minProcessBurstTime, minProcessIndex, elapsedTime = 0;
struct Process process;
for(i = 0; i < numberOfProcesses; i++){
j = 0;
while((processQueue[j].burstTime == 0) || (processQueue[j].arrivalTime > elapsedTime)){
j++;
}
minProcessBurstTime = processQueue[j].burstTime;
minProcessIndex = j;
for(; j < numberOfProcesses; j++){
process = processQueue[j];
if((process.burstTime != 0) && ((process.arrivalTime <= elapsedTime) && (process.burstTime <= minProcessBurstTime))){
minProcessBurstTime = process.burstTime;
minProcessIndex = j;
}
}
elapsedTime += minProcessBurstTime;
printf("Min Process: {%d, %d, %d}\n", processQueue[minProcessIndex].processID, minProcessBurstTime, processQueue[minProcessIndex].arrivalTime);
printf("Time elapsed: %d\n", elapsedTime);
processQueue[minProcessIndex].burstTime = 0;
}
printf("There are no more processes available.\n");
return 0;
}
int delete(int numberOfProcesses){
int i, j, availableProcesses, currProcessArrivalTime, minProcessArrivalTime, minProcessBurstTime, minProcessIndex, elapsedTime = 0;
struct Process currProcess, minProcess;
availableProcesses = numberOfProcesses;
for(i = 0; i < numberOfProcesses; i++){
print(numberOfProcesses);
j = 0;
minProcess = processQueue[j];
minProcessIndex = j;
minProcessArrivalTime = minProcess.arrivalTime;
currProcessArrivalTime = minProcessArrivalTime;
while(currProcessArrivalTime > elapsedTime){
if(j + 1 < availableProcesses){
j++;
currProcessArrivalTime = processQueue[j].arrivalTime;
} else {
printf("No min process at time %d.\n", elapsedTime);
elapsedTime = minProcessArrivalTime;
currProcessArrivalTime = minProcessArrivalTime;
printf("Time elapsed: %d\n**************************************\n", elapsedTime);
print(numberOfProcesses);
}
if(currProcessArrivalTime < minProcessArrivalTime){
minProcess = processQueue[j];
minProcessIndex = j;
minProcessArrivalTime = currProcessArrivalTime;
}
}
j = minProcessIndex;
minProcessBurstTime = minProcess.burstTime;
for(; j < availableProcesses; j++){
currProcess = processQueue[j];
if((currProcess.arrivalTime <= elapsedTime) && (currProcess.burstTime <= minProcessBurstTime)){
minProcess = currProcess;
minProcessIndex = j;
minProcessBurstTime = minProcess.burstTime;
}
}
availableProcesses--;
elapsedTime += minProcessBurstTime;
printf("Min Process: {%d, %d, %d}\n", minProcess.processID, minProcessBurstTime, minProcess.arrivalTime);
printf("Time elapsed: %d\n**************************************\n", elapsedTime);
for(j = minProcessIndex; j < numberOfProcesses; j++){
processQueue[j] = (j + 1 != numberOfProcesses ? processQueue[j + 1] : minProcess);
}
}
print(numberOfProcesses);
printf("There are no more processes available.\n");
return 0;
}
int main(){
int numberOfProcesses = 5;
//List of processes is given as an array
int processList[][2] = {
//{BurstTime, ArrivalTime}
//Array index is treated as a Process ID
{2, 0},
{5, 5},
{10, 0},
{2, 3},
{4, 6}
};
insert(processList, numberOfProcesses);
//deleteWithoutShifting(numberOfProcesses);
delete(numberOfProcesses);
return 0;
}