java 实现进程间的同步(源代码)

来源:互联网 发布:2014网络伤感流行歌曲 编辑:程序博客网 时间:2024/05/29 04:45

操作系统课程设计:实现进程间的同步。 有问题留言


import java.util.concurrent.Semaphore;

/******** 信号量 *******/
class Sign {
final static int NUMS = 6; // 进程的数目
final static int RUN = 1; // 运行完
final static int STOP = 0; // 阻塞态
static int[] status = new int[NUMS]; // 进程的状态 不运行 则阻塞 默认为禁止
static Semaphore s[] = null; // 信号量
final static int[] times = new int[6];//运行的次数


static Semaphore mutex = new Semaphore(1);// 信号量:控制对临界区的访问


static {
s = new Semaphore[NUMS];
for (int i = 0; i < NUMS; i++) {
s[i] = new Semaphore(1); // 初始化
}
for (int j = 0; j < times.length; j++) {
times[j] = 10; // 每个进程执行10次则结束
}
}
}


/*********线程***********/
class process6 implements Runnable {// 进程线程
private int id;


public process6(int id) {
super();
this.id = id;
}


void test(int id) throws InterruptedException { // 测试当前进程是否可以运行
// 当自己没有运行,前置条件满足时 可以运行
switch (id) {
case 0:


if (Sign.times[0] > 0) {
Sign.s[0].release();
Sign.times[0]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 1:


if (Sign.status[0] == Sign.RUN && Sign.times[1] > 0) { // 如果进程1
// 已经运行完,进程2可以直接运行


Sign.s[1].release();
Sign.times[1]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 2:


if (Sign.status[0] == Sign.RUN && Sign.times[2] > 0) { // 如果进程1
// 已经开启,进程3可以直接运行


Sign.s[2].release();


Sign.times[2]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 3:


if (Sign.status[1] == Sign.RUN && Sign.times[3] > 0) { // 如果进程2已经开启,进程4可以直接运行


Sign.s[3].release();
Sign.times[3]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 4:


if (Sign.times[4] > 0
&& (Sign.status[1] == Sign.RUN || Sign.status[2] == Sign.RUN)) {// 如果进程2、或3已经运行完,进程5可以直接运行


Sign.s[4].release();
Sign.times[4]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 5:


if (Sign.times[5] > 0
&& (Sign.status[4] == Sign.RUN || Sign.status[3] == Sign.RUN)) {// 如果进程4或5已经运行完,进程6可以直接运行


Sign.s[5].release();
Sign.times[5]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
}
}


void print() {// 打印 每个进程的状态
for (int i = 0; i < Sign.NUMS; i++) {
System.out.println("Thread P" + (i + 1) + " :" + Sign.status[i]);
}
}


@Override
public void run() {
try {
while (true) {
// print(); //先打印全部进程状态
Sign.mutex.acquire();
// System.out.println("P" + id + " is testing!");
test(id);
Sign.mutex.release();
// 判断当前进程运行的状况,不能运行 则 阻塞
Sign.s[id].acquire();


// 唤醒其他进程 不能运行 则阻塞
if (Sign.times[id] == 0 || Sign.times[id] < 0) {
Sign.status[id] = Sign.RUN;
// System.out.println("P" + (id+1) + " run out!");


Sign.mutex.acquire();
test((id + 1) % 6);
test((id + 2) % 6);
test((id + 3) % 6);
test((id + 4) % 6);
test((id + 5) % 6);
Sign.mutex.release();


} else {
// System.out.println(+"前置条件不够!");
Thread.currentThread().sleep((100));
}
}
} catch (Exception e) {
e.printStackTrace();
}


}


}


public class SYSV {


/**
* 任务3 进程/线程同步
*/
public static void main(String[] args) {
new Thread(new process6(0)).start();
new Thread(new process6(1)).start();
new Thread(new process6(2)).start();
new Thread(new process6(3)).start();
new Thread(new process6(4)).start();
new Thread(new process6(5)).start();
}
}
原创粉丝点击