在前面的基礎上。希望添加一個等待隊列,用於阻塞不能得到資源的進程們。在資源釋放時,再從阻塞中恢復。
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <linux/device.h>
#include <linux/sched.h>
#define DEV_NAME "new_timer"
#define DEV_MAJOR 0
#define DEV_MONITOR 0
#define CLASS_NAME "cla_ntimer"
struct timer_dev {
struct cdev cdev;
struct class *pclas;
struct device *pdev;
int dev_num;
struct timer_list timer;
atomic_t open_cnt; //原子操作來保證併發數量限制
struct semaphore sem; //信號量操作
int counter; //用來給計時器計數用
wait_queue_head_t wqh; //用來存儲阻塞進程用
};
static struct timer_dev mytimer;
static void timer_irq_hand( unsigned long arg)
{
down( &(mytimer.sem)); //主要與read()互斥
mytimer.counter++;
up( &(mytimer.sem));
printk("time: %d, pid = %d\n", mytimer.counter, current->pid);
mod_timer( &(mytimer.timer), jiffies + HZ);
#if 0 //測試中斷中進行進程調度的後果
struct semaphore sem;
sema_init( &sem, 0);
down( &sem); //這裏會進入阻塞
//實驗發現,由於進程調度,操作系統確實跑飛了
#endif
}
static int timer_open(struct inode *inode, struct file *fp)
{
int ret = 0;
#if 0
if( atomic_sub_return( 1, &(mytimer.open_cnt))< 0 ) //假如計數已經小於0,表明已經達到最大併發數
{
atomic_add(1, &(mytimer.open_cnt));
return -EBUSY; //不能得到資源就立即返回
}
#else
wait_queue_t wq;
init_wait( &(wq));
printk("I am waiting ,pid = %d\n", current->pid);
while( atomic_sub_return( 1, &(mytimer.open_cnt))< 0)
{
atomic_add( 1, &(mytimer.open_cnt)); //實際上,如果資源釋放發生在這三行代碼中,可能會導致有資源卻沒能獲得的情況
if( atomic_read( &(mytimer.open_cnt))<=0 )
set_current_state( TASK_INTERRUPTIBLE);
if( atomic_read( &(mytimer.open_cnt))<=0 )
{
printk(" waiting ,pid = %d\n", current->pid);
add_wait_queue( &(mytimer.wqh), &wq);
schedule();
printk("waked ,pid = %d\n", current->pid);
}
if( signal_pending( current))
{
ret = -EBUSY;
goto OUT;
}
}
printk("I am waked ,pid = %d\n", current->pid);
#endif
DEFINE_TIMER( temp, timer_irq_hand, jiffies + HZ, 0);
mytimer.timer = temp;
add_timer( &(mytimer.timer));
sema_init( &(mytimer.sem), 1);
//mytimer.counter = 0;
ret = 0;
OUT:
remove_wait_queue( &(mytimer.wqh), &wq);
set_current_state( TASK_RUNNING);
return ret;
}
static int list_len( struct list_head *phead)
{
int i = 0;
struct list_head *pos;
list_for_each(pos,phead)
{
i++;
}
return i;
}
static int timer_release(struct inode *inode, struct file *fp)
{
printk(" ====> list empty : %d, pid = %d\n", list_len(&(mytimer.wqh.task_list)), current->pid);
atomic_add( 1, &(mytimer.open_cnt)); //釋放資源
//經過分析懷疑是wake_up_interruptible()函數清空了等待隊列,檢驗發現確實是他乾的
wake_up_interruptible( &(mytimer.wqh)); //喚醒其他等待資源的進程
printk(" ====<list empty : %d, pid = %d\n", list_len(&(mytimer.wqh.task_list)), current->pid);
del_timer( &(mytimer.timer));
return 0;
}
static ssize_t timer_read(struct file *fp, char __user *buf, size_t sz, loff_t *off)
{
printk("read....\n");
int temp;
#if 1 //阻塞版
down( &(mytimer.sem)); //主要與中斷中的自加互斥
#else //非阻塞版
if( down_trylock( &(mytimer.sem)))
{
return -EBUSY;
}
#endif
temp = mytimer.counter; //安全的獲取數據
up( &(mytimer.sem));
copy_to_user( buf, &temp, sz);
return sz;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = timer_open,
.release = timer_release,
.read = timer_read,
};
static int __init timer_init(void)
{
int ret = 0;
printk("keyBoard_init..........%s\n", __TIME__);
ret = alloc_chrdev_region( &(mytimer.dev_num), DEV_MONITOR, 1, DEV_NAME);
if( ret<0 )
{
printk("error:%s, %d\n", __FUNCTION__,__LINE__);
goto ERR_ALLOC_CHRDEV;
}
cdev_init( &(mytimer.cdev), &fops);
mytimer.cdev.owner = THIS_MODULE;
ret = cdev_add( &(mytimer.cdev), mytimer.dev_num, 1);
if( ret<0)
{
printk("error: %s, %d\n", __FUNCTION__, __LINE__);
goto ERR_CDEV_ADD;
}
mytimer.pclas = class_create( THIS_MODULE, CLASS_NAME);
if( NULL==mytimer.pclas )
{
printk("error: %s, %d\n", __FUNCTION__, __LINE__);
goto ERR_CLAS_CREAT;
}
mytimer.pdev = device_create( mytimer.pclas, NULL, mytimer.dev_num, NULL, DEV_NAME);
if( NULL==mytimer.pdev )
{
goto ERR;
}
atomic_set( &(mytimer.open_cnt) , 1); //只允許打開一次
init_waitqueue_head( &(mytimer.wqh));
return 0;
ERR:
class_destroy( mytimer.pclas);
ERR_CLAS_CREAT:
cdev_del( &(mytimer.cdev));
ERR_CDEV_ADD:
unregister_chrdev_region( mytimer.dev_num, 1);
ERR_ALLOC_CHRDEV:
return -1;
}
static void __exit timer_exit(void)
{
printk("keyBoard_exit..........\n");
device_destroy( mytimer.pclas, mytimer.dev_num);
class_destroy( mytimer.pclas);
cdev_del( &(mytimer.cdev));
unregister_chrdev_region( mytimer.dev_num, 1);
return;
}
MODULE_LICENSE("GPL");
module_init(timer_init);
module_exit(timer_exit);
在編程過程中出現了一個錯誤,最初在timer_open()中的用於獲取資源的while()循環是下面
add_wait_queue( &(mytimer.wqh), &wq);
while( atomic_sub_return( 1, &(mytimer.open_cnt))< 0)
{
atomic_add( 1, &(mytimer.open_cnt)); //實際上,如果資源釋放發生在這三行代碼中,可能會導致有資源卻沒能獲得的情況
if( atomic_read( &(mytimer.open_cnt))<=0 )
set_current_state( TASK_INTERRUPTIBLE);
if( atomic_read( &(mytimer.open_cnt))<=0 )
{
printk(" waiting ,pid = %d\n", current->pid);
schedule();
printk("waked ,pid = %d\n", current->pid);
}
if( signal_pending( current))
{
ret = -EBUSY;
goto OUT;
}
}
而釋放資源的代碼是這樣
atomic_add( 1, &(mytimer.open_cnt)); //釋放資源
wake_up_interruptible( &(mytimer.wqh)); //喚醒其他等待資源的進程
會導致死鎖。經檢查發現原因是 wake_up_interruptible()函數在每次喚醒後會清空等待隊列。下面來認真看一下這個函數
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
void __wake_up(wait_queue_head_t *q, unsigned int mode,
int nr_exclusive, void *key)
{
unsigned long flags;
spin_lock_irqsave(&q->lock, flags);
__wake_up_common(q, mode, nr_exclusive, 0, key);
spin_unlock_irqrestore(&q->lock, flags);
}
void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
int nr_exclusive, int sync, void *key)
{
wait_queue_t *curr, *next;
list_for_each_entry_safe(curr, next, &q->task_list, task_list) { //由於curr->fun()函數會自動清除自身節點,所以選用了能保證清除安全的list_for_each_entry_safe()
unsigned flags = curr->flags;
if (curr->func(curr, mode, sync, key) &&
(flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) //爲0表示喚醒所有
break;
}
}
在上面的list_for_each_entry_safe()中會遍歷所有滿足條件的節點,然後等待節點的清除由curr->func()函數自己完成。去檢查會發現,其函數內容如下,
int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
{
int ret = default_wake_function(wait, mode, sync, key);
if (ret)
list_del_init(&wait->task_list); //就是在這裏清除
return ret;
}
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
知識點:1).在驅動中由current宏指向驅動的當前進程。
2).wake_up_interruptible()會清除等待隊列。
3). list_for_each_entry_safe()爲在遍歷中刪除節點提供了安全,預防了由於節點刪除導致的遍歷斷鏈。