linux内核中一些常用的数据结构和操作
醒后操作完再睡眠;或者加入等待队列通过schedule()调度获得执行时间。总之是不能一直占着 CPU。以下是内核线程的一个实例,取自kernel/context.c:int start_context_thread(void)
{
static struct completion startup __initdata = COMPLETION_INITIALIZER(startup);kernel_thread(context_thread, startup, CLONE_FS | CLONE_FILES);
wait_for_completion(startup);
return 0;
}static int context_thread(void *startup)
{
struct task_struct *curtask = current;
DECLARE_WAITQUEUE(wait, curtask);
struct k_sigaction sa;daemonize();
strcpy(curtask->comm, keventd);
keventd_running = 1;
keventd_task = curtask;spin_lock_irq(curtask->sigmask_lock);
siginitsetinv(curtask->blocked, sigmask(SIGCHLD));
recalc_sigpending(curtask);
spin_unlock_irq(curtask->sigmask_lock);complete((struct completion *)startup);/* Install a handler so SIGCLD is delivered */
sa.sa.sa_handler = SIG_IGN;
sa.sa.sa_flags = 0;
siginitset(sa.sa.sa_mask, sigmask(SIGCHLD));
do_sigaction(SIGCHLD, sa, (struct k_sigaction *)0);/*
* If one of the functions on a task queue re-adds itself
* to the task queue we call schedule() in state TASK_RUNNING
*/
for (;;) {
set_task_state(curtask, TASK_INTERRUPTIBLE);
add_wait_queue(context_task_wq, wait);
if (TQ_ACTIVE(tq_context))
set_task_state(curtask, TASK_RUNNING);
schedule();
remove_wait_queue(context_task_wq, wait);
run_task_queue(tq_context);
wake_up(context_task_done);
if (signal_pending(curtask)) {
while (waitpid(-1, (unsigned int *)0, __WALL|WNOHANG) > 0)
;
spin_lock_irq(curtask->sigmask_lock);
flush_signals(curtask);
recalc_sigpending(curtask);
spin_unlock_irq(curtask->sigmask_lock);
}
}
}6. 结构地址在C中,结构地址和结构中第一个元素的地址是相同的,因此在linux内核中经常出现使用结构第一个元素的地址来表示结构地址的情况,在读代码时要注意这一点,这和list_entry宏的意思一样。如:
struct my_struct{
int a;
int b;
}c;if(c == c.a){ // always true
...
}
linux操作系统文章专题:linux操作系统详解(linux不再难懂)
- 队列(JAVA实现)“>数据结构->队列(JAVA实现)(12-01)
- 栈(C实现)“>数据结构->栈(C实现)(12-01)
- 如何优化C语言(单片机) ?(05-13)
- 教你如何成为编程高手(05-06)
- 如何提高自己的编程水平(04-11)
- VxWorks实时操作系统下MPC8260ATM驱动的实现(11-11)