Chinaunix首页 | 论坛 | 博客
  • 博客访问: 189604
  • 博文数量: 46
  • 博客积分: 1355
  • 博客等级: 中尉
  • 技术积分: 336
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-02 16:20
文章分类

全部博文(46)

文章存档

2017年(2)

2014年(12)

2012年(1)

2011年(5)

2010年(26)

我的朋友

分类: 嵌入式

2010-05-11 21:17:03

前几天在看驱动的时候碰到了等待队列,上网去搜了 一下,再结合代码看了一下,深有体会.在 kernel 里,wait_queue 的应用很广,举凡 device driver,semaphore 等方面都会使用到 wait_queue 来 implement。所以,它算是 kernel 里蛮 基本的一个数据结构。
首先,我们得明白,linux中的所有的进程都由task_struct这个结构管 理。在生成进程的时候将会分配一个task_struct结构,之后将通过这个结构对进程进行管理。 task_struct结构存在于平坦地址空间内,任何时候Linux内核都可以参照所有进程的所有管理情报。内核堆栈也同样位于平坦地址空间内。(平坦 的意思是"独立的连续区间")
下面是tesk_struct的主要成员:
--------------------------------------------------------------------------------
      struct task_struct {
struct files_struct* files; //文件描述符
struct signal_struct* sig; //信号控制signal handler
struct mm_struct* mm;        //内存管理模块
long stat                           //进程状态
struct list_head runlist;                     //用于联结RUN队列
long priority;              //基本优先权
long counter;               //变动优先权
char comm[];                                     //命令名
struct thread_struct tss;    //上下文保存领域
...
};
我们现在只需了解它里面的state就可以,state有下面几种状态:
状态                                             说明
TASK_RUNNING                                  执行可能状态
TASK_INTERRUPTIBLE                        等待状态。可接受信号
TASK_UNINTERRUPTIBLE                    等待状态。不能接受信号
TASK_ZOMBIE                                     僵尸状态。exit后的状态
TASK_STOPPED                                   延缓状态
我们要知道内核没有多进程,就只有一个进程(SMP就不清楚了),这跟在user space下是不同的.在用户空间里,我们可以使一个进程跑起while(1),其他的进程也能用,但是在内核中就不行了,原因在上面。
假设我们在 kernel 里产生一个 buffer,user 可以经由 read,write 等 system call 来读取或写资料到这个 buffer 里。如果有一个 user 写资料到 buffer 时,此时 buffer 已经满了。那请问你要如何去处理这种情形呢 ? 第一种,传给 user 一个错误讯息,说 buffer 已经满了,不能再写入。第二种,将 user 的要求 block 住, 等有人将 buffer 内容读走,留出空位时,再让 user 写入资料。但问题来了,你要怎么将 user 的要求 block 住。难道你要用
while ( is_full );
write_to_buffer;
这样的程序代码吗? 想想看,如果你这样做会发生什么事? 第一,kernel会一直在这个 while 里执行。第二个,如果 kernel 一直在这个 while 里执行,表示它没有办法去 maintain系统的运作。那此时系统就相当于当掉了。在这里 is_full 是一个变量,当然,你可以让 is_full 是一个 function,在这个 function里会去做别的事让 kernel 可以运作,那系统就不会当。这是一个方式。还有,你说可以在while里面把buffer里的内容读走,再把is_full的值改了,但是我们会可能把重 要的数据在我们不想被读的时候被读走了,那是比较麻烦的,而且很不灵活.如果我们使用 wait_queue 的话, 那程序看起来会比较漂亮,而且也比较让人了解,如下所示:
struct wait_queue_head_t wq; /* global variable */
DECLARE_WAIT_QUEUE_HEAD (wq);
while ( is_full ){
interruptible_sleep_on( &wq );
} write_to_buffer();
interruptible_sleep_on( &wq ) 是用来将目前的 process,也就是要求写资料到buffer 的 process放到 wq 这个 wait_queue 里。在 interruptible_sleep_on 里,则是最后会呼叫 schedule() 来做 schedule 的动作,谁调用了schedule谁就趴下,让别人去运行,醒来就原地起来,执行schedule()后的代码。那那个调用了schedule的家伙什么 醒过来呢?这时候就需要用到另一个函数了wake_up_interruptible()了,如下所示:
if ( !is_empty ) {
read_from_buffer();
wake_up_interruptible( &wq );
}
这就wait_queue的用法,挺好懂的.那wait_queue到底是怎么工作的呢?wait_queue_head_t是一个相单简单的结构,在中,代码如下:
--------------------------------------------------------------------------------
struct __wait_queue_head {
         wq_lock_t lock;
truct list_head task_list;
#if WAITQUEUE_DEBUG
long __magic;
long __creator;#endif
};
typedef struct __wait_queue_head wait_queue_head_t;
其中task_list是一个正在睡眠的进程的链表,链表中的各个数据项的类型是wait_queue_t,链表就是在中定义的通用链表,wait_queue_t代码如下:
struct __wait_queue {
unsigned int flags;
#define WQ_FLAG_EXCLUSIVE        0x01
struct task_struct * task;
struct list_head task_list;
#if WAITQUEUE_DEBUG
long __magic;
long __waker;#endif
};
typedef struct __wait_queue wait_queue_t;
其实,主要的结构是wait_queue_t.让我们来看一下interruptible_sleep_on的代码中,代码如下:
--------------------------------------------------------------------------------
#define SLEEP_ON_VAR                 \
unsigned long flags;                  \
wait_queue_t wait;                   \
init_waitqueue_entry(&wait, current);         //用当前进程生成一个wait_queue_t
#define SLEEP_ON_HEAD                      \
        spin_lock_irqsave(&q->lock,flags);               \
__add_wait_queue(q, &wait);               //把 wait 放到 q 所属的wait_queue_t list 的开头
spin_unlock(&q->lock);
#define SLEEP_ON_TAIL                                            \
spin_lock_irq(&q->lock);                              \
__remove_wait_queue(q, &wait);                           \
spin_unlock_irqrestore(&q->lock, flags);
void interruptible_sleep_on(wait_queue_head_t *q)
{
SLEEP_ON_VAR
current->state = TASK_INTERRUPTIBLE;
SLEEP_ON_HEAD
schedule();              //状态为TASK_INTERRUPTIBLE的进程是不会执行的
SLEEP_ON_TAIL
}
static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
{
#if WAITQUEUE_DEBUG
if (!head || !new)
WQ_BUG();
CHECK_MAGIC_WQHEAD(head);
CHECK_MAGIC(new->__magic);
if (!head->task_list.next || !head->task_list.prev)
WQ_BUG();
#endif
list_add(&new->task_list, &head->task_list);
}
static inline void __remove_wait_queue(wait_queue_head_t *head,                                                         wait_queue_t *old)
{
#if WAITQUEUE_DEBUG
if (!old)
WQ_BUG();
CHECK_MAGIC(old->__magic);
#endif
list_del(&old->task_list);
}
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
/*** list_del - deletes entry from list.
* @entry: the element to delete from the list
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{        
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}
上面的代码都应该比较好懂.我们先用当前进程生成了一个wait_queue_t,把 当前进程的state改成TASK_INTERRUPTIBLE,然后把这个wait_queue_t加到我们已经声明并初始化好的全局变量q中去.这时 调用shedule,current 所指到的 process 会被放到 scheduling queue 中等待被挑出来执行。执行完 schedule() 之后,current 就没办法继续执行了。而当 current 以后被 wake up 时,就会从 schedule() 之后,也就是从 SLEEP_ON_TAIL 开始执行。我们现在当然明白wake_up_interruptible所需做的是把进程的状态改成Running的,其代码如下:
--------------------------------------------------------------------------------
#define wake_up_interruptible(x)         __wake_up((x),TASK_INTERRUPTIBLE, 1)
void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
{
unsigned long flags;
if (unlikely(!q))
return;
spin_lock_irqsave(&q->lock, flags);
__wake_up_common(q, mode, nr_exclusive, 0);
spin_unlock_irqrestore(&q->lock, flags);
}
static inline void __wake_up_common(wait_queue_head_t *q, unsigned int mode, int nr_exclusive, int sync)
{
struct list_head *tmp;
unsigned int state;
wait_queue_t *curr;
task_t *p
list_for_each(tmp, &q->task_list) {
curr = list_entry(tmp, wait_queue_t, task_list);
p = curr->task;
state = p->state;
if ((state & mode) && try_to_wake_up(p, mode, sync) &&                         ((curr->flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive))
break;        
}
}
static int try_to_wake_up(task_t * p, unsigned int state, int sync)
{
unsigned long flags;
int success = 0;
long old_state;
runqueue_t *rq;
sync &= SYNC_WAKEUPS;repeat_lock_task:
rq = task_rq_lock(p, &flags);
old_state = p->state;
if (old_state & state)
{       //状态相同的就改
if (!p->array) {
/*
* Fast-migrate the task if it's not running or runnable                          * currently. Do not violate hard affinity.
*/
if (unlikely(sync&&!task_running(rq, p) && (task_cpu(p)!= smp_processor_id())&& (p->cpus_allowed & (1UL << smp_processor_id()))))
{
set_task_cpu(p, smp_processor_id());
task_rq_unlock(rq, &flags);
goto repeat_lock_task;
}
if (old_state == TASK_UNINTERRUPTIBLE)
rq->nr_uninterruptible--;
if (sync)
__activate_task(p, rq);
else {
activate_task(p, rq);
resched_task(rq->curr);
}
success = 1;
}
if (p->state >= TASK_ZOMBIE)
BUG();
p->state = TASK_RUNNING;
}
task_rq_unlock(rq, &flags);
return success;
}
由于 schedule的代码量比较大,就不贴
阅读(1637) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~