用html实现一个时间片轮转的进程调度算法,进程手动输入可修改。
时间: 2024-02-13 11:06:52 浏览: 31
以下是一个简单的使用HTML、CSS和JavaScript实现时间片轮转进程调度算法的示例。用户可以手动输入进程并修改其属性,然后点击“开始调度”按钮开始模拟进程调度。
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>时间片轮转进程调度算法</title>
<style>
table {
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid black;
padding: 5px;
}
th {
background-color: #ddd;
}
input[type="number"] {
width: 50px;
}
</style>
</head>
<body>
<h1>时间片轮转进程调度算法</h1>
<table>
<thead>
<tr>
<th>进程ID</th>
<th>到达时间</th>
<th>需要时间</th>
<th>剩余时间</th>
<th>优先级</th>
</tr>
</thead>
<tbody id="processes">
<tr>
<td><input type="text" value="P1"></td>
<td><input type="number" min="0" value="0"></td>
<td><input type="number" min="1" value="5"></td>
<td>5</td>
<td><input type="number" min="1" value="1"></td>
</tr>
<tr>
<td><input type="text" value="P2"></td>
<td><input type="number" min="0" value="1"></td>
<td><input type="number" min="1" value="3"></td>
<td>3</td>
<td><input type="number" min="1" value="2"></td>
</tr>
<tr>
<td><input type="text" value="P3"></td>
<td><input type="number" min="0" value="2"></td>
<td><input type="number" min="1" value="2"></td>
<td>2</td>
<td><input type="number" min="1" value="3"></td>
</tr>
</tbody>
</table>
<button id="start">开始调度</button>
<button id="reset">重置</button>
<p id="result"></p>
<script>
function run() {
const processes = Array.from(document.querySelectorAll("#processes tr")).map(tr => ({
id: tr.querySelector("input[type='text']").value,
arrivalTime: parseInt(tr.querySelector("input[type='number'][min='0']").value),
remainingTime: parseInt(tr.querySelector("td:nth-child(4)").textContent),
priority: parseInt(tr.querySelector("input[type='number'][min='1']").value),
originalPriority: parseInt(tr.querySelector("input[type='number'][min='1']").value)
}));
const quantum = 2;
let currentTime = 0;
let completedProcesses = 0;
let result = "";
while (completedProcesses < processes.length) {
let isCompleted = true;
for (const process of processes) {
if (process.remainingTime > 0) {
isCompleted = false;
if (process.remainingTime > quantum) {
process.remainingTime -= quantum;
currentTime += quantum;
} else {
currentTime += process.remainingTime;
process.remainingTime = 0;
process.completionTime = currentTime;
process.turnaroundTime = process.completionTime - process.arrivalTime;
process.waitingTime = process.turnaroundTime - process.originalPriority * process.completionTime;
completedProcesses++;
}
}
}
if (isCompleted) {
break;
}
}
result += "进程ID 到达时间 完成时间 周转时间 等待时间\n";
let totalTurnaroundTime = 0;
let totalWaitingTime = 0;
for (const process of processes) {
result += `${process.id} ${process.arrivalTime} ${process.completionTime} ${process.turnaroundTime} ${process.waitingTime}\n`;
totalTurnaroundTime += process.turnaroundTime;
totalWaitingTime += process.waitingTime;
}
const averageTurnaroundTime = totalTurnaroundTime / processes.length;
const averageWaitingTime = totalWaitingTime / processes.length;
result += `平均周转时间: ${averageTurnaroundTime.toFixed(2)}\n平均等待时间: ${averageWaitingTime.toFixed(2)}\n`;
document.querySelector("#result").textContent = result;
}
document.querySelector("#start").addEventListener("click", run);
document.querySelector("#reset").addEventListener("click", () => location.reload());
</script>
</body>
</html>
```
该代码使用一个包含所有进程信息的HTML表格,并包含一个“开始调度”按钮和一个“重置”按钮。进程信息包括进程ID、到达时间、需要时间、剩余时间和优先级,用户可以在表格中手动输入和修改这些信息。当用户点击“开始调度”按钮时,JavaScript代码将读取表格中的进程信息,并使用时间片轮转算法模拟进程调度过程。完成进程调度后,页面将显示每个进程的完成时间、周转时间和等待时间,以及平均周转时间和平均等待时间。当用户点击“重置”按钮时,页面将重新加载以清除所有进程信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)