-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpremprio.c
188 lines (168 loc) · 5.27 KB
/
premprio.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
void drawGanttChart();
void calculateProcessSequence();
int findAptProcessNumber(int);
#include<stdlib.h>
#include<stdio.h>
int numberOfProcesses,totalCPUBurstTime,*arrivalTime,*CPUBurstTime,*CPUBurstTimeCopy,*processNumber,minimumArrivalTime,*processSequenceForEachSecond,*waitingTime,*priority;
float averageWaitingTime,averageTurnAroundTime;int timeQuantum;
float averageWaitingTime=0;
/*arrays used to draw Gantt Chart*/
int *processNumberGantt,*CPUBurstTimeGantt,ganttSize;
int main()
{
int i,j,temp;
arrivalTime=(int*)malloc(sizeof(int)*numberOfProcesses);
CPUBurstTime=(int*)malloc(sizeof(int)*numberOfProcesses);
CPUBurstTimeCopy=(int*)malloc(sizeof(int)*numberOfProcesses);
processNumber=(int*)malloc(sizeof(int)*numberOfProcesses);
waitingTime=(int*)malloc(sizeof(int)*numberOfProcesses);
priority=(int*)malloc(sizeof(int)*numberOfProcesses);
minimumArrivalTime=2147483647;
FILE *fp;
fp=fopen("Input.txt","r");
fscanf(fp,"%d",&numberOfProcesses);
for(i=0;i<numberOfProcesses;i++) {
processNumber[i] = i;
fscanf(fp,"%d\t%d\t%d",&arrivalTime[i],&CPUBurstTime[i],&priority[i]);
}fscanf(fp,"%d",&timeQuantum);
fclose(fp);
for(i=0;i<numberOfProcesses;i++)
{
waitingTime[i]=0;
processNumber[i]=i;
/*printf("\nEnter the data for process number : %d\n",i);
printf("\n");
printf("Arrival Time : ");
scanf("%d",&arrivalTime[i]);
printf("CPU Burst time : ");
scanf("%d",&CPUBurstTime[i]);
printf("Priority : ");
scanf("%d",&priority[i]);*/
CPUBurstTimeCopy[i]=CPUBurstTime[i];
totalCPUBurstTime+=CPUBurstTime[i];
if(minimumArrivalTime>arrivalTime[i])
minimumArrivalTime=arrivalTime[i];
}
processSequenceForEachSecond=(int*)malloc(sizeof(int)*totalCPUBurstTime);
/* this function calculates the process sequence for each second. */
calculateProcessSequence();
/* printf("\nAverage Waiting Time = %f\n",averageWaitingTime);
printf("\nAverage Turn Around Time = %f\n\n",averageTurnAroundTime);
*/
printf("\n\n================================================ PREMPTIVE PRIORITY ====================================\n");
drawGanttChart();
}
void calculateProcessSequence()
{
int i,j,pNumber,prevProcess,tempCPUBurstTime,counter,prevProcesss;
for(i=minimumArrivalTime;i<totalCPUBurstTime + minimumArrivalTime;i++)
{
pNumber=findAptProcessNumber(i);
processSequenceForEachSecond[i-minimumArrivalTime]=pNumber;
CPUBurstTime[pNumber]--;
/*
claculating the waiting time of each process;
*/
for(j=0;j<numberOfProcesses;j++)
if(CPUBurstTime[j]!=0 && arrivalTime[j]<=i && j!=pNumber)
waitingTime[j]++;
}
/* claculating the size of gantt chart arrays*/
ganttSize=1;
prevProcess=processSequenceForEachSecond[0];
for(i=0;i<totalCPUBurstTime;i++)
{
if(prevProcess!=processSequenceForEachSecond[i])
ganttSize++;
prevProcess=processSequenceForEachSecond[i];
}
/* allocating the size of gantt chart arrays */
processNumberGantt=(int*)malloc(sizeof(int)*ganttSize);
CPUBurstTimeGantt=(int*)malloc(sizeof(int)*ganttSize);
/* assigning the values to Gantt Chart Arrays */
prevProcess=processSequenceForEachSecond[0];
tempCPUBurstTime=0;
counter=0;
for(i=0;i<totalCPUBurstTime;i++)
{
if(prevProcess!=processSequenceForEachSecond[i])
{
processNumberGantt[counter]=prevProcess;
CPUBurstTimeGantt[counter]=tempCPUBurstTime;
counter++;
tempCPUBurstTime=0;
}
tempCPUBurstTime++;
prevProcess=processSequenceForEachSecond[i];
}
CPUBurstTimeGantt[ganttSize-1]=tempCPUBurstTime;
processNumberGantt[ganttSize-1]=prevProcess;
/* calculating the averageWaitingTime and averageTurnAroundTime */
averageWaitingTime=0;
averageTurnAroundTime=0;
for(i=0;i<numberOfProcesses;i++)
{
averageWaitingTime+=waitingTime[i];
averageTurnAroundTime+=waitingTime[i]+CPUBurstTimeCopy[i];
}
averageWaitingTime=averageWaitingTime/(float)numberOfProcesses;
averageTurnAroundTime=averageTurnAroundTime/(float)numberOfProcesses;
}
int findAptProcessNumber(int currentTime)
{
int i,min=2147483647,pNumber;
for(i=0;i<numberOfProcesses;i++)
if(arrivalTime[i]<=currentTime && min>priority[i] && CPUBurstTime[i]!=0)
{
min=priority[i];
pNumber=i;
}
return pNumber;
}
void drawGanttChart()
{
const int maxWidth=100;
int scalingFactor,i,counter,tempi,currentTime;
printf("The gantt chart for the given processes is : \n\n");
scalingFactor=maxWidth/totalCPUBurstTime;
for(i=0;i<scalingFactor*totalCPUBurstTime+2+ganttSize;i++)
printf("-");
printf("\n|");
counter=0,tempi=0;
for(i=0;i<scalingFactor*totalCPUBurstTime;i++)
if(i==CPUBurstTimeGantt[counter]*scalingFactor+tempi)
{
counter++;
tempi=i;
printf("|");
}
else if(i==(CPUBurstTimeGantt[counter]*scalingFactor)/2+tempi)
printf("P%d",processNumberGantt[counter]);
else
printf(" ");
printf("|");
printf("\n");
for(i=0;i<scalingFactor*totalCPUBurstTime+2+ganttSize;i++)
printf("-");
printf("\n");
/* printing the time labels of the gantt chart */
counter=0;
tempi=0;
currentTime=minimumArrivalTime;
printf("%d",currentTime);
for(i=0;i<scalingFactor*totalCPUBurstTime;i++)
if(i==CPUBurstTimeGantt[counter]*scalingFactor+tempi)
{
tempi=i;
currentTime+=CPUBurstTimeGantt[counter];
averageWaitingTime+=currentTime;
counter++;
printf("%2d",currentTime);
}
else
{
printf(" ");
}
currentTime+=CPUBurstTimeGantt[counter];
printf("%2d\n\n",currentTime);
}