简单的介绍一下内核线程函数:kernel_thread:
- #include <linux/module.h>
-
#include <linux/sched.h>
-
#include <linux/pid.h>
-
-
MODULE_LICENSE("GPL");
-
-
int my_thread(void *);
-
-
static int __init kthread_init(void)
-
{
-
int result;
-
-
printk("into kenrnel thread init...\n");
-
result = kernel_thread(my_thread, NULL, CLONE_KERNEL);
-
printk("kernel thread result is: %d\n", result);
-
printk("<0>0 current pid is: %d, tgid is: %d\n", current->pid, current->tgid);
-
-
return 0;
-
}
-
-
int my_thread(void *arg)
-
{
-
printk("in the kernel thread function...\n");
-
printk("<1>1 current pid is: %d, tgid is: %d\n", current->pid, current->tgid);
-
-
return 0;
-
}
-
-
static void __exit kthread_exit(void)
-
{
-
printk("kernel thread quit...\n");
-
}
-
-
module_init(kthread_init);
-
module_exit(kthread_exit);
-
-
MODULE_AUTHOR("ZHAOQIAO");
下面是Makefile文件:
- obj-m := kthread.o
-
-
CURRENT_PATH = $(shell pwd)
-
KERNEL_PATH = $(shell uname -r)
-
-
kernel_path = /usr/src/linux-headers-$(KERNEL_PATH)/
-
-
all:
-
make -C $(kernel_path) M=$(CURRENT_PATH) modules
-
clean:
-
make -C $(kernel_path) M=$(CURRENT_PATH) clean
这样运行后的结果为:
[16546.959065] into kenrnel thread init...
[16546.959086] kernel thread result is: 12107
[16546.959088] 0 current pid is: 12105, tgid is: 12105
[16546.959502] in the kernel thread function...
[16546.959504] 1 current pid is: 12107, tgid is: 12107
[16549.485885] kernel thread quit...
这里的0和1两个线程为什么他们的tgid不一样呢?(PS:正在查。。。)
阅读(2194) | 评论(0) | 转发(0) |