English 中文(简体)
在模拟银行数据库中防止僵局
原标题:Preventing deadlock in a mock banking database

这是一个涉及僵局的模拟银行数据库问题。 我认为,我已经有了答案,但如果这是一个好的解决办法,我会感到奇怪。 提出的问题如下:

你们如何防止以下法典的僵局?

void transaction(Account from, Account to, double amount)
{
    Semaphore lock1, lock2;
    lock1 = getLock(from);
    lock2 = getLock(to);

    wait(lock1);
       wait(lock2);

          withdraw(from, amount);
          deposit(to, amount);

       signal(lock2);
    signal(lock1);
 }

如何打破僵局,是用两种途径(或程序)把交易方法与对立账户同时进行,即:

交易(储蓄、检查、1);

交易(检查、储蓄、2);阅读2。

根据我的不理解,我认为正在发生什么,为什么陷入僵局的原因是,由于两条read子都试图走一个锁(相互之间)而没有遵守锁定。

我的迅速和 d忙的解决办法是,将职能交易之外的锁移至它希望这样做的地方。

 //somewhere in main
    Semaphore lock1, lock2;
    lock1 = getLock(from);
    lock2 = getLock(to);

    wait(lock1);
       wait(lock2);

       transaction(checking, savings, 2);

       signal(lock2);
    signal(lock1);
    //.....

• 进行交易。

void transaction(Account from, Account to, double amount)
{
   withdraw(from, amount);
   deposit(to, amount);
}

这样,它们永远不会同时执行,因为交易在技术上是关键部分。 如果是“java”方案,你能否使用监测器,在职能申报中,该词在宣布无效后同步使用? 这一工作吗? 这似乎是一种简单的做法。

我还可能根本不理解这一点,因此感到我可以自由上学,特别是如果我的解释不准确的话。 感谢。

最佳回答

我认为,它正在经历什么,为什么陷入僵局,是因为24小时命令没有得到遵守,因为两条路人都在试图走锁。

这里的问题是,正如你所提到的那样:

wait(fromLock);
   wait(toLock);

而另一国家可能这样做:

wait(toLock);
   wait(fromLock);

这可能造成僵局。

What you need to do is insure that they always lock in the same order. You could use the id of the account somehow to figure out the order of the locks:

if (from.id < to.id) {
   wait(fromLock)
     wait(toLock)
       transaction(checking, savings, 2);
     signal(toLock);
   signal(fromLock);
} else {
   wait(toLock)
     wait(FromLock)
       transaction(checking, savings, 2);
     signal(FromLock);
   signal(toLock);
}

我认为,这将解决任何僵局。 您也不妨对<条码>和<条码>作为同一实体的<条码>进行检查。

问题回答

将这一关键部分分为两个部分:

void transaction(Account from, Account to, double amount)
{
    Semaphore lock1, lock2;
    lock1 = getLock(from);
    lock2 = getLock(to);

    wait(lock1);
          withdraw(from, amount);
    signal(lock1);


    wait(lock2);      
          deposit(to, amount);
    signal(lock2);
}

这将是现实世界的最佳解决办法,因为将这一关键部分分开,不会导致它们之间的任何不想要的国家(也就是说,永远不会出现一种情况,即,如果收回的资金比可能要多)。

The simple solution to leave the critical section in one piece, is to ensure that lock order is the same in all transactions. In this case you can sort the locks and choose the smaller one for locking first.

关于在这些局势中防止僵局的更详细信息,见我的问题:Multiple mutex locking strategies,以及为什么图书馆不使用网址

在等待对方时,任何交易都不应被搁置一锁。 如果无法获得它所需要的锁之一,那么它就应当释放它获得的所有锁。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签