📄
字号:
netxiong@263.net
**********************************内核信号量的原理和实现机制*********************************
这是一些非常难懂的代码。
涉及到的文件是 arch/i386/kernel/semaphore.c
include/asm/semaphore.h
/include/asm/atomic.h
实际上信号量semaphore的实现机制和等待队列是一样的,也要用到list_head,wait_queue等一系列的数据结构,
主要函数如下:
(1)static inline void sema_init (struct semaphore *sem, int val) /asm/semaphore.h
(2):static inline void down(struct semaphore * sem) /asm/semaphore.h
(3):static inline int down_interruptible(struct semaphore * sem) /asm/semaphore.h
(4):static inline void up(struct semaphore * sem) /asm/semaphore.h
使用方法如下
#定义一个信号量,使用sema_init对他进行初始化,并赋给他一个初值。
#使用down或者down_interruptible对临界量进行访问
#使用完毕使用up释放临界量。
注意,所有的down类型都使用up来释放。
down和down_interruptible的区别在于,down的返回值是void,也就是说它一定会对临界区成功地进行加锁
而down_interruptible的返回值是int,如果返回0,则表明成功加锁,否则说明加锁失败,可以进行相应的
处理。
*********************************************************************************************
!!!!这里千万注意一点,在中断处理函数中不能使用down这样的临界区操作,因为这有可能引起调度,休眠
而这是在中断中不允许出现的。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -