【操作系统】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.的情况。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九死九歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值