⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.java

📁 页面置换算法 包括fifo 先进现出
💻 JAVA
字号:
package 操作系统.各个置换算法;

import javax.swing.*;

public class Clock {
	// 时钟页面置换算法Clock
	public static final int PROGRAMLEN = 12;
	public static final int PROGRAMNUM = 5;
	public static final int PAGELEN = 3;

	public static void main(String[] args) {

		int[] program = new int[PROGRAMLEN]; // program为要装入的程序
		for (int i = 0; i < PROGRAMLEN; i++) {
			program[i] = (int) (Math.random() * PROGRAMNUM + 1);
		}
		// int[] program = { 5, 5, 5, 5, 1, 5, 2, 3, 1, 3, 3, 4 };//------5
		// int[] program = { 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2 };// ------8
		// clock
		// int[] program = { 4, 3, 5, 5, 1, 5, 3, 4, 1, 1, 4, 2 };//------6
		// int[] program = { 3, 1, 3, 2, 5, 2, 3, 4, 1, 4, 3, 5 };// ------7
		for (int pro : program)
			System.out.print(" " + pro);
		System.out.println("");

		int[] page = new int[PAGELEN];
		for (int i = 0; i < PAGELEN; i++)
			page[i] = 0;

		int[] index = new int[PAGELEN];
		for (int i = 0; i < PAGELEN; i++)
			index[i] = 0;

		int lostPage = 0;
		int pointer = 0;
		for (int i = 0; i < PROGRAMLEN; i++) {
			int j = 0;
			for (j = 0; j < PAGELEN; j++)
				if (page[j] == program[i]) {
					index[j] = 1;
					break;
				}
			if (j < PAGELEN)
				continue;

			lostPage++;
			while (index[pointer] == 1) {
				index[pointer++] = 0;
				pointer %= PAGELEN;
			}
			page[pointer] = program[i];
			index[pointer++] = 1;
			pointer %= PAGELEN;
		}

		JOptionPane.showMessageDialog(null, "时钟页面置换算法(Clock)缺页 " + lostPage
				+ " 次");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -