Operating system Course design: To achieve synchronization between processes. Leave a message.
Import Java.util.concurrent.Semaphore; /******** Signal Volume *******/
Class Sign {
Final static int nums = 6; Number of processes
Final static int RUN = 1; Run out
Final static int STOP = 0; Blocking state
static int[] status = new Int[nums]; The state of the process is not running, blocking defaults to prohibit
Static semaphore s[] = null; Signal Volume
Final static int[] times = new Int[6]; Number of times to run
Static semaphore mutex = new semaphore (1);//Semaphore: Control access to critical areas
static {
s = new Semaphore[nums];
for (int i = 0; i < nums; i++) {
S[i] = new semaphore (1); Class
}
for (int j = 0; J < Times.length; J + +) {
TIMES[J] = 10; Each process executes 10 times and ends
}
}
}
/********* Thread ***********/
Class PROCESS6 implements Runnable {//Process thread
private int id;
public process6 (int id) {
Super ();
This.id = ID;
}
void test (int id) throws Interruptedexception {//test whether the current process can run
When you are not running, the precondition can be run when the preconditions are satisfied.
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) {//If process 1
Has run out, process 2 can run directly
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) {//If process 1
is turned on, process 3 can be run directly
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 {//If process 2 is turned on, process 4 can be run directly
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) {//If process 2, or 3 has run out, process 5 can run directly
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) {//If process 4 or 5 has run out, process 6 can run directly
Sign.s[5].release ();
sign.times[5]--;
System.out.println ("I am Thread P" + (ID + 1) + "... "
+ Sign.times[id]);
}
;
Break
}
}
void print () {//print status of each process
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 (); Print all process states first
Sign.mutex.acquire ();
System.out.println ("P" + ID + "is testing!");
Test (ID);
Sign.mutex.release ();
To determine the status of the current process running, blocking
Sign.s[id].acquire ();
Wake up other processes not running then block
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 (+ "front condition is not enough.") ");
Thread.CurrentThread (). Sleep ((100));
}
}
catch (Exception e) {
E.printstacktrace ();
}
}
}
public class SysV {
/**
* Task 3 process/thread synchronization
*/
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 ();
}
}