Java 显示一个简单的信号量死锁示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24251383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing a simple example of deadlock with semaphores
提问by user3736114
I'm currently taking Operating Systems and our teacher assigned this problem for our lab but he's not very helpful. So I need to show a basic example of deadlock with semaphores and my output needs to demonstrate the occurence of the deadlock. I'm assuming he means if my exception is caught. This is as close as I've gotten.
我目前正在学习操作系统,我们的老师为我们的实验室分配了这个问题,但他的帮助不大。所以我需要展示一个带有信号量的死锁的基本例子,我的输出需要证明死锁的发生。我假设他的意思是如果我的异常被捕获。这和我得到的一样接近。
import java.util.concurrent.Semaphore;
public class deadlockTest2
{
private Semaphore sem1=new Semaphore(1);
private Semaphore sem2=new Semaphore(1);
private int num;
public deadlockTest2(int random)
{
num=random;
}
public void run()
{
try
{
sem1.acquire();
}
catch (InterruptedException e)
{
System.out.println("I am deadlocked");}
}
public static void main(String[] args)
{
deadlockTest2 tester=new deadlockTest2(5);
deadlockTest2 tester2=new deadlockTest2(20);
tester.run();
tester2.run();
}
}
~
~
Am I even close? I keep reading material but not fully grasping it. I think I don't understand what a process is. Someone please help.
〜
〜
上午我还差得远?我一直在阅读材料,但没有完全掌握它。我想我不明白什么是过程。有人请帮忙。
回答by ewolfers
A deadlock occurs when two or more processes all block one another from completing execution. You can think of a process as just a separate program; they run concurrently with your other processes.
当两个或多个进程都相互阻止完成执行时,就会发生死锁。您可以将进程视为一个单独的程序;它们与您的其他进程同时运行。
A basic example of a deadlock is a program with two processes and two mutexes, where each process needs access to both mutexes but does not release it's own mutex until it acquires the other mutex. An example:
死锁的一个基本示例是具有两个进程和两个互斥锁的程序,其中每个进程都需要访问这两个互斥锁,但在获取另一个互斥锁之前不会释放自己的互斥锁。一个例子:
- Process A locks Mutex A
- Process B locks Mutex B
- Process A wants to lock Mutex B but has already been locked by Process B!
- Process B wants to lock Mutex A but has already been locked by Process A!
- 进程 A 锁定互斥锁 A
- 进程 B 锁定互斥体 B
- 进程 A 想要锁定互斥锁 B 但已经被进程 B 锁定!
- 进程 B 想要锁定互斥锁 A 但已经被进程 A 锁定!
Note: A mutex is simply a semaphore with a value of 1.
注意:互斥锁只是一个值为 1 的信号量。
It's easy to see this will continue forever, since neither process will release their own resource before locking the other resource. These two processes are in deadlock.
很容易看出这将永远持续下去,因为在锁定另一个资源之前,两个进程都不会释放自己的资源。这两个进程处于死锁状态。
The first problem I see with your implementation is that you're giving each process a set of their OWN semaphores, this won't work because you need these semaphores to be shared between the processes. It's as if you did this:
我在您的实现中看到的第一个问题是,您为每个进程提供了一组它们自己的信号量,这是行不通的,因为您需要在进程之间共享这些信号量。就好像你这样做了:
- Process A locks Mutex A_A (exists in Process A)
- Process B locks Mutex B_B (exists in Process B)
- Process A locks Mutex B_A (exists in Process A)
- Process B locks Mutex A_B (exists in Process B)
- 进程A锁定互斥锁A_A(存在于进程A中)
- 进程B锁定互斥体B_B(存在于进程B中)
- 进程A锁定互斥体B_A(存在于进程A中)
- Process B 锁定 Mutex A_B(存在于 Process B 中)
So it's as if you had FOUR seperate semaphores (each process has one Mutex A and one Mutex B) instead of the intended TWO.
所以就好像你有四个单独的信号量(每个进程有一个互斥量 A 和一个互斥量 B)而不是预期的两个。
What you want should be more along the lines of:
你想要的应该更多的是:
Semaphore sem1 = new Semaphore(1);
Semaphore sem2 = new Semaphore(1);
public class deadlockTest1 implements Runnable
{
public void run()
{
sem1.acquire();
Thread.sleep(1000);
sem2.acquire();
}
}
public class deadlockTest2 implements Runnable
{
public void run()
{
sem2.acquire();
Thread.sleep(1000);
sem1.acquire();
}
}
Then in your main function:
然后在您的主要功能中:
public static void main(String[] args)
{
deadlockTest1 tester1 = new deadlockTest1();
deadlockTest2 tester2 = new deadlockTest2();
tester1.run();
tester2.run();
}
This should do exactly what I described in my first example. When each process is first run, they will each successfully acquire their own mutexes (A lock sem1, B lock sem2) then they will sleep for 1 second. On wake up they will each try to lock the other mutex (A try lock sem2, B try lock sem1), but since those resources were never released by their respective processes, they cannot be acquired and so these two processes will block indefinitely.
这应该完全符合我在第一个示例中的描述。当每个进程第一次运行时,它们都会成功获取自己的互斥锁(A 锁 sem1,B 锁 sem2),然后它们将休眠 1 秒。唤醒时,它们将各自尝试锁定另一个互斥锁(A try lock sem2,B try lock sem1),但由于这些资源从未被各自的进程释放,因此无法获取它们,因此这两个进程将无限期阻塞。