江油市建設(shè)局網(wǎng)站網(wǎng)站建設(shè)平臺
信號量
信號量允許多個進(jìn)程同時進(jìn)入臨界區(qū),大多數(shù)情況下只允許一個進(jìn)程進(jìn)入臨界區(qū),把信號量的計數(shù)值設(shè)置為 1,即二值信號量,這種信號量稱為互斥信號量??稍试S多個鎖持有者。
和自旋鎖相比,信號量適合保護(hù)比較長的臨界區(qū),因為競爭信號量時進(jìn)程可能睡眠和再次喚醒,代價很高。中斷服務(wù)函數(shù)不能進(jìn)行睡眠,因此信號量不能用于中斷當(dāng)中
信號量的使用流程:
定義一個信號量
?? ?↓
初始化信號量
?? ?↓
獲得信號量(減操作)
?? ?↓
釋放信號量(加操作)
內(nèi)核使用的信號量定義如下:
include/linux/semaphore.h
struct semaphore {
? ? raw_spinlock_t ? ? ?lock;
? ? unsigned int ? ? ? ?count;
? ? struct list_head ? ?wait_list;
};
成員 lock 是自旋鎖,用來保護(hù)信號量的其他成員。
成員 count 是計數(shù)值,表示還可以允許多少次進(jìn)入臨界區(qū)。
成員 wait_list 是等待進(jìn)入臨界區(qū)的進(jìn)程鏈表。
struct semaphore_waiter {
? ? struct task_struct *task;
? ? bool up;
? ? struct list_head list;
};
初始化靜態(tài)信號量的方法如下。
(1)?? ?__SEMAPHORE_INITIALIZER(name, n):指定名稱和計數(shù)值,允許同時n 次進(jìn)入臨界區(qū)。
(2)?? ?DEFINE_SEMAPHORE(name):初始化一個互斥信號量。
在運行時動態(tài)初始化信號量的方法如下:
static inline void sema_init(struct semaphore *sem, int val);
參數(shù) val 指定允許同時進(jìn)入臨界區(qū)的數(shù)量。
獲取信號量的函數(shù)如下。
(1) void down(struct semaphore *sem);
獲取信號量,如果計數(shù)值是 0,進(jìn)程深度睡眠。
(2) int down_interruptible(struct semaphore *sem);
獲取信號量,如果計數(shù)值是 0,進(jìn)程輕度睡眠(可以被系統(tǒng)消息打斷,該函數(shù)的調(diào)用允許中斷)。如果返回0,表示獲得信號量正常返回,如果被信號打斷,返回-EINTR。
(3) int down_killable(struct semaphore *sem);
獲取信號量,如果計數(shù)值是 0,進(jìn)程中度睡眠(可以因為受到致命信號而被喚醒)。
(4) int down_trylock(struct semaphore *sem);
獲取信號量,如果計數(shù)值是 0,進(jìn)程不等待不會導(dǎo)致調(diào)用者睡眠。
(5) int down_timeout(struct semaphore *sem, long jiffies);
獲取信號量,指定等待的時間。
釋放信號量的函數(shù)如下:
void up(struct semaphore *sem);
使用示例
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>?
#include <linux/semaphore.h>?
#include <linux/delay.h>
int num[2][5]= { {
? ? ? ? 0,2,4,6,8
? ? }
? ? , {
? ? ? ? 1,3,5,7,9
? ? }
}
;
struct semaphore sem_first;
struct semaphore sem_second;
struct task_struct * task1;
struct task_struct * task2;
int thread_print_first(void *p) {
? ? int i;
? ? int *num=(int *)p;
? ? if(kthread_should_stop()){
? ? ? ? return 0;
? ? }
? ? printk(KERN_ALERT"Hello World:first\n");
? ? for (i=0;i<5;i++) {
? ? ? ? down(&sem_first);
? ? ? ? printk(KERN_ALERT"Hello World:%d\n",num[i]);
? ? ? ? up(&sem_second);
? ? }
? ? do {
? ? ? ? msleep(1000);
? ? }
? ? while(!kthread_should_stop());
? ? return 0;
}
int thread_print_second(void *p) {
? ? int i;
? ? int *num=(int *)p;
? ? if(kthread_should_stop()){
? ? ? ? return 0;
? ? }
? ? printk(KERN_ALERT"Hello World:second\n");
? ? for (i=0;i<5;i++) {
? ? ? ? down(&sem_second);
? ? ? ? printk(KERN_ALERT"Hello World:%d\n",num[i]);
? ? ? ? up(&sem_first);
? ? }
? ? do {
? ? ? ? msleep(1000);
? ? }
? ? while(!kthread_should_stop());
? ? return 0;
}
static int hello_init(void) {
? ? printk(KERN_ALERT"Hello World enter\n");
? ? sema_init(&sem_first,1);
? ? sema_init(&sem_second,0);
? ? task1 = kthread_create(thread_print_first,num[0],"first");
? ? if(IS_ERR(task1)) {
? ? ? ? printk(KERN_ALERT"kthread_create error!\n");
? ? ? ? return -1;
? ? }
? ? task2 = kthread_create(thread_print_second,num[1],"second");
? ? if(IS_ERR(task2)) {
? ? ? ? printk(KERN_ALERT"kthread_create error!\n");
? ? ? ? kthread_stop(task1);
? ? ? ? return -1;
? ? }
? ? wake_up_process(task1);
? ? wake_up_process(task2);
? ? return 0;
}
static void hello_exit(void) {
? ? int ret;
? ? if (!IS_ERR(task1)) {
? ? ? ? ret = kthread_stop(task1);
? ? ? ? printk("<<<<<<<<task1 exit, ret = %d\n", ret);
? ? }
? ? if (!IS_ERR(task2)) {
? ? ? ? ret = kthread_stop(task2);
? ? ? ? printk("<<<<<<<<task2 exit, ret = %d\n", ret);
? ? }
? ? printk(KERN_ALERT"hello world exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
?