Chinaunix首页 | 论坛 | 博客
  • 博客访问: 971533
  • 博文数量: 109
  • 博客积分: 1751
  • 博客等级: 上尉
  • 技术积分: 1817
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-31 22:37
文章分类

全部博文(109)

文章存档

2014年(9)

2013年(21)

2012年(48)

2011年(31)

分类: Android平台

2013-06-04 17:03:58

这里仅限于linux内核2.6.16以后的大前提,说的是linux内核里的保护临界资源的接口,不是普通的OS原理里的概念

有句话叫神仙打架,凡人遭殃
从前有一本书,叫LDD,被誉为Linux驱动开发的圣经,虽然最新已是第三版,但还是太老了,我估计它所谓包含2.6内核内容,没有高过2.6.10 ,现在都是2.6.32了,所以很期待ldd4
后来又有一本书,叫ELDD(Essential Linux Device Driver),虽然是第一版,但是已经有人给出了 “取代LDD成为新圣经”的评价
如果这两本圣经掐起架来,OMG,我该信谁啊?

首先,我以前看到的所有驱动开发书籍都跟LDD说的一样(估计他们也是看LDD启蒙的):内核保护临界资源的接口有两种--自旋锁spinlock信号量Semaphore
LDD3第五章里说:当信号量用于互斥时(即避免多个进程同是在一个临界区运行),信号量的值应初始化为1。这种信号量在任何给定时刻只能由单个进程或线程拥有。在这种使用模式下,一个信号量有时也称为一个“互斥体(mutex)”,它是互斥(mutual exclusion)的简称。Linux内核中几乎所有的信号量均用于互斥。
(本文作者deep_pro  转载请注明出处)

但是今天看ELDD,第二章
访问共享资源的代码区域称作临界区。自选锁( spinlock )和互斥体( mutex , mutual exclusion 的缩写)是保护内核临界区的 2 种基本机制。
互斥体接口代替了老的信号量接口(semaphore ),互斥体诞生于-rt 树,在2.6.16 内核中被融入主线内核。 
尽管如此,但是老的信号量仍然在内核和驱动中被广泛使用。
原文
Spinlocks and mutexes (short for mutual exclusion) are the two basic mechanisms used to protect critical sections in the kernel. 
The mutex interface, which replaces the older semaphore interface, originated in the –rt tree and was merged into the mainline with the 2.6.16 kernel release. The semaphore interface is still around, however. 

LDD说一个信号量有时也称为一个“互斥体(mutex)”
ELDD说互斥体接口代替了老的信号量接口(semaphore )

哦耶,好戏来了,看看两个老家伙谁更牛x
查看较新的linux内核源码/Documentation/mutex-design.txt 前10行

Generic Mutex Subsystem
   
   started by Ingo Molnar <>
   
     "Why on earth do we need a new mutex subsystem, and what's wrong
      with semaphores?"
   
   firstly, there's nothing wrong with semaphores. But if the simpler
   mutex semantics are sufficient for your code
, then there are a couple
of advantages of mutexes:

注意这两句黑体,第一句可以肯定,ELDD没有说错,mutex是一种新的子系统,跟Semaphores不一样
第二句,更郁闷了,LDD3也没有说错, the simpler mutex semantics ,我的理解就是被初始化为1的信号量,LDD3也就可以称之为“mutex”
同为mutex,但是写作时间不一样,所指的东西也不一样
这两个老家伙都没有写错,因为LDD3写作那会,估计ELDD所说的mutex子系统还没有成气候
失望啊

mutex 接口的 Disadvantages
-------------

The stricter mutex API means you cannot use mutexes the same way you
can use semaphores: e.g. they cannot be used from an interrupt context,
nor can they be unlocked from a different context that which acquired
it.
 [ I'm not aware of any other (e.g. performance) disadvantages from
using mutexes at the moment, please let me know if you find any. ]

nor can they be unlocked from a different context that which acquired it. 
这句话比较难理解一点,就是说你自己锁上的mutex,只能由你自己来解锁,不能靠其他进程或者中断来解锁

最后附上ELD给的使用 spinlock 、mutex、seamphore 3种接口的范例

spinlock 
#include
spinlock_t mylock = SPIN_LOCK_UNLOCKED; /* Initialize */

/* Acquire the spinlock. This is inexpensive if there
* is no one inside the critical section. In the face of
* contention, spinlock() has to busy-wait.
*/
spin_lock(&mylock);

/* ... Critical Section code ... */

spin_unlock(&mylock); /* Release the lock */

Basic mutex usage is as follows:

#include /* Statically declare a mutex. To dynamically create a mutex, use mutex_init() */static DEFINE_MUTEX(mymutex);/* Acquire the mutex. This is inexpensive if there * is no one inside the critical section. In the face of * contention, mutex_lock() puts the calling thread to sleep. */mutex_lock(&mymutex);/* ... Critical Section code ... */mutex_unlock(&mymutex); /* Release the mutex */



Basic usage of the semaphore interface is as follows:

#include /* Architecture dependent header *//* Statically declare a semaphore. To dynamically create a semaphore, use init_MUTEX() */static DECLARE_MUTEX(mysem);down(&mysem); /* Acquire the semaphore *//* ... Critical Section code ... */up(&mysem); /* Release the semaphore */


不管怎么说,还是喜欢ELDD更多一点

阅读(5066) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~