显示带有信号量的死锁的简单示例 [英] Showing a simple example of deadlock with semaphores

查看:142
本文介绍了显示带有信号量的死锁的简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用操作系统,我们的老师为我们的实验室分配了此问题,但他的帮助不是很大。因此,我需要显示带有信号量的死锁的基本示例,并且我的输出需要演示死锁的发生。我假设他的意思是如果发现我的例外情况。距离我已经近了。

  import java.util.concurrent.Semaphore; 
public class deadlockTest2
{
private Semaphore sem1 = new Semaphore(1);
private Semaphore sem2 = new Semaphore(1);
私人整数;

public deadlockTest2(随机随机)
{
num = random;
}
public void run()
{
try
{
sem1.acquire();
}
捕获(InterruptedException e)
{
System.out.println(我陷入僵局);}
}

公共静态无效main(String [] args)
{
deadlockTest2 tester = new deadlockTest2(5);
deadlockTest2 tester2 = new deadlockTest2(20);
tester.run();
tester2.run();
}
}





我什至关门了吗?我一直在阅读材料,但没有完全掌握它。我想我不明白什么是过程。

解决方案

当两个或多个进程相互阻塞而无法完成执行时,就会发生死锁。您可以将流程视为一个单独的程序。它们与您的其他进程同时运行。



一个死锁的基本示例是具有两个进程和两个互斥量的程序,其中每个进程都需要访问两个互斥量,但不需要释放它自己的互斥锁,直到获得另一个互斥锁为止。示例:


  1. 进程A锁定Mutex A

  2. 进程B锁定Mutex B

  3. 进程A要锁定互斥锁B,但已被进程B锁定!

  4. 进程B要锁定互斥锁,但已被进程A锁定!

注意:互斥量只是一个值为1的信号量。



很容易看到这种情况将永远持续下去,因为在锁定另一个资源之前,两个进程都不会释放自己的资源。这两个进程陷入僵局。



我在实现中遇到的第一个问题是,您为每个进程提供了一组自己的OWN信号量,这是行不通的因为您需要在进程之间共享这些信号灯。就像您这样做一样:


  1. 进程A锁定Mutex A_A(存在于进程A中)

  2. 进程B锁定Mutex B_B(存在于进程B中)

  3. 进程A锁定Mutex B_A(存在于进程A中)

  4. 进程B锁定Mutex A_B (存在于进程B中)

因此,就好像您有四个单独的信号灯一样(每个进程有一个Mutex A和一个Mutex B)



您想要的应该是:

 信号量sem1 =新信号量(1); 
信号量sem2 =新信号量(1);

公共类deadlockTest1实现了Runnable
{
public void run()
{
sem1.acquire();
Thread.sleep(1000);
sem2.acquire();
}
}

公共类deadlockTest2实现可运行
{
public void run()
{
sem2.acquire( );
Thread.sleep(1000);
sem1.acquire();
}
}

然后在您的主函数中:

  public static void main(String [] args)
{
deadlockTest1 tester1 = new deadlockTest1();
deadlockTest2 tester2 =新的deadlockTest2();
tester1.run();
tester2.run();
}

这应该完全符合我在第一个示例中所述。首次运行每个进程时,它们将各自成功获取自己的互斥对象(A锁sem1,B锁sem2),然后将睡眠1秒钟。唤醒时,它们将各自尝试锁定另一个互斥锁(A尝试锁定sem2,B尝试锁定sem1),但是由于这些资源从未被各自的进程释放,因此无法获取它们,因此这两个进程将无限期地阻塞。 / p>

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.

解决方案

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:

  1. Process A locks Mutex A
  2. Process B locks Mutex B
  3. Process A wants to lock Mutex B but has already been locked by Process B!
  4. Process B wants to lock Mutex A but has already been locked by Process A!

Note: A mutex is simply a semaphore with a value of 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:

  1. Process A locks Mutex A_A (exists in Process A)
  2. Process B locks Mutex B_B (exists in Process B)
  3. Process A locks Mutex B_A (exists in Process A)
  4. Process B locks Mutex A_B (exists in 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.

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.

这篇关于显示带有信号量的死锁的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆