内核的同步(synchronization)以及锁机制

本文探讨了内核同步的重要性,特别是在多处理器和抢占式内核环境中。临界区的概念被提出,强调了保护共享资源的必要性。文章通过银行取款的例子介绍了同步的原子性和加锁方法,并详细阐述了Linux内核的不同锁机制。此外,还讨论了死锁问题以及加锁粒度对系统性能的影响,指出平衡锁的使用和粒度选择是优化内核同步的关键。
摘要由CSDN通过智能技术生成

1.前言
为什么要考虑内核同步

在单一CPU 的情况下,中断或者内核代码明确调度时,多个执行线程并发访问共享数据。目前多处理器以及抢占式内核的存在,更需要注意保护共享资源。

造成并发的原因
- Interrupts— An interrupt can occur asynchronously at almost any time, interrupting the currently executing code.
- Softirqs and tasklets— The kernel can raise or schedule a softirq or tasklet at
almost any time, interrupting the currently executing code.
- Kernel preemption— Because the kernel is preemptive, one task in the kernel
can preempt another.
- Sleeping and synchronization with user-space— A task in the kernel can
sleep and thus invoke the scheduler, resulting in the running of a new process.
- Symmetrical multiprocessing— Two or more processors can execute kernel
code at exactly the same time.

临界区

临界区( Critical Regions ) 指的是访问内存中共享数据的 代码段,编程者需要

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值