内核线程学习笔记总结5
来源:互联网 发布:MP3音序软件 编辑:程序博客网 时间:2024/05/19 07:09
struct task_struct *kthread_create(int (*threadfn)(void *data),
void *data,
const char namefmt[],
...)
{
struct kthread_create_info create;
create.threadfn = threadfn;
create.data = data;
init_completion(&create.started);
init_completion(&create.done);
spin_lock(&kthread_create_lock);
list_add_tail(&create.list, &kthread_create_list);
spin_unlock(&kthread_create_lock);
wake_up_process(kthreadd_task);
wait_for_completion(&create.done);
if (!IS_ERR(create.result)) {
va_list args;
va_start(args, namefmt);
vsnprintf(create.result->comm, sizeof(create.result->comm),
namefmt, args);
va_end(args);
}
return create.result;
将kthread_create_info挂到全局队列,然后换醒进程kthreadd_task
static void noinline __init_refok rest_init(void)
__releases(kernel_lock)
{
int pid;
kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
numa_default_policy();
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
unlock_kernel();
/*
* The boot idle thread must execute schedule()
* at least once to get things moving:
*/
init_idle_bootup_task(current);
preempt_enable_no_resched();
schedule();
preempt_disable();
/* Call into cpu_idle with preempt disabled */
cpu_idle();
}
从上面可以看出就是kthreadd,这个是干嘛?
int kthreadd(void *unused)
{
struct task_struct *tsk = current;
/* Setup a clean context for our children to inherit. */
set_task_comm(tsk, "kthreadd");
ignore_signals(tsk);
set_user_nice(tsk, KTHREAD_NICE_LEVEL);
set_cpus_allowed_ptr(tsk, CPU_MASK_ALL_PTR);
current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;
for (;;) {
set_current_state(TASK_INTERRUPTIBLE);
if (list_empty(&kthread_create_list))
schedule();
__set_current_state(TASK_RUNNING);
spin_lock(&kthread_create_lock);
while (!list_empty(&kthread_create_list)) {
struct kthread_create_info *create;
create = list_entry(kthread_create_list.next,
struct kthread_create_info, list);
list_del_init(&create->list);
spin_unlock(&kthread_create_lock);
create_kthread(create);
spin_lock(&kthread_create_lock);
}
spin_unlock(&kthread_create_lock);
}
return 0;
}
从这个函数里面 create_kthread(create);如果这个kthread_create_list为空,那么这个内核线程一直睡。
static void create_kthread(struct kthread_create_info *create)
{
int pid;
/* We want our own signal handler (we take no signals by default). */
pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
if (pid < 0) {
create->result = ERR_PTR(pid);
} else {
struct sched_param param = { .sched_priority = 0 };
wait_for_completion(&create->started);
read_lock(&tasklist_lock);
create->result = find_task_by_pid_ns(pid, &init_pid_ns);
read_unlock(&tasklist_lock);
/*
* root may have changed our (kthreadd's) priority or CPU mask.
* The kernel thread should not inherit these properties.
*/
sched_setscheduler(create->result, SCHED_NORMAL, ¶m);
set_user_nice(create->result, KTHREAD_NICE_LEVEL);
set_cpus_allowed_ptr(create->result, CPU_MASK_ALL_PTR);
}
complete(&create->done);
}
可以看到kernel_thread的故事。
- 内核线程学习笔记总结5
- 内核线程学习笔记总结1
- 内核线程学习笔记总结2
- 内核线程学习笔记总结3
- 内核线程学习笔记总结4
- 内核线程学习笔记总结6
- 内核线程学习笔记
- Linux学习笔记之内核线程
- 【学习笔记】原子位|内核线程
- java学习笔记之线程操作方法总结
- [原创]W2k Driving 学习笔记(一)内核线程及同步
- 学习笔记——JAVA线程<7>线程的总结
- 内核线程与用户线程的一点总结 《程序员的自我修养》·笔记
- 内核sk_buff工作线程总结
- 线程同步 总结 内核对象
- 内核笔记:内核线程的创建
- KTHREAD 线程调度 SDT TEB SEH shellcode中DLL模块机制动态获取 《寒江独钓》内核学习笔记(5)
- 内核阶段学习总结
- ora-01789:query block has incorrect number of result columns 错误
- 关于C++类库KYLib: String 替换函数源码
- 数组参数
- Ajax中同步与异步区别的直观性说明
- Qt二进制安装文件下载地址
- 内核线程学习笔记总结5
- wine install
- JSF参数传递方式说明
- 创建Connection对象并连接数据库
- Ubuntu 下构建基于 mysql 管理的 pure-ftpd
- 如何解决MFCO42D.dll 0xC0000005:Access Violation问题
- visitor访问者模式及dom4j中使用(二)
- strcpy
- ADODB连接数据库,通过msado15组件实现,VC2005