write a java program for the following assignment. Assignment: Use threads to im
ID: 3804254 • Letter: W
Question
write a java program for the following assignment.
Assignment:
Use threads to implement Peterson's algorithm for mutual exclusion. Demonstrate that the algorithm works using two threads. One of the threads should continually print "a". The other thread should continually print "b". Each time a thread prints, it should update a character counter (which is protected using Peterson's algorithm). After 30 characters have been printed, the thread that prints next should print a new line and reset the character counter to 0. All access to the character counter should reside in a critical section protected by Peterson's algorithm. All code that does not require mutual exclusion should reside outside of critical sections.
Note: You must explicitly use threads. You must also explicitly implement Peterson's algorithm for mutual exclusion. Finding some language that implements threads, the algorithm (or general mutual exclusion), as a built-in function of some command/commands will not be acceptable.
Finally, you will write a report explaining your implementation and addressing the following question:
Does Peterson’s Algorithm properly implement Mutual Exclusion of the critical code section?
Be sure to address each of the six requirements for Mutual Exclusion.
1. Mutual exclusion must be enforced: Only one process at a time is allowed into its critical section, among all processes that have critical sections for the same resource or shared object.
2. A process that halts in its noncritical section must do so without interfering with other processes.
3. It must not be possible for a process requiring access to a critical section to be delayed indefinitely: no deadlock or starvation.
4. When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter without delay.
5. No assumptions are made about relative process speeds or number of processors.
6. A process remains inside its critical section for a finite time only.
Explanation / Answer
public class PetersonAlgorithm implements Runnable {
static int flag[] = new int[2];
static int turn, counter;
private int processID;
private char character;
public PetersonAlgorithm(int processID, char character) {
this.character = character;
this.processID = processID;
}
@Override
public void run() {
while (true) {
this.getLock(processID);
this.criticalSection(character);
this.releaseLock(processID);
}
}
public void getLock(int processID) {
flag[processID] = 1;
turn = 1 - processID;
while (flag[1-processID]==1 && turn == 1-processID) {
try { Thread.sleep(1); } catch (InterruptedException e) {}
}
}
public void releaseLock(int processID) {
flag[processID] = 0;
}
public void criticalSection(char character) {
System.out.print(character + " ");
counter++;
if (counter == 30) {
System.out.println();
counter = 0;
}
}
public static void main(String args[]) {
PetersonAlgorithm processOne = new PetersonAlgorithm(0, 'a');
PetersonAlgorithm processTwo = new PetersonAlgorithm(1, 'b');
Thread threadOne = new Thread(processOne);
Thread threadTwo = new Thread(processTwo);
threadOne.start();
threadTwo.start();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.