program: public class PetersonAlgorithm implements Runnable { static int flag[]
ID: 3804845 • Letter: P
Question
program:
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();
}
}
For the following code explain your implementation and address the following questions:
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
The above program for Peterson Algorithm doesn't implement Mutual Exclusion. For Mutual Exclusion the critical section must be thread safe i.e. Syncronised.
In any Language, concurrency raises interesting problems with respect to handling shared resources, and Java is no exception. Typically, operating systems provide facilities for creating mutual-exclusion semaphores or similar objects useful for serializing access to a shared resource. However, in Java, it is desirable to avoid calling machine or operating-system-specific code. Fortunately, the designers of Java have provided some facilities for basic mutual exclusion that, in turn, can be extended to meet more sophisticated requirements.
The mechanism used in Java to serialize access to objects is the synchronized method or the synchronized statement. Every object in Java has an associated lock, which is implicitly obtained by a thread when a synchronized method is called or when the synchronized statement is executed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.