Write a program to implement the deadlock detection algorithm with multiple reso
ID: 3825205 • Letter: W
Question
Write a program to implement the deadlock detection algorithm with multiple resources
of each type. Your program should read from a file the following inputs: the
number of processes, the number of resource types, the number of resources of each
type in existence (vector E ), the current allocation matrix C (first row, followed by the
second row, and so on), the request matrix R (first row, followed by the second row,
and so on). The output of your program should indicate whether there is a deadlock in
the system. In case there is, the program should print out the identities of all processes
that are deadlocked. (In Java)
Explanation / Answer
private static void test1() {
final Object lock1 = new Object();
final Object lock2 = new Object();
Thread t1 = new Thread(new Runnable() {
@Override public void run() {
synchronized (lock1) {
System.out.println("Thread1 acquired lock1");
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException ignore) {}
synchronized (lock2) {
System.out.println("Thread1 acquired lock2");
}
}
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override public void run() {
synchronized (lock2) {
System.out.println("Thread2 acquired lock2");
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException ignore) {}
synchronized (lock1) {
System.out.println("Thread2 acquired lock1");
}
}
}
});
t2.start();
// Wait a little for threads to deadlock.
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException ignore) {}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.