Fork me on GitHub

Linux 内核学习笔记(五)——内核同步及方法 kernel synchronization and synchronization methods

An introduction to kernel synchronization

当一个thread正在对数据进行读写操作时,另一个thread对同一个数据进行读写操作,这就会产生冲突竞争,比如重写数据单元等。为了解决这样的问题,必须考虑synchronization的问题。这样的竞争问题,并不总是能够复现,所以调试起来也比较困难。

这个需要被保护,防止被改变的section,称为critical regions (也称为critical sections)。

atomically:中间不能再细分,不能再打断。

scalability:也就是所谓的a large number of processes,a large number of processors,or large amounts of memory. 设计lock的时候需要考虑这种规模性变大的情况。

Lock

A lock provides such a mechanism; it works much like a lock on a door. Imagine the room beyond the door as the critical region. Inside the room, only one thread of execution can be present at a given time.When a thread enters the room, it locks the door behind it.When the thread is finished anipulating the shared data, it leaves the room and unlocks the door. If another thread reaches the door while it is locked, it must wait for the thread inside to exit the room and unlock the door before it can nter.Threads hold locks; locks protect data. 比如说设定一个变量,变量为0代表unlock,否则代表locked。

lock是自愿的,根据自己的程序环境自己添加。

kernel发生并发的原因可能如下:

对于interrupt handler安全的代码Code,称为interrupt-safe;

对于symmetrical multiprocessing machines 安全的代码Code,称为SMP-safe;

对于concurrency with kernel preemption 安全的代码code,称为preempt-safe;

实际的同步保护机制是针对以上三种所有情况来说的。

其中SMP为:

那么到底lock什么呢?记住,是lock data ,not code。如果是一个全局变量,最好lock;如果其他的thread可以access the data,这部分data就需要lock;如果anyone else 可以see it ,也需要lock(这显然是针对Linux的多用户来说的)。

deadlock

比如几个threads在互相等待,因为需要获得一部分data,否则不能进行下一步,但是这部分data分别被彼此锁定了,因此互相僵持,形成了deadlock(如下图)。其中一个例子就是self-lock。The simplest example of a deadlock is the self-deadlock: 4 If a thread of execution attempts to acquire a lock it already holds, it has to wait for the lock to be released.

写代码的时候需要注意这样自锁的问题,同时在处理不同变量的时候一定要考虑变量的顺序,最好释放、获取的顺序相搭配。(比如一个thread获取cat、fox、dog,另一个thread获取时也按照cat、fox、dog的顺序来,不要乱)

lock contention : highly contended.也就是所谓的高并发,通常是因为一个lock频繁地被使用,或者这个lock持续了很长时间,或者二者同时。这就导致其他的thread没有太高几率去获得这个data,所以称之为高并发。

kernel synchronization

通过内核的同步方式,可以让开发者写出高效的,race-free的代码来。

atomic operation

前面已经提过,atomic就是原子,所谓的不能再细化分,也就是不能再被interrupt的最小单元。它是其他的同步方法的基础。它不仅提供了32位、64位的integer operation,也提供bitwise operation。

spin locks

当一个thread想要获得相应的data,发现已经被占用了,它就在那里spin,旋转等待,loop个不停,直到别人开了门,轮到它进去为止。显然这样非常浪费时间,效率不高。为此,可以让其在loop等待的时候进入到sleep中,处理器可以去做其他工作,等到lock被释放了,将其唤醒即可(这个就是另一种方法:semaphores)。 spin lock可以放在interrupt handler里面,而semaphores就不可以,因为semaphores可以睡眠。

Because a bottom half might preempt process context code, if data is shared between a bottom-half process context, you must protect the data in process context with both a lock and the disabling of bottom halves. Likewise, because an interrupt handler might preempt a bottom half, if data is shared between an interrupt handler and a bottom half, you must both obtain the appropriate lock and disable interrupts.

semaphores

semaphores in Linux are sleeping locks.

同步部分还有太多内容,对我来说太难了,现在先不予研究。

参考文献

  1. Robert Love. "Linux Kernel Development." (2008).
  2. https://en.wikipedia.org/wiki/Symmetric_multiprocessing?oldformat=true

本文作者:LT_Rock

本文链接https://www.cnblogs.com/LT-Rock/p/13582542.html

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!


posted @ 2020-08-29 16:12  LT_Rock  阅读(325)  评论(0)    收藏  举报