操作系统——互斥算法的实现

目录

实验目的及要求

实验目的

实验要求

实验环境(使用的软件)

实验内容

实验原理

实验过程

试验

试验代码

试验结论

​编辑

Dekker互斥算法

实验代码

实验结论

面包店互斥算法

实验代码

实验结论

Peterson互斥算法

实验代码

实验结论

Eisenberg_Meguire算法

实验代码

实验结论


实验目的及要求

实验目的

掌握并实现互斥算法

实验要求

完成如下题目,并撰写实验报告。

实验环境(使用的软件)

Eclipse

实验内容

本实验在Eclipse环境下,用Java语言编程实现互斥算法。

实验原理

基于互斥算法的思想用java语言编程实现。

实验过程

试验

试验代码

package try1;

public class hard {

	static int x = 1;

	public static void main(String[] args) {

		Runnable runA = new Runnable() {

			public void run() {

				if (x >= 1) {

					try {

						Thread.sleep(100);

					} catch (InterruptedException e) {

						e.printStackTrace();

					}

					x--;

					System.out.println("in threadA the only book is borrowed!");

					System.out.println("in threadA x is" + x);

				}

				else

					System.out.println("in threadA no book is left and book can not be borrowed!");

			}

		};

		Thread threadA = new Thread(runA, "threadA");

		threadA.start();

		Runnable runB = new Runnable() {

			public void run() {

				if (x >= 1) {

					try {

						Thread.sleep(100);

					} catch (InterruptedException e) {

						e.printStackTrace();

					}

					x--;

					System.out.println("in threadB the only book is borrowed!");

					System.out.println("in threadB x is " + x);

				}

				else

					System.out.println("in threadB no book is left and book can not be borrowed!");

			}

		};

		Thread threadB = new Thread(runB, "threadB");

		threadB.start();

	}

}

试验结论

在进行多次运行后,我发现所有的结果均为以上三个的其中一种。原因分析:Thread.sleep函数的使用导致进程休眠100ms,这使得共享变量还未发生变化就通过if条件进入临界区,A进程和B进程谁先执行并不确定,导致了多个进程同时进入临界区,破坏了互斥性原则。

Dekker互斥算法

实验代码

public class hard1 {

	static int[] flag = new int[] { 0, 0 };

	static int turn = 0;

	static int x = 1;

	public static void main(String[] args) {
		Runnable runA = new Runnable() {
			public void run() {

				flag[0] = 1;
				while (flag[1] == 1) {
					if (turn == 1) {
						flag[0] = 0;
						while (turn == 1) {
						}
						;
						flag[0] = 1;
					}
				}

				if (x >= 1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					x--;
					System.out.println("in threadA the only book is borrowed!");
					System.out.println("in threadA x is " + x);
				} else
					System.out.println("in threadA no book is left and book can not be borrowed!");

				turn = 1;
				flag[0] = 0;

			}
		};
		Thread threadA = new Thread(runA, "threadA");
		threadA.start();

		Runnable runB = new Runnable() {
			public void run() {

				flag[1] = 1;
				while (flag[0] == 1) {
					if (turn == 0) {
						flag[1] = 0;
						while (turn == 0) {
						}
						;
						flag[1] = 1;
					}
				}

				if (x >= 1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					x--;
					System.out.println("in threadB the only book is borrowed!");
					System.out.println("in threadB x is " + x);
				} else
					System.out.println("in threadB no book is left and book can not be borrowed!");

				turn = 1;
				flag[0] = 0;

			}
		};
		Thread threadB = new Thread(runB, "threadB");
		threadB.start();
	}
}

实验结论

原因:

Dekker互斥算法实现了进程的互斥,使得A进程先执行,实现了A的借书。

面包店互斥算法

实验代码

package try1;

public class hard2 {
	static int[] choosing = new int[] { 0, 0 };
	static int[] number = new int[] { 0, 0 };
	static int x = 1;

	public static void main(String[] args) {
		Runnable runA = new Runnable() {
			public void run() {
				choosing[0] = 1;
				number[0] = max(number[0], number[1]) + 1;
				choosing[0] = 0;
				for (int j = 0; j < 2; j++) {
					while (choosing[j] == 1) {
					}
					;

					while ((number[j] == 0) && ((number[j] < number[0]) || (number[j] == number[0] && j < 0))) {
					}
					;
				}
				if (x >= 1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					x--;
					System.out.println("in threadA the only book is borrowed!");
					System.out.println("in threadA x is " + x);
				} else
					System.out.println("in threadA no book is left and book can not be borrowed!");
				number[0] = 0;
			}

			private void While(boolean b) {

			}

			private int max(int i, int j) {

				return 0;
			}
		};
		Thread threadA = new Thread(runA, "threadA");
		threadA.start();

		Runnable runB = new Runnable() {
			public void run() {

				choosing[1] = 1;
				number[1] = max(number[0], number[1]) + 1;
				choosing[1] = 0;
				for (int j = 0; j < 2; j++) {
					while (choosing[j] == 1) {
					}
					;

					while ((number[j] != 0) && ((number[j] < number[1]) || (number[j] == number[1] && j < 1))) {
					}
					;
				}

				if (x >= 1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					x--;
					System.out.println("in threadB the only book is borrowed!");
					System.out.println("in threadB x is " + x);
				} else
					System.out.println("in threadB no book is left and book can not be borrowed!");

				number[1] = 0;

			}

			private int max(int i, int j) {

				return 0;
			}

			private void While(boolean b) {

			}
		};
		Thread threadB = new Thread(runB, "threadB");
		threadB.start();
	}
}

实验结论

原因同上

Peterson互斥算法

实验代码

public class hard3 {

	static int[] flag = new int[] { 0, 0 };
	static int turn = 1;

	static int x = 1;

	public static void main(String[] args) {
		Runnable runA = new Runnable() {
			public void run() {

				flag[0] = 1;
				turn = 1;
				while (flag[1] == 1 && turn == 1)
					;

				if (x >= 1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					x--;
					System.out.println("in threadA the only book is borrowed!");
					System.out.println("in threadA x is " + x);
				} else
					System.out.println("in threadA no book is left and book can not be borrowed!");

				flag[0] = 0;

			}
		};
		Thread threadA = new Thread(runA, "threadA");
		threadA.start();

		Runnable runB = new Runnable() {
			public void run() {

				flag[1] = 1;
				turn = 0;
				while (flag[0] == 1 && turn == 0)
					;

				if (x >= 1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					x--;
					System.out.println("in threadB the only book is borrowed!");
					System.out.println("in threadB x is " + x);
				} else
					System.out.println("in threadB no book is left and book can not be borrowed!");

				flag[1] = 0;

			}
		};
		Thread threadB = new Thread(runB, "threadB");
		threadB.start();
	}
}

实验结论

原因同上

Eisenberg_Meguire算法

实验代码

public class hard4 {

	static String[] flag = { "idle", "idle", "idle" };

	static int turn = 0;
	static int j = 0;

	static int x = 1;

	public static void main(String[] args) {
		Runnable runA = new Runnable() {
			public void run() {

				do {
					flag[0] = "want_in";
					j = turn;
					while (j != 0) {
						if (flag[j] != "idle") {
							j = turn;
						} else
							j = (j + 1) % 2;
					}
					flag[0] = "in_cs";
					j = 0;
					while ((j < 2) && (j == 0 || flag[j] != "in_cs")) {
						j++;
					}
				} while (j != 2);
				turn = 0;

				if (x >= 1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					x--;
					System.out.println("in threadA the only book is borrowed!");
					System.out.println("in threadA x is " + x);
				} else
					System.out.println("in threadA no book is left and book can not be borrowed!");

				j = (turn + 1) % 2;
				while (flag[j] == "idle")
					j = (j + 1) % 2;
				turn = j;
				flag[0] = "idle";

			}
		};
		Thread threadA = new Thread(runA, "threadA");
		threadA.start();

		Runnable runB = new Runnable() {
			public void run() {

				do {
					flag[1] = "want_in";
					j = turn;
					while (j != 1) {
						if (flag[j] != "idle") {
							j = turn;
						} else
							j = (j + 1) % 2;
					}
					flag[1] = "in_cs";
					j = 0;
					while ((j < 2) && (j == 1 || flag[j] != "in_cs")) {
						j++;
					}
				} while (j != 2);
				turn = 1;

				if (x >= 1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					x--;
					System.out.println("in threadB the only book is borrowed!");
					System.out.println("in threadB x is " + x);
				} else
					System.out.println("in threadB no book is left and book can not be borrowed!");

				j = (turn + 1) % 2;
				while (flag[j] == "idle")
					j = (j + 1) % 2;
				turn = j;
				flag[1] = "idle";

			}
		};
		Thread threadB = new Thread(runB, "threadB");
		threadB.start();
	}
}

实验结论

原因同上

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

PawnTz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值