[Problem Link]
- Solution here is to add some `ordering` on the resources (i.e. forks) requested by the processes (i.e. the philosophers).
- Identify the resource id’s (or fork id’s) that a philosopher is surround with
- We make sure the process requests the `lower` id fork before the `higher` id fork to avoid deadlocks
- Identify which fork (left or right) is the lower id fork and request in that order (i.e. first left then right first right then left)
- Make sure mutex’s are held until forks are released
class DiningPhilosophers {
private static ReentrantLock[] mutex = new ReentrantLock[] {
new ReentrantLock(),
new ReentrantLock(),
new ReentrantLock(),
new ReentrantLock(),
new ReentrantLock(),
};
public DiningPhilosophers() {
}
// call the run() method of any runnable to execute its code
public void wantsToEat(int philosopher,
Runnable pickLeftFork,
Runnable pickRightFork,
Runnable eat,
Runnable putLeftFork,
Runnable putRightFork) throws InterruptedException {
int left = philosopher;
int right = (philosopher-1) == -1 ? 4 : philosopher-1;
if (Math.min(left, right) == left) {
DiningPhilosophers.mutex[left].lock();
DiningPhilosophers.mutex[right].lock();
pickLeftFork.run();
pickRightFork.run();
} else {
DiningPhilosophers.mutex[right].lock();
DiningPhilosophers.mutex[left].lock();
pickRightFork.run();
pickLeftFork.run();
}
eat.run();
putLeftFork.run();
putRightFork.run();
DiningPhilosophers.mutex[left].unlock();
DiningPhilosophers.mutex[right].unlock();
}
} |
class DiningPhilosophers {
private static ReentrantLock[] mutex = new ReentrantLock[] {
new ReentrantLock(),
new ReentrantLock(),
new ReentrantLock(),
new ReentrantLock(),
new ReentrantLock(),
};
public DiningPhilosophers() {
}
// call the run() method of any runnable to execute its code
public void wantsToEat(int philosopher,
Runnable pickLeftFork,
Runnable pickRightFork,
Runnable eat,
Runnable putLeftFork,
Runnable putRightFork) throws InterruptedException {
int left = philosopher;
int right = (philosopher-1) == -1 ? 4 : philosopher-1;
if (Math.min(left, right) == left) {
DiningPhilosophers.mutex[left].lock();
DiningPhilosophers.mutex[right].lock();
pickLeftFork.run();
pickRightFork.run();
} else {
DiningPhilosophers.mutex[right].lock();
DiningPhilosophers.mutex[left].lock();
pickRightFork.run();
pickLeftFork.run();
}
eat.run();
putLeftFork.run();
putRightFork.run();
DiningPhilosophers.mutex[left].unlock();
DiningPhilosophers.mutex[right].unlock();
}
}
Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт техники в волгограде
кондиционер ремонт
Patient pill facts. Drug resource available.
pharmacy technician online course
Find medication information. Comprehensive pill overview.
Pill overview available. Pill information provided.
buy proscar in ireland
Medication resource here. Medication essentials explained.
Medication effects explained. Pill effects explained.
buy proscar online australia
Medicine facts provided. Medication facts provided.
Drug details provided. Find medicine information.
cheap proscar online
Latest drug developments. Medication trends described.
Side effects explained. Comprehensive medication guide.
where to order proscar
Find medication information. Read about pills.
Drug brochure available. Recent medicine developments.
order proscar uk
Get pill details. Abuse effects detailed.
Find drug information. Current drug trends.
buy proscar 5 mg online
Administration guidelines here. Get pill facts.
Medication pamphlet available. Pill leaflet available.
buy merck proscar online
Comprehensive medicine resource. Medicine resource available.
Medicine overview available. Find medication facts.
buy proscar paypal
Drug trends described. Prescribing guidelines here.
Pill guide here. Drug pamphlet provided.
where to buy proscar uk
Latest drug developments. Find medication info.
Access pill information. Pill info available.
cheap proscar australia
Drug info here. Drug guide provided.
Medication data provided. Medication pamphlet available.
cheap proscar australia
Comprehensive medication facts. Pill facts provided.
Comprehensive medicine guide. Drug specifics here.
buy proscar australia
Medicine impacts described. Find medicine details.
Medicine guide here. Latest pill trends.
where can i buy proscar online
Comprehensive pill guide. Prescribing details available.
Latest medicine news. Patient drug resource.
buy proscar from india
Misuse consequences detailed. Medication impacts described.
Latest pill updates. Side effects listed.
where to order proscar
Drug leaflet available. Pill guide available.
Drug leaflet available. Access drug facts.
buy proscar tablets
Abuse effects detailed. Comprehensive pill resource.
Find drug details. Medication impacts described.
buy proscar canada
Latest drug news. Medication effects explained.
Latest medication developments. Active ingredients listed.
buy proscar india
Drug information available. Pill facts here.