免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 18740 | 回复: 57
打印 上一主题 下一主题

难题:内核线程讨论 [复制链接]

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-11 11:17 |只看该作者 |倒序浏览
5可用积分
1. ps 出来的哪些是 内核线程?

2. 内核线程 是不是 独立存在的, 跟某个进程不关的?

3. 内核线程是怎样增加的?

论坛徽章:
0
2 [报告]
发表于 2009-02-11 11:50 |只看该作者
1. ps -elf 的结果中最后面一栏,如果CMD是用[ ] 包含起来的就为内核线程,如[ksoftirqd/1]等
2. 嗯,内核线程只运行在内核态,与普通的进程无关。
3. 使用函数kernel_thread创建新的内核线程。

ULK上面讲到:
3.4.2. Kernel Threads
Traditional Unix systems delegate some critical tasks to intermittently running processes, including flushing disk caches, swapping out unused pages, servicing network connections, and so on. Indeed, it is not efficient to perform these tasks in strict linear fashion; both their functions and the end user processes get better response if they are scheduled in the background. Because some of the system processes run only in Kernel Mode, modern operating systems delegate their functions to kernel threads , which are not encumbered with the unnecessary User Mode context. In Linux, kernel threads differ from regular processes in the following ways:

Kernel threads run only in Kernel Mode, while regular processes run alternatively in Kernel Mode and in User Mode.

Because kernel threads run only in Kernel Mode, they use only linear addresses greater than PAGE_OFFSET. Regular processes, on the other hand, use all four gigabytes of linear addresses, in either User Mode or Kernel Mode.

3.4.2.1. Creating a kernel thread
The kernel_thread( ) function creates a new kernel thread. It receives as parameters the address of the kernel function to be executed (fn), the argument to be passed to that function (arg), and a set of clone flags (flags). The function essentially invokes do_fork( ) as follows:

    do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, pregs, 0, NULL, NULL);


The CLONE_VM flag avoids the duplication of the page tables of the calling process: this duplication would be a waste of time and memory, because the new kernel thread will not access the User Mode address space anyway. The CLONE_UNTRACED flag ensures that no process will be able to trace the new kernel thread, even if the calling process is being traced.

...

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
3 [报告]
发表于 2009-02-11 17:37 |只看该作者
谢谢

第三个问题,可能是我一时不知怎样表达,现整理这样:

一个进程对应一个内核线程(多线程除外),当进程exit后,这个内核线程会不会也exit?

论坛徽章:
0
4 [报告]
发表于 2009-02-11 17:47 |只看该作者
原帖由 chenzhanyiczy 于 2009-2-11 17:37 发表
谢谢

第三个问题,可能是我一时不知怎样表达,现整理这样:

一个进程对应一个内核线程(多线程除外),当进程exit后,这个内核线程会不会也exit?


内核线程是在内核态创建的, 和普通的进程没有对应关系. 所以谈不上进程exit后, 内核线程也exit.

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
5 [报告]
发表于 2009-02-11 18:48 |只看该作者
这样的话,进程通过什么手段和一个内核线程联系在一起的?能否大概地表述一下

论坛徽章:
0
6 [报告]
发表于 2009-02-11 19:01 |只看该作者
原帖由 chenzhanyiczy 于 2009-2-11 18:48 发表
这样的话,进程通过什么手段和一个内核线程联系在一起的?能否大概地表述一下


see 例子: http://www.scs.ch/~frey/linux/kernelthreads.html

论坛徽章:
0
7 [报告]
发表于 2009-02-11 19:18 |只看该作者
原帖由 chenzhanyiczy 于 2009-2-11 18:48 发表
这样的话,进程通过什么手段和一个内核线程联系在一起的?能否大概地表述一下

内核线程只运行于内核态, 是为内核中的某些任务进行周期性地执行.

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
8 [报告]
发表于 2009-02-11 19:36 |只看该作者
我知道 内核线程只运行于内核态,只是不明白 进程怎样关联到内核线程?
进程的运行其实是通过内核线程实现的,但这个进程是怎样过度到内核线程的,让内核线程去执行代码?

论坛徽章:
0
9 [报告]
发表于 2009-02-11 19:40 |只看该作者
原帖由 scutan 于 2009-2-11 19:18 发表

内核线程只运行于内核态, 是为内核中的某些任务进行周期性地执行.


说的是,我理解LZ可能是问与内核线程关连的方面.

一个是在如驱动等程序中可以创建.(即在内核态创建)
另一个是用户进程会发出一些如IO类的请求(即在用户态发出请求),这样酒会有内核thread帮住用户进程去处理一些操作.

论坛徽章:
0
10 [报告]
发表于 2009-02-11 19:42 |只看该作者
原帖由 chenzhanyiczy 于 2009-2-11 19:36 发表
我知道 内核线程只运行于内核态,只是不明白 进程怎样关联到内核线程?
进程的运行其实是通过内核线程实现的,但这个进程是怎样过度到内核线程的,让内核线程去执行代码?


比如:用户进程在用户态发出一些必须有内河完成的动作(如IO等),那么这时候就会把执行流程转入到核心态,就有可能由核心thread去做些事情.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP