操作系统peterson算法
Posted 九死九歌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了操作系统peterson算法相关的知识,希望对你有一定的参考价值。
一、简述
算法思想:双标志后检查法中,两个进程都争着想进入临界区,但是谁也不让谁,最后谁都无法进入临界区。Gary L. Peterson想到了一种方法,如果双方都争着想进入临界区,那可以让进程尝试“孔融让梨”,主动让对方先使用临界区。
Peterson算法用软件方法解决了进程互斥问题,遵循了空闲让进、忙则等待、有限等待三个原则,但是依然未遵循让权等待的原则。
二、代码实现
public class PeterSon implements Runnable {
private static int turn;
private static boolean[] flags = new boolean[2];
public static void main(String[] args) {
new Thread(new PeterSon(1), "T1").start();
new Thread(new PeterSon(0), "T0").start();
}
private final int thisId;
public PeterSon(int id) {
thisId = id;
}
public int otherId() {
return thisId == 0 ? 1 : 0;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 进入区
flags[thisId] = true;
turn = otherId();
while (flags[otherId()] && turn == otherId()) {
System.out.println(Thread.currentThread().getName() + " is waiting for thread scheduling.");
}
// 临界区
System.out.println(Thread.currentThread().getName() + " is in critical section.");
// 退出区
flags[thisId] = false;
// 剩余区
System.out.println(Thread.currentThread().getName() + " is in remainder section.");
}
}
}
三、运行结果
T0 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
T0 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T0 is in critical section.
T1 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
T0 is in remainder section.
T1 is in critical section.
T1 is in remainder section.
T0 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
Process finished with exit code -1
T0 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
T1 is in critical section.
T1 is in remainder section.
T0 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T1 is waiting for thread scheduling.
T0 is in critical section.
T0 is in remainder section.
T1 is waiting for thread scheduling.
T1 is in critical section.
T1 is in remainder section.
Process finished with exit code -1
很明显我们可以看出运行结果中不会出现两个线程同时进入临界区的现象,也就是T0 is in critical section.\\n T1 is in critical section.
的情况。
以上是关于操作系统peterson算法的主要内容,如果未能解决你的问题,请参考以下文章
在具有两个元素的领域中利用 SIMD 实现 Peterson 和 Monico 的 Lanczos 算法
操作系统 王道考研2019 第二章:进程管理 -- 进程同步进程互斥(临界资源)实现临界区互斥的基本方法(软(单/双 标志Peterson )硬件(中断屏蔽TSSwap))饥饿
Django 迁移失败 - 无法查询“peterson”:必须是“用户”实例