mirror of
https://github.com/gatieme/LDD-LinuxDeviceDrivers.git
synced 2026-08-19 19:14:17 +08:00
内核线程的学习...
This commit is contained in:
@@ -223,6 +223,7 @@ struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
|
||||
#define kthread_create(threadfn, data, namefmt, arg...) \
|
||||
kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
|
||||
```
|
||||
|
||||
创建内核更常用的方法是辅助函数kthread_create,该函数创建一个新的内核线程。最初线程是停止的,需要使用wake_up_process启动它。
|
||||
|
||||
|
||||
@@ -251,3 +252,4 @@ struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
|
||||
```
|
||||
|
||||
使用kthread_run,与kthread_create不同的是,其创建新线程后立即唤醒它,其本质就是先用kthread_create创建一个内核线程,然后通过wake_up_process唤醒它
|
||||
|
||||
|
||||
@@ -6,15 +6,18 @@
|
||||
#include <linux/delay.h> // error: implicit declaration of function ‘msleep’
|
||||
|
||||
|
||||
static struct task_struct *tsk;
|
||||
static struct task_struct *ptask;
|
||||
|
||||
static int thread_function(void *data)
|
||||
{
|
||||
int time_count = 0;
|
||||
do {
|
||||
do
|
||||
{
|
||||
printk(KERN_INFO "thread_function: %d times", ++time_count);
|
||||
msleep(1000);
|
||||
}while(!kthread_should_stop() && time_count<=30);
|
||||
}while(!kthread_should_stop()
|
||||
&& time_count<=30);
|
||||
|
||||
return time_count;
|
||||
}
|
||||
|
||||
@@ -22,9 +25,9 @@ static int test_kthread_run_init(void)
|
||||
{
|
||||
printk(KERN_INFO "test_kthread_run, world!\n");
|
||||
|
||||
tsk = kthread_run(thread_function, NULL, "mythread%d", 1);
|
||||
ptask = kthread_run(thread_function, NULL, "mythread%d", 1);
|
||||
|
||||
if (IS_ERR(tsk))
|
||||
if (IS_ERR(ptask))
|
||||
{
|
||||
printk(KERN_INFO "create kthread failed!\n");
|
||||
}
|
||||
@@ -39,9 +42,10 @@ static int test_kthread_run_init(void)
|
||||
static void test_kthread_run_exit(void)
|
||||
{
|
||||
printk(KERN_INFO "test_kthread_run, exit!\n");
|
||||
if (!IS_ERR(tsk))
|
||||
|
||||
if (!IS_ERR(ptask))
|
||||
{
|
||||
int ret = kthread_stop(tsk);
|
||||
int ret = kthread_stop(ptask);
|
||||
printk(KERN_INFO "thread function has run %ds\n", ret);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
Linux进程启动过程do_execve详解
|
||||
=======
|
||||
|
||||
|
||||
| 日期 | 内核版本 | 架构| 作者 | GitHub| CSDN |
|
||||
| ------------- |:-------------:|:-------------:|:-------------:|:-------------:|:-------------:|
|
||||
| 2016-06-04 | [Linux-4.5](http://lxr.free-electrons.com/source/?v=4.5) | X86 & arm | [gatieme](http://blog.csdn.net/gatieme) | [LinuxDeviceDrivers](https://github.com/gatieme/LDD-LinuxDeviceDrivers) | [Linux进程管理与调度-之-进程的描述](http://blog.csdn.net/gatieme/article/category/6225543) |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user