-
Notifications
You must be signed in to change notification settings - Fork 1
/
fcfs.c
130 lines (115 loc) · 2.78 KB
/
fcfs.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
void drawGanttChart();
#include<stdlib.h>
#include<stdio.h>
int numberOfProcesses,totalCPUBurstTime,*arrivalTime,*CPUBurstTime,*processNumber,minimumArrivalTime;
float averageWaitingTime=0;
int main()
{
int i,j,temp;
printf("Enter the number of processes : ");
scanf("%d",&numberOfProcesses);
arrivalTime=(int*)malloc(sizeof(int)*numberOfProcesses);
CPUBurstTime=(int*)malloc(sizeof(int)*numberOfProcesses);
processNumber=(int*)malloc(sizeof(int)*numberOfProcesses);
minimumArrivalTime=2147483647;
for(i=0;i<numberOfProcesses;i++)
{
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]);
totalCPUBurstTime+=CPUBurstTime[i];
if(minimumArrivalTime>arrivalTime[i])
minimumArrivalTime=arrivalTime[i];
}
/*
implementing bubble sort algorithm
*/
for(i=1;i<numberOfProcesses;i++)
{
for(j=1;j<numberOfProcesses-1;j++)
{
if(arrivalTime[j]>arrivalTime[j+1])
{
temp=arrivalTime[j];
arrivalTime[j]=arrivalTime[j+1];
arrivalTime[j+1]=temp;
temp=CPUBurstTime[j];
CPUBurstTime[j]=CPUBurstTime[j+1];
CPUBurstTime[j+1]=temp;
temp=processNumber[j];
processNumber[j]=processNumber[j+1];
processNumber[j+1]=temp;
}
}
}
for(i=0;i<numberOfProcesses;i++)
{
printf("P%d -> %d,%d\n",processNumber[i],arrivalTime[i],CPUBurstTime[i]);
}
drawGanttChart();
}
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+numberOfProcesses;i++)
{
printf("-");
}
printf("\n|");
counter=0,tempi=0;
for(i=0;i<scalingFactor*totalCPUBurstTime;i++)
{
if(i==CPUBurstTime[counter]*scalingFactor+tempi)
{
counter++;
tempi=i;
printf("|");
}
else if(i==(CPUBurstTime[counter]*scalingFactor)/2+tempi)
{
printf("P%d",processNumber[counter]);
}
else
{
printf(" ");
}
}
printf("|");
printf("\n");
for(i=0;i<scalingFactor*totalCPUBurstTime+2+numberOfProcesses;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==CPUBurstTime[counter]*scalingFactor+tempi)
{
tempi=i;
currentTime+=CPUBurstTime[counter];
averageWaitingTime+=currentTime;
counter++;
printf("%2d",currentTime);
}
else
{
printf(" ");
}
}
currentTime+=CPUBurstTime[counter];
printf("%2d\n\n",currentTime);
averageWaitingTime=averageWaitingTime/numberOfProcesses;
printf("Average waiting Time : %f\n",averageWaitingTime);
}