竞态条件和死锁之间的区别 [英] Difference between racearound condition and deadlock

查看:222
本文介绍了竞态条件和死锁之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编程方面,死锁和争分夺秒有什么区别?

What is the difference between a dead lock and a race around condition in programming terms?

推荐答案

使用传统示例来考虑竞争条件.假设您和朋友使用同一个银行帐户的ATM卡.现在假设帐户中有$ 100.请考虑一下,当您尝试提取10美元,而您的朋友尝试同时完全提取50美元时,会发生什么情况.

Think of a race condition using the traditional example. Say you and a friend have an ATM cards for the same bank account. Now suppose the account has $100 in it. Consider what happens when you attempt to withdraw $10 and your friend attempts to withdraw $50 at exactly the same time.

考虑必须发生的事情. ATM机必须接受您的输入,读取您帐户中当前的内容,然后修改金额.请注意,就编程而言,赋值语句是一个多步骤过程.

Think about what has to happen. The ATM machine must take your input, read what is currently in your account, and then modify the amount. Note, that in programming terms, an assignment statement is a multi-step process.

因此,同时标记两个交易T1(您提取10美元)和T2(您的朋友提取50美元).现在,下面左侧的数字代表时间步长.

So, label both of your transactions T1 (you withdraw $10), and T2 (your friend withdraws $50). Now, the numbers below, to the left, represent time steps.

       T1                        T2
       ----------------          ------------------------
 1.    Read Acct ($100)          
 2.                              Read Acct ($100)
 3.    Write New Amt ($90)
 4.                              Write New Amt ($50)
 5.                              End
 6.    End

在完成这两项交易后,使用此时间轴(如果您不使用任何锁定机制,则该帐户中可能有50美元),这是可能的.这比应该多出10美元(您的交易会永远丢失,但您仍然有钱).

After both transactions complete, using this timeline, which is possible if you don't use any sort of locking mechanism, the account has $50 in it. This is $10 more than it should (your transaction is lost forever, but you still have the money).

这是所谓的比赛条件.您想要的是可序列化的事务,也就是说,无论您如何交错执行各个指令,最终结果都将与 some 串行时间表完全相同(意味着您一个接一个地运行它们,没有交错)相同的事务.同样,解决方案是引入锁定.但是错误的锁定会导致死锁.

This is a called race condition. What you want is for the transaction to be serializable, that is in no matter how you interleave the individual instruction executions, the end result will be the exact same as some serial schedule (meaning you run them one after the other with no interleaving) of the same transactions. The solution, again, is to introduce locking; however incorrect locking can lead to dead lock.

当共享资源冲突时,将发生死锁.有点像Catch-22.

Deadlock occurs when there is a conflict of a shared resource. It's sort of like a Catch-22.

   T1            T2
   -------       --------
1.  Lock(x)
2.               Lock(y)
3.  Write x=1
4.               Write y=19
5.  Lock(y)
6.  Write y=x+1
7.               Lock(x)
8.               Write x=y+2
9.  Unlock(x)
10.              Unlock(x)
11. Unlock(y)
12.              Unlock(y)

您可以看到在时间7发生了死锁,因为T2试图获取x上的锁,但是T1已经持有了x上的锁,但它正在等待y上的锁,T2持有该锁.

You can see that a deadlock occurs at time 7 because T2 tries to acquire a lock on x but T1 already holds the lock on x but it is waiting on a lock for y, which T2 holds.

这很糟糕.您可以将此图变成一个依赖图,您会看到有一个循环.这里的问题是x和y是可以一起修改的资源.

This bad. You can turn this diagram into a dependency graph and you will see that there is a cycle. The problem here is that x and y are resources that may be modified together.

使用多个锁定对象(资源)防止这种死锁问题的一种方法是引入排序.在前面的示例中,您看到T1锁定了x,然后又锁定了y,但是T2锁定了y,然后又锁定了x.如果这两个事务都遵循某些排序规则,即"x应该始终在y之前被锁定",则不会发生此问题. (您可以牢记此规则来更改前面的示例,并且不会发生死锁).

One way to prevent this sort of deadlock problem with multiple lock objects (resources) is to introduce an ordering. You see, in the previous example, T1 locked x and then y but T2 locked y and then x. If both transactions adhered here to some ordering rule that says "x shall always be locked before y" then this problem will not occur. (You can change the previous example with this rule in mind and see no deadlock occurs).

这些都是琐碎的示例,实际上,我只是使用了您可能已经看过的示例,如果您对此进行过任何本科课程的话.实际上,解决死锁问题可能比这困难得多,因为您往往拥有多个资源和多个事务交互.

These are trivial examples and really I've just used the examples you may have already seen if you have taken any kind of undergrad course on this. In reality, solving deadlock problems can be much harder than this because you tend to have more than a couple resources and a couple transactions interacting.

希望这会有所帮助.与往常一样,使用Wikipedia作为CS概念的起点:

Hope this helps a little bit. As always, use Wikipedia as a starting point for CS concepts:

http://en.wikipedia.org/wiki/Deadlock

http://en.wikipedia.org/wiki/Race_condition

这篇关于竞态条件和死锁之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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