内核经常需要在后台执行一些操作。这种任务可以通过内核线程(kernel thread)完成------独立运行在内核空间的标准进程。内核线程和普通的进程的区别在于内核线程没有独立的地址空间(实际上指向地址空间的mm指针被设置为NULL)。 它们只在内核空间运行,从来不切换到用户空间去。内核进程和普通进程一样,可以被调度,也可以被抢占。
像flush 和 ksoftirqd 就是内核线程。
创建内核线程使用以下方法:
struct task_struct *kthread_create(int (*threadfn)(void *data),
void *data,
const char namefmt[], ...)
__attribute__((format(printf, 3, 4)));
新创建的进程处于不可运行状态,如果不通过调用wake_up_process()明确地唤醒它,它不会主动运行。
创建一个进程并让它运行起来,可以通过kthread_run()函数达到:
/**
* kthread_run - create and wake a thread.
* @threadfn: the function to run until signal_pending(current).
* @data: data ptr for @threadfn.
* @namefmt: printf-style name for the thread.
*
* Description: Convenient wrapper for kthread_create() followed by
* wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
*/
#define kthread_run(threadfn, data, namefmt, ...) \
({ \
struct task_struct *__k \
= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
if (!IS_ERR(__k)) \
wake_up_process(__k); \
__k; \
})
它只是简单的调用kthread_create() 和 wake_up_process()。
1658

被折叠的 条评论
为什么被折叠?



