-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcfs.html
204 lines (165 loc) · 5.43 KB
/
fcfs.html
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html>
<head>
<title>FCFS Scheduler</title>
<style>
body {
background-color: black;
color: #ffd700;
font-family: Arial, sans-serif;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
margin-bottom: 50px;
}
.processes {
margin-top: 40px;
}
.process {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.process .pid {
background-color: #ffd700;
color: black;
border-radius: 3px;
padding: 5px 10px;
margin-right: 10px;
}
.process .burst-time, .process .arrival-time {
background-color: black;
color: #ffd700;
padding: 5px 10px;
margin-right: 10px;
}
.output {
margin-top: 40px;
text-align: center;
}
table {
margin-top: 20px;
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ffd700;
}
th {
background-color: #ffd700;
color: black;
}
.gantt-chart {
margin-top: 40px;
display: flex;
align-items: center;
justify-content: center;
}
.process-bar {
height: 40px;
background-color: #ffd700;
display: flex;
align-items: center;
justify-content: center;
margin-right: 10px;
}
.btn{
width:150px;
height:40px;
background-color: #ffd700;
color:black;
border-radius: 10px;
margin-left:10px;
font-size: 16px;
}
input{
width:150px;
height:30px;
}
</style>
</head>
<body>
<div class="container">
<h1>FIRST COME FIRST SERVE</h1>
<label for="num-processes">Number of Processes:</label>
<input type="number" id="num-processes">
<button onclick="createTable()" class="btn">Create Table</button>
<div id="process-table"></div>
<div class="output">
<button onclick="runFCFS()" class="btn">Run FCFS</button>
<p id="result"></p>
</div>
<div class="gantt-chart" id="gantt-chart"></div>
</div>
<script>
function createTable() {
const numProcessesInput = document.getElementById('num-processes');
const numProcesses = parseInt(numProcessesInput.value);
if (isNaN(numProcesses) || numProcesses <= 0) {
alert('Please enter a valid number of processes.');
return;
}
let tableHtml = '<table>';
tableHtml += '<tr><th>PID</th><th>Arrival Time</th><th>Burst Time</th></tr>';
for (let i = 0; i < numProcesses; i++) {
tableHtml += `<tr><td>P${i + 1}</td><td><input type="number" class="arrival-time-input"></td><td><input type="number" class="burst-time-input"></td></tr>`;
}
tableHtml += '</table>';
const processTableDiv = document.getElementById('process-table');
processTableDiv.innerHTML = tableHtml;
}
function runFCFS() {
const arrivalTimeInputs = document.getElementsByClassName('arrival-time-input');
const burstTimeInputs = document.getElementsByClassName('burst-time-input');
const processes = [];
for (let i = 0; i < arrivalTimeInputs.length; i++) {
const arrivalTime = parseInt(arrivalTimeInputs[i].value);
const burstTime = parseInt(burstTimeInputs[i].value);
if (isNaN(arrivalTime) || isNaN(burstTime) || arrivalTime < 0 || burstTime <= 0) {
alert('Please enter valid arrival and burst times for all processes.');
return;
}
processes.push({ pid: `P${i + 1}`, arrivalTime, burstTime });
}
processes.sort((a, b) => a.arrivalTime - b.arrivalTime);
let waitingTime = 0;
let totalWaitingTime = 0;
let completionTime = 0;
let totalTurnaroundTime = 0;
for (let i = 0; i < processes.length; i++) {
const process = processes[i];
if (process.arrivalTime > completionTime) {
completionTime = process.arrivalTime;
}
process.waitingTime = completionTime - process.arrivalTime;
process.completionTime = completionTime + process.burstTime;
process.turnaroundTime = process.completionTime - process.arrivalTime;
totalWaitingTime += process.waitingTime;
totalTurnaroundTime += process.turnaroundTime;
completionTime = process.completionTime;
}
const averageWaitingTime = totalWaitingTime / processes.length;
const averageTurnaroundTime = totalTurnaroundTime / processes.length;
const resultElement = document.getElementById('result');
resultElement.innerHTML = `Average Waiting Time: ${averageWaitingTime.toFixed(2)}<br>Average Turnaround Time: ${averageTurnaroundTime.toFixed(2)}`;
generateGanttChart(processes);
}
function generateGanttChart(processes) {
const ganttChartDiv = document.getElementById('gantt-chart');
let ganttChartHtml = 'Gantt <br> Chart : ';
for (let i = 0; i < processes.length; i++) {
const process = processes[i];
ganttChartHtml += `<div class="process-bar" style="width: ${process.burstTime * 20}px; color:black">${process.pid}</div>`;
}
ganttChartDiv.innerHTML = ganttChartHtml;
}
</script>
</body>
</html>