[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.
Find drug information. Patient drug info.
buy avodart for hair loss
Get drug info. Pill guide available.
Medication guide here. Access drug details.
buy avodart for hairloss
Comprehensive medication overview. Drug details provided.
Comprehensive drug resource. Drug resource available.
where to buy avodart in canada
Drug brochure available. Get drug details.
Patient medicine guide. Medicine impacts explained.
where can i buy avodart
Medication leaflet provided. Abuse effects detailed.
Pill information provided. Misuse consequences detailed.
best place to buy avodart
Medicine impacts described. Find drug information.
Find drug information. Patient medicine guide.
can you buy avodart over the counter
Get pill facts. Medication impacts explained.
Drug impacts explained. Pill leaflet available.
buy avodart online canada
Medicine facts available. Find medicine information.
Side effects explained. Medicine effects explained.
buy avodart dutasteride
Comprehensive pill resource. Drug facts here.
Access medication details. Access drug data.
cheapest generic avodart
Drug information here. Find medicine information.
Overdose effects detailed. Patient medicine resource.
where can i buy avodart
Access medication facts. Medication guide here.
Drug specifics here. Get pill details.
buy avodart
Drug leaflet available. Get medicine details.
Comprehensive pill guide. Drug leaflet here.
order avodart online
Medicine impacts explained. Access medication details.
Pill leaflet provided. Medication information here.
buy ed medications pills online
Comprehensive drug facts. Comprehensive medicine facts.
Drug guide available. Find drug information.
buy ed drugs
Latest drug developments. Pill facts provided.
Misuse consequences detailed. Latest drug developments.
purchase ed medications online no prescription
Comprehensive medication guide. Access drug data.
Pill effects explained. Drug specifics here.
purchase ed drugs
Find drug details. Get pill facts.
Comprehensive drug overview. Medication impacts explained.
buy ed medications with no prescription
Find medicine information. Find medication info.
Medication details here. Generic names listed.
ed medications online
Latest pill trends. Drug reactions explained.
Latest medicine developments. Latest medication developments.
buy ed drugs pills
Comprehensive pill guide. Medication effects explained.
Medication impacts described. Medicine effects explained.
ed drugs
Read about medications. Interactions explained here.
Medication impacts explained. Patient drug info.
buy ed medications uk
Current drug trends. Current medication trends.
Полезный сервис быстрого загона ссылок сайта в индексация поисковой системы – быстрая индексация ссылок
Если вы искали где отремонтировать сломаную технику, обратите внимание – профи ремонт