Chinaunix首页 | 论坛 | 博客
  • 博客访问: 560004
  • 博文数量: 61
  • 博客积分: 2438
  • 博客等级: 大尉
  • 技术积分: 871
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-28 08:04
文章分类
文章存档

2013年(1)

2012年(8)

2011年(15)

2010年(37)

分类: LINUX

2011-10-11 21:06:18

简单的介绍一下内核线程函数:kernel_thread:
  1. #include <linux/module.h>
  2. #include <linux/sched.h>
  3. #include <linux/pid.h>

  4. MODULE_LICENSE("GPL");

  5. int my_thread(void *);

  6. static int __init kthread_init(void)
  7. {
  8.     int result;

  9.     printk("into kenrnel thread init...\n");
  10.     result = kernel_thread(my_thread, NULL, CLONE_KERNEL);
  11.     printk("kernel thread result is: %d\n", result);
  12.     printk("<0>0 current pid is: %d, tgid is: %d\n", current->pid, current->tgid);

  13.     return 0;
  14. }

  15. int my_thread(void *arg)
  16. {
  17.     printk("in the kernel thread function...\n");
  18.     printk("<1>1 current pid is: %d, tgid is: %d\n", current->pid, current->tgid);

  19.     return 0;
  20. }

  21. static void __exit kthread_exit(void)
  22. {
  23.     printk("kernel thread quit...\n");
  24. }

  25. module_init(kthread_init);
  26. module_exit(kthread_exit);

  27. MODULE_AUTHOR("ZHAOQIAO");
下面是Makefile文件:
  1. obj-m := kthread.o

  2. CURRENT_PATH = $(shell pwd)
  3. KERNEL_PATH = $(shell uname -r)

  4. kernel_path = /usr/src/linux-headers-$(KERNEL_PATH)/

  5. all:
  6.     make -C $(kernel_path) M=$(CURRENT_PATH) modules
  7. clean:
  8.     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:正在查。。。)
阅读(2075) | 评论(0) | 转发(0) |
0

上一篇:taglist的使用

下一篇:linux启动分析-图

给主人留下些什么吧!~~