Threads - petersonproblem

P

ppp

Hi, I try to implement Peterson algorithm in java. That is very easy.
Here you are:


class Peterson {
private int turn;
private boolean[] interested = {false, false};

public Peterson(String name) {
super(name);
}

public void enterRegion(int process) {

int other = 1 - process;
interested[process] = true;
turn = process;

while (turn==process && interested[other]==true)
;// do nothing

}

public void leaveRegion(int process) {
interested[process] = false;
}
}

But it's hard to me use this class any how.

Should i create 2 Threads, and in this threads method run should use
methods leave/enter Region?

please help.
 
P

ppp

ppp napisal(a):
Hi, I try to implement Peterson algorithm in java. That is very easy.
Here you are:


class Peterson {
private int turn;
private boolean[] interested = {false, false};

public Peterson(String name) {
super(name);
}

public void enterRegion(int process) {

int other = 1 - process;
interested[process] = true;
turn = process;

while (turn==process && interested[other]==true)
;// do nothing

}

public void leaveRegion(int process) {
interested[process] = false;
}
}

But it's hard to me use this class any how.

Should i create 2 Threads, and in this threads method run should use
methods leave/enter Region?

please help.

i know the answer:
yes - that should I do.
 
P

Patricia Shanahan

ppp said:
ppp napisal(a):
Hi, I try to implement Peterson algorithm in java. That is very easy.
Here you are:


class Peterson {
private int turn;
private boolean[] interested = {false, false};

public Peterson(String name) {
super(name);
}

public void enterRegion(int process) {

int other = 1 - process;
interested[process] = true;
turn = process;

while (turn==process && interested[other]==true)
;// do nothing

}

public void leaveRegion(int process) {
interested[process] = false;
}
}

But it's hard to me use this class any how.

Should i create 2 Threads, and in this threads method run should use
methods leave/enter Region?

please help.

i know the answer:
yes - that should I do.

However, there are some subtle issues with the memory model that you
need to consider. See the thread "Peterson's Algorithm in java,
sequencial instruction execution ?" in this group.

Patricia
 
P

ppp

ok. Peterson algorithm is simple. I have found another - Lamport
algorithm.

http://en.wikipedia.org/wiki/Lamport's_bakery_algorithm

I want to implement this in my program for N threads. I'm wondering how
to do it.

can i "cut" this algorithm to 2 methods (like in Peterson) enter/leawe
Region? (in this way: lines 3-14 enterRegion(int i); line 16:
leaveRegion(int i)?
 
P

ppp

i can, if someone will search for the soluthion - here it is.


public class Lamport {
private static int N = 1; // howMenyThreads
private static int[] enter = new int[N];
private static int[] number = new int[N];

public void enterRegion(int i) {

enter = 1;
number = 1 + maximum(number);
enter = 0;
for (int j = 0; j < N; j++) {
while (enter[j] != 0) {
// wait until thread j receives its number
}
while ((number[j] != 0) && ((number[j]<number) ||
(number[j]==number && j<i) )) {
// wait until threads with smaller numbers or with the same
// number, but with higher priority, finish their work
}
}
}

public void leaveRegion(int i) {
number = 0;
}

private int maximum(int[] table) {
int result=table[0];

for (int i=0;i<table.length;i++) {
if (result<table)
result = table;
}
return result;
}
}

ppp napisal(a):
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top