免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: nqdgj2007

什么时候改变父进程的slice的 [复制链接]

论坛徽章:
0
发表于 2008-12-28 14:17 |显示全部楼层
谢谢楼上的回答 但是2.6.24中的sched_fork是没有任何对time_slice的操作的阿
不信你们自己看看  所以我搞不懂哪里改变了子进程的时间片

论坛徽章:
0
发表于 2008-12-28 14:54 |显示全部楼层
原帖由 dreamice 于 2008-12-28 13:39 发表



ULK3中文版第126页:
sched_fork()
“为了保证公平的进程调度,该函数在父子进程之间共享父进程的时间片(参见第七章scheduler_tick()函数)”
我想应该有不共享时间片的情况,至于线程池的问题,看来我 ...

典型的断章取义,没把来龙去脉搞清楚就想当然了
Moreover, in order to keep process scheduling fair, the function shares the remaining timeslice of parent between the parent and the child (出自ULK3原版)
这明显说的是子进程在创建后第一次运行的时间数是父进程剩余的时间数。当子进程的时间片耗完后新分得的时间片必然和父进程一样多,而不是什么共享父进程的时间片。
我刚才去看了一下代码,我只有2.6.27-RC2的,变化太大,没看懂。但我肯定绝不是共享父进程时间片。因为这很容易就被推翻,如果共享,Linux系统中所有进程都共享了init的时间片,到最后进程越来越多,大家分得的时间就越来越少,最后啥活都不能干。
我以前还认为线程池是共享时间片的(当然是我自己想当然),现在想来也不是,因为时间片必然会被分尽。除非线程池中的线程并非系统中的线程,而是程序自己的概念(或许就是一个大循环轮番调用几个函数)。
版主在回答这种基本问题时,我认为应该调查清楚,否则很容易误导人,特别是新人。

如果我说错了,请大家指出。

PS:丢掉ULK中文版吧,看那书除了误导没啥用处。

[ 本帖最后由 zx_wing 于 2008-12-28 14:57 编辑 ]

论坛徽章:
0
发表于 2008-12-28 14:56 |显示全部楼层
原帖由 nqdgj2007 于 2008-12-28 14:17 发表
谢谢楼上的回答 但是2.6.24中的sched_fork是没有任何对time_slice的操作的阿
不信你们自己看看  所以我搞不懂哪里改变了子进程的时间片

为什么要改变子进程时间片?
子进程创建之初继承父进程剩余的时间片,耗完后就和其它进程一样,调度器为它分配新时间片,其值与父进程相同

论坛徽章:
0
发表于 2008-12-28 15:50 |显示全部楼层
每人回答我么

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
发表于 2008-12-28 15:51 |显示全部楼层
原帖由 zx_wing 于 2008-12-28 14:54 发表

典型的断章取义,没把来龙去脉搞清楚就想当然了
Moreover, in order to keep process scheduling fair, the function shares the remaining timeslice of parent between the parent and the child (出自UL ...


我没有看原版,如果这个是我理解错了,我表示歉意。

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
发表于 2008-12-28 16:07 |显示全部楼层
原帖由 zx_wing 于 2008-12-28 14:54 发表

典型的断章取义,没把来龙去脉搞清楚就想当然了
Moreover, in order to keep process scheduling fair, the function shares the remaining timeslice of parent between the parent and the child (出自UL ...


When a new process is created, sched_fork( ), invoked by copy_process( ), sets the time_slice field of both current (the parent) and p (the child) processes in the following way:

p->time_slice = (current->time_slice + 1) >> 1;
current->time_slice >>= 1;


In other words, the number of ticks left to the parent is split in two halves: one for the parent and one for the child. This is done to prevent users from getting an unlimited amount of CPU time by using the following method: the parent process creates a child process that runs the same code and then kills itself; by properly adjusting the creation rate, the child process would always get a fresh quantum before the quantum of its parent expires. This programming trick does not work because the kernel does not reward forks. Similarly, a user cannot hog an unfair share of the processor by starting several background processes in a shell or by opening a lot of windows on a graphical desktop. More generally speaking, a process cannot hog resources (unless it has privileges to give itself a real-time policy) by forking multiple descendents.

If the parent had just one tick left in its time slice, the splitting operation forces current->time_slice to 0, thus exhausting the quantum of the parent. In this case, copy_process( ) sets current->time_slice back to 1, then invokes scheduler_tick( ) to decrease the field (see the following section).


这一段是英文原版的。我理解的意思是,一个父进程在创建一个子进程的时候,在第一次运行时,父子进程共享父进程剩余的时间片,直到他们用完时间片,进入过期队列。当第二次被调度的时候,那么他们才会拥有各自独立的时间片。我前面说的意思,指的是第一次被调度的情况,至于更深入的情况,确实没有细节的研究代码。
如果理解有错,zx_wing兄请多包涵。看来以后得慎言……

论坛徽章:
0
发表于 2008-12-28 16:25 |显示全部楼层
楼上在2.6.24以上版本 hed_fork()已经没有这些操作了 包括不调用scheduler_tick()函数 ULK3讲的是2.6.11  之后改动很大

论坛徽章:
0
发表于 2008-12-28 19:56 |显示全部楼层
原帖由 dreamice 于 2008-12-28 16:07 发表





这一段是英文原版的。我理解的意思是,一个父进程在创建一个子进程的时候,在第一次运行时,父子进程共享父进程剩余的时间片,直到他们用完时间片,进入过期队列。当第二次被调度的时候,那么他们才会 ...

我可能说话时候直了点哈,目的只是想把问题讨论清楚。
ULK真翻译的很烂,建议不要看

论坛徽章:
0
发表于 2008-12-28 22:06 |显示全部楼层
呵呵 

论坛徽章:
0
发表于 2008-12-29 10:00 |显示全部楼层
原帖由 zx_wing 于 2008-12-28 12:32 发表

是theard情况吗?说实话我一直不清楚Linux能不能实现线程池,因为我不知道theard是否共享时间片,我一直认为是不共享的。
clone时传什么参数表示共享时间片呢?假设我有1000个子线程,都共享时间片,那么一个 ...



如果频繁调度,当然会消耗很多的cpu资源。
但是线程是在什么时候有用呢? 有可能需要等待外部事件,通过多线程并发提高cpu的利用率。

如果cpu一直都在忙,那多线程就没有必要了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP