微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > linux内核中一些常用的数据结构和操作

linux内核中一些常用的数据结构和操作

时间:08-21 来源:互联网 点击:

醒后操作完再睡眠;或者加入等待队列通过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不再难懂)

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top