程序代码:
《Main.java》
package mutual_exclusion;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Content content=new Content();
content.setSize(400, 400);
content.setLocation(400, 200);
content.setVisible(true);
content.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
《Content.java》
package mutual_exclusion;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;
import javax.swing.event.*;
public class Content extends JFrame {
/**
* @author Napster;YangXia;LiHaiwei
*/
private static final long serialVersionUID = 1L;
private static int THREADS;
private BorderLayout bor_layout, button_layout;
private JPanel panel, panel_button;
private JRadioButton radiobutton[];
private ButtonGroup buttongroup;
private JTextField textField;
private JButton button_ok;
private JTextArea text;
private JScrollPane scroll;
private Listener_Button listener_button;
private Listener_Ok listener_ok;
private int alg_ID;
public Content() {
super("进程互斥的模拟实现");
Container container = this.getContentPane();
bor_layout = new BorderLayout();
container.setLayout(bor_layout);
panel = new JPanel();
panel.setSize(300, 100);
button_layout = new BorderLayout();
panel.setLayout(button_layout);
panel.setVisible(true);
panel_button = new JPanel(new GridLayout(5, 1));
radiobutton = new JRadioButton[4];
textField = new JTextField();
textField.setEditable(true);
textField.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {}
@Override
public void focusGained(FocusEvent arg0) {
textField.setText("");
}
});
radiobutton[0] = new JRadioButton("Dekker");
radiobutton[1] = new JRadioButton("Peterson");
radiobutton[2] = new JRadioButton("Lamport");
radiobutton[3] = new JRadioButton("Eisenburg_Mcguire");
listener_button = new Listener_Button();
radiobutton[0].addActionListener(listener_button);
radiobutton[1].addActionListener(listener_button);
radiobutton[2].addActionListener(listener_button);
radiobutton[3].addActionListener(listener_button);
panel_button.add(radiobutton[0]);
panel_button.add(radiobutton[1]);
panel_button.add(radiobutton[2]);
panel_button.add(radiobutton[3]);
panel_button.add(textField);
buttongroup = new ButtonGroup();
buttongroup.add(radiobutton[0]);
buttongroup.add(radiobutton[1]);
buttongroup.add(radiobutton[2]);
buttongroup.add(radiobutton[3]);
button_ok = new JButton("启动");
button_ok.setSize(50, 50);
listener_ok = new Listener_Ok();
button_ok.addActionListener(listener_ok);
panel.add(panel_button, BorderLayout.NORTH);
panel.add(button_ok, BorderLayout.SOUTH);
text = new JTextArea();
scroll = new JScrollPane(text);
text.setEditable(false);
text.setAutoscrolls(true);
container.add(panel, BorderLayout.EAST);
container.add(scroll, BorderLayout.CENTER);
}
class Listener_Button implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getSource().equals(radiobutton[0])) {
alg_ID = 0;
textField.setEditable(false);
}
if (arg0.getSource().equals(radiobutton[1])) {
alg_ID = 1;
textField.setEditable(false);
}
if (arg0.getSource().equals(radiobutton[2])) {
alg_ID = 2;
textField.setEditable(true);
textField.setText("请输入线程数(大于2)");
}
if (arg0.getSource().equals(radiobutton[3])) {
alg_ID = 3;
textField.setEditable(true);
textField.setText("请输入线程数(大于2)");
}
}
}
class Listener_Ok implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try{
if (!textField.getText().toString().equals(""))
THREADS = Integer.valueOf(textField.getText().toString());
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "错误,请正确输入!");
}
switch (alg_ID) {
case 0:
Dekker();
break;
case 1:
Peterson();
break;
case 2:
Lamport();
break;
case 3:
Eisenburg_Mcguire();
break;
}
}
private void Dekker() {
// TODO Auto-generated method stub
new Thread(new Dekker(0, text), "Thread - 0").start();
new Thread(new Dekker(1, text), "Thread - 1").start();
}
private void Peterson() {
// TODO Auto-generated method stub
new Thread(new Peterson(0, text), "Thread - 0").start();
new Thread(new Peterson(1, text), "Thread - 1").start();
}
private void Lamport() {
// TODO Auto-generated method stub
for (int i = 0; i < THREADS; i++) {
new Thread(new Lamport(i, THREADS, text), "Thread - " + i)
.start();
}
}
private void Eisenburg_Mcguire() {
// TODO Auto-generated method stub
for (int i = 0; i < THREADS; i++) {
new Thread(new Eisenburg_Mcguire(i, THREADS, text), "Thread - "
+ i).start();
}
}
}
}
《Dekker.java》
package mutual_exclusion;
import javax.swing.JTextArea;
public class Dekker implements Runnable {
private static boolean[] interested = { false, false };
private static volatile int turn = -1;
private final int id;
private final JTextArea text;
public Dekker(int i, JTextArea text1) {
id = i;
text = text1;
text.setText("");
}
private int other() {
return id == 0 ? 1 : 0;
}
@Override
public void run() {
// TODO 自动生成的方法存根
//announce that I am interested in the resource
interested[id] = true;
//If other thread is also interested in the resource,I will not interested in it.
//then if it's turn to other thread ,wait until other thread is finished.
//If other thread has finished,then i will interested in it and enter the critical section
while (interested[other()]) {
if (turn == other()) {
interested[id] = false;
while (turn == other()) {
text.append("[" + id + "] - - Waiting...\n");
}
interested[id] = true;
}
}
/* critical section */
try {
text.append("[" + id + "] - - Working \n");
Thread.sleep(1000);
text.append("[" + id + "] - - Done!\n");
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/* Leaving the critical section */
//I have finished now and It's turn to others.
interested[id] = false;
turn = other();
}
}
《Peterson.java》
package mutual_exclusion;
import javax.swing.JTextArea;
public class Peterson implements Runnable {
private static boolean[] interested = { false, false };
private static volatile int turn = -1;
private final int id;
private final JTextArea text;
public Peterson(int i, JTextArea text1) {
id = i;
text = text1;
text.setText("");
}
private int other() {
return id == 0 ? 1 : 0;
}
@Override
public void run() {
//announce that I am interested in the resource
interested[id] = true;
//It means that other thread may has entered the critical section
turn = other();
//If other thread is also interested and it's turn to other thread
//wait until other thread is finished.
while (interested[other()] && turn == other()) {
text.append("[" + id + "] - - Waiting...\n");
}
//If other thread has finished,then i will interested in it and enter the critical section
/* critical section */
try {
text.append("[" + id + "] - - Working \n");
Thread.sleep(1000);
text.append("[" + id + "] - - Done!\n");
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/* Leaving the critical section */
interested[id] = false;
}
}
《Lamport.java》
package mutual_exclusion;
import java.util.concurrent.atomic.AtomicIntegerArray;
import javax.swing.JTextArea;
public class Lamport implements Runnable {
private static int threads;
// AtomicInteger:achieve atomization of the integer variable operations
// queue for threads in line.
private static AtomicIntegerArray queue;
// need this for non-atomic max selection
private static AtomicIntegerArray entering;
private final int id;
private final JTextArea text;
public Lamport(int i, int N, JTextArea text1) {
id = i;
text = text1;
text.setText("");
Lamport.threads = N;
queue = new AtomicIntegerArray(threads);
entering = new AtomicIntegerArray(threads);
for (int j = 0; j < threads; ++j) {
queue.set(j, 0);
entering.set(j, 0);
}
}
@Override
public void run() {
// TODO 自动生成的方法存根
entering.set(id, 1);// Want to enter
queue.set(id, max(queue) + 1); // Enter the queue
entering.set(id, 0);// Has entered
for (int i = 0; i < queue.length(); ++i) {
if (i != id) {
/*
* If at this time other thread wants to enter the queue,wait
* while other thread has entered the queue.
*/
while (entering.get(i) == 1) {
text.append("[" + id + "] - - Waiting...\n");
}
/*
* If at this time other thread has the high priority,wait for
* the other thread is finished.
*/
while (queue.get(i) != 0
&& (queue.get(id) > queue.get(i) || (queue.get(id) == queue
.get(i) && id > i))) {
text.append("[" + id + "] - - Waiting...\n");
}
}
}
/* critical section */
try {
text.append("[" + id + "] - - Working \n");
Thread.sleep(1000);
text.append("[" + id + "] - - Done!\n");
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/* Leaving the critical section */
queue.set(id, 0);
}
private int max(AtomicIntegerArray queue2) {
// TODO 自动生成的方法存根
int max = 0;
for (int i = 0; i < queue2.length(); i++) {
if (queue2.get(i) >= max) {
max = queue2.get(i);
}
}
return max;
}
}
《Eisenburg_Mcguire.java》
package mutual_exclusion;
import javax.swing.JTextArea;
public class Eisenburg_Mcguire implements Runnable {
private static int threads ;
private static enum states {
IDLE, WAITTING, ACTIVE
};
private static states flags[];
private static volatile int turn = 0;
private int index;
private final int id;
private final JTextArea text;
public Eisenburg_Mcguire(int i, int N,JTextArea text1) {
id = i;
text = text1;
text.setText("");
Eisenburg_Mcguire.threads=N;
flags=new states[threads];
for (index = 0; index < threads; ++index) {
flags[index] = states.IDLE;
}
}
@Override
public void run() {
// TODO 自动生成的方法存根
flags[id] = states.WAITTING;
/* scan processes from the one with the turn up to ourselves. */
/* repeat if necessary until the scan finds all processes idle */
index = turn;
do {
while (index != id) {
if (flags[index] != states.IDLE)
index = turn;
else
index = (index + 1) %threads;
}
/* now tentatively claim the resource */
flags[id] = states.ACTIVE;
/* find the first active process besides ourselves, if any */
index =0;
while (flags[index] != states.ACTIVE) {
index = (index + 1)%threads;
}
/* if there were no other active processes, AND if we have the turn
or else whoever has it is idle, then proceed. Otherwise, repeat
the whole sequence. */
text.append("[" + id + "] - - Waiting...\n");
} while (!(index == id && (turn == id || flags[turn] == states.IDLE)));
turn=id;
/* critical section */
try {
text.append("[" + id + "] - - Working \n");
Thread.sleep(1000);
text.append("[" + id + "] - - Done!\n");
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/* Leaving the critical section */
/* find a process which is not IDLE */
/* (if there are no others, we will find ourselves) */
index = (turn+1)%threads;
while (flags[index] == states.IDLE) {
index = (index+1)%threads;
}
/* give the turn to someone that needs it, or keep it */
turn = index;
/* we're finished now */
flags[id] = states.IDLE;
}
}
《操作系统原理》实验报告院(部):管理工程学院专业:信息管理与信息系统实验项目:实验一二三五班级:信管102姓名:学号:目录引言.…
西安郵電大學操作系统设计报告题目进程线程互斥锁院系名称计算机学院班级1104学生姓名赵大伟学号8位04113124指导教师舒新峰设…
操作系统课程论文院系班级姓名学号指导教师完成时间东莞理工学院摘要本文分析面向对象教学操作系统EOS的系统结构和代码构成通过源代码分…
操作系统课程设计实验报告姓名学号班级地点20xx年月日任务说明共完成四个任务任务一IO系统调用开销比较任务二实现一个简单的shel…
操作系统课程设计总结报告学期20xx20xx学年第2学期学院软件学院学号姓名20xx年7月3日本学期开设了操作系统课程主要学习了计…
《操作系统原理》实验报告院(部):管理工程学院专业:信息管理与信息系统实验项目:实验一二三五班级:信管102姓名:学号:目录引言.…
操作系统课程设计报告专业学号姓名提交日期操作系统课程设计报告设计目的1本实验的目的是通过一个简单多用户文件系统的设计加深理解文件系…
上海电力学院计算机操作系统原理课程设计报告题目名称编写程序模拟虚拟存储器管理姓名杜志豪学号20xx1798班级20xx053班同组…