目录
实验目的及要求
实验目的
掌握并实现互斥算法
实验要求
完成如下题目,并撰写实验报告。
实验环境(使用的软件)
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();
}
}
实验结论
原因同上