作業系統課程設計:實現進程間的同步。 有問題留言
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();
}
}