网址收藏 自旋锁

http://blog.csdn.net/vividonly/article/details/6594195
假如持有自旋锁时进程的时间片用完了的话,是该如何来处理呢?
在单处理机上,spin_lock()就退化成了Preemp_disable(),它就是禁止抢占,即虽然时间片用完了,但是仍然是不能切换到其它进程去的。因此每个进程有一个preemp_count这个变量,如果这个变量为0的话表示其可以被其它进程所抢占,如果大于0则不能够被抢占,而在调用preemp_disable()的时候就会将该进程的preemp_count值加1。使得其不能够被抢占。而继续运行该进程,直到调用preemp_enable()将其设为可抢占,由于时间一般不长,因此不会有影响。
refer to
http://blog.csdn.net/scutanjun/article/details/6240200

另外 ulk3中讲
1.6.5.4. Spin locks

In multiprocessor systems, semaphores are not always the best solution to the synchronization problems. Some kernel data structures should be protected from being concurrently accessed by kernel control paths that run on different CPUs. In this case, if the time required to update the data structure is short, a semaphore could be very inefficient. To check a semaphore, the kernel must insert a process in the semaphore list and then suspend it. Because both operations are relatively expensive, in the time it takes to complete them, the other kernel control path could have already released the semaphore.

In these cases, multiprocessor operating systems use spin locks . A spin lock is very similar to a semaphore, but it has no process list; when a process finds the lock closed by another process, it "spins" around repeatedly, executing a tight instruction loop until the lock becomes open.

Of course, spin locks are useless in a uniprocessor environment. When a kernel control path tries to access a locked data structure, it starts an endless loop. Therefore, the kernel control path that is updating the protected data structure would not have a chance to continue the execution and release the spin lock. The final result would be that the system hangs.
当然,自旋锁在单处理器环境下无效。当一个内核控制路径试图访问一个已被加锁了的临界数据(即试图获取同一个锁)时,它开始原地打转。因此原来那个正在更新临界数据的内核控制路径将没有机会继续执行去释放锁。最终结果是挂机。


所以对于
单处理器支持内核抢占的系统,在某个内核执行路径获得锁后要关闭内核抢占,在释放锁后再打开内核抢占。以防止死锁。
单处理器不支持内核抢占系统,没必要使用自旋锁。自旋锁可以退化为空操作。

smp系统类似于单处理器支持内核抢占的系统,在自旋锁获得期间也要关闭内核抢占。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值