如果不同处理器中的两个进程尝试同时完全获取锁,会发生什么情况? [英] What happens if two process in different processors try to acquire the lock at EXACTLY same time

查看:236
本文介绍了如果不同处理器中的两个进程尝试同时完全获取锁,会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我正在阅读有关同步的内容,并且通读了各种算法,例如自旋锁,信号量和互斥锁,以避免出现竞争状况.

Ok, so I am reading about synchronization, and I read through various algorithms such as spinlocks, semaphores, and mutex to avoid race condition.

但是,当多个进程恰好同时访问数据时,这些算法无法防止SMP中的竞争状况.

However, these algorithms can't prevent race condition in SMP when multiple proceses access the data exactly at the same time.

例如,假设处理器A中的线程1运行 锁(mutex1); 提现(1000); unlock(mutex1);

For example, suppose thread 1 in processor A runs lock(mutex1); withdraw(1000); unlock(mutex1);

,并且处理器B中的线程2运行 锁(mutex1); 押金(1000); 押金(1000); unlock(mutex1);

and thread 2 in processor B runs lock(mutex1); deposit(1000); deposit(1000); unlock(mutex1);

两个线程在同一时间准确运行时,两个线程将同时处于关键部分.

When both threads run EXACTLY AT THE SAME TIME, both threads will be in critical section simultaneously.

唯一的解决方案(应该在硬件级别上)是使每个处理器彼此之间略微跑开,但它破坏了并行性的目的.

The only solution (should be in hardware level) would be making each processors run slightly off to each other, but it defeats the purpose of parallelism.

是否有硬件级别的支持来避免多个处理器试图在同一时间获取锁的情况?

Is there any hardware level support to avoid these situation where multiple processors try to acquire the lock at the exactly same time?

(这不是原子性的问题,而是确切的并行性问题,我想知道SMP如何处理它).

(this is not a problem of atomicity, but rather problem of exact parallelism, and I wonder how SMP deals with it).

推荐答案

互斥锁的全部要点是,即使两个内核试图同时获取它,它们中的一个也会被阻塞,直到另一个释放它为止. .允许两个内核同时保存该互斥锁的互斥锁将完全损坏,并且仅出于其预期目的而无用.

The whole point of a mutex is that even if two cores try to acquire it at the same time, one of them will be blocked until the other one releases it. A mutex that permitted two cores to hold that mutex at the same time would be completely broken and useless for its sole, intended purpose.

在硬件的某个地方,有一个总线仲裁器,该仲裁器仅允许一个内核控制链接这两个内核的总线.如果任何一个核心已经具有将互斥量保存在专用缓存中的内存,则该核心将获胜.否则,以先乘公共汽车的人为准.

Somewhere in hardware there is a bus arbitrator that permits only one core to control the bus that links those two cores. If either core already has the memory holding the mutex in a private cache, that core will win. Otherwise, whichever one gets the bus first will win.

总线仲裁器可能以多种方式工作,但通常会旋转.因此,如果核心是0、1、2和3,而核心2的总线最后一次使用,则总线将根据需要转到核心3;否则,核心0会进入核心3;如果核心1,它会到达核心1,否则返回内核2.根据所涉及的总线的确切性(无论是两个内核的L2高速缓存之间的争斗还是内存本身或其他方面的冲突),某些内核可能会作为一个单元与其他内核组竞争,然后进行子竞争.哪个特定的内核先获得它.

The bus arbitrator may work in many ways, but typically it will rotate. So if the cores are 0, 1, 2, and 3 and core 2 had the bus last, the bus will next go to core 3 if it wants it, otherwise core 0 if it wants it, otherwise core 1 if it wants it, otherwise back to core 2. Depending on exactly which bus is involved (whether it's a fight between the two core's L2 caches or over the memory itself or whatever) some of the cores may contend as a unit against other core groups and then sub-contend for which specific core gets it first.

可能是一个内核已经控制了总线,因此它将完全获胜.通常,仲裁器允许内核继续保持总线,只要它继续希望将总线用于几次事务,以避免浪费的切换,从而使内核无法向前发展.

It may be that one core already has control over the bus and so it will win outright. Typically, the arbitrator allows a core to continuously hold the bus so long as it continues to want to use it for a few transactions to avoid wasteful handoffs that don't allow the core to make forward progress.

具体细节可能会因多种因素而异,包括内核的排列方式,哪些内核将缓存锁定在什么状态,哪个总线最后使用以及总线仲裁程序是否使用时间片,循环轮询等.或其他某种机制,等等.但是,任何不能保证只有一个核心完成锁定的实现都将被视为严重破坏.

The exact details can vary depending on a large number of factors including how the cores are arranged, which cores have the lock in their caches in what states, who had the bus last and whether the bus arbitrator uses timeslices, round-robin, or some other mechanism, and so on. But any implementation that didn't guarantee that one and only one core winds up getting the lock would be considered horribly broken.

这篇关于如果不同处理器中的两个进程尝试同时完全获取锁,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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