是否可以防止子进程继承父进程的 CPU/核心关联性?
我对在 Linux 上执行有关 Java 程序的操作特别感兴趣。已经有一些问题表明您无法通过 Java 进行控制,并且一些 RFE 已被 Sun/Oracle 关闭。
如果您可以访问源代码并使用低级语言,那么您当然可以进行相关的系统调用。然而,沙盒系统——可能没有源代码——带来了更多的挑战。我本以为设置每个进程或内核参数的工具能够从父进程外部控制它。这确实是我所追求的。
我了解默认设置的原因 。看起来某些版本的 Windows 可能允许某些控制 的,但大多数没有。我本来希望 Linux 允许控制它,但似乎这不是一个选项。
I'm particularly interesting in doing this on Linux, regarding Java programs. There are already a few questions that say you have no control from Java, and some RFEs closed by Sun/Oracle.
If you have access to source code and use a low-level language, you can certainly make the relevant system calls. However, sand-boxed systems - possibly without source code - present more of a challenge. I would have thought that a tool to set this per-process or an kernel parameter are able to control this from outside the parent process. This is really what I'm after.
I understand the reason why this is the default. It looks like some version of Windows may allow some control of this, but most do not. I was expecting Linux to allow control of it, but seems like it's not an option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
您还可以做的是,在 fork() 之后更改子级与父级的亲和力。顺便说一句,我假设你使用的是 Linux,其中一些东西,例如使用 sysconf() 检索核心数量,在不同的操作系统和 unix 风格上会有所不同......这里的示例获取 cpu父进程并尝试确保所有子进程以循环方式调度在不同的核心上。
/* get the number of cpu's */
numcpu = sysconf( _SC_NPROCESSORS_ONLN );
/* get our CPU */
CPU_ZERO(&mycpuset);
sched_getaffinity( getpid() , sizeof mycpuset , &mycpuset);
for(i=0 ; i < numcpu ; i++ )
{
if(CPU_ISSET( i, &mycpuset))
{
mycpu = i;
break;
}
}
//...
while(1)
{
//Some other stuff.....
/* now the fork */
if((pid = fork()) == 0)
{
//do your child stuff
}
/* Parent... can schedule child. */
else
{
cpu = ++cpu % numcpu;
if(cpu == mycpu)
cpu = ++cpu % numcpu;
CPU_ZERO(&mycpuset);
CPU_SET(cpu,&mycpuset);
/*set processor affinity*/
sched_setaffinity(pid, sizeof mycpuset, &mycpuset );
//any other father stuff
}
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果您有足够的权限,您可以在子进程中执行之前简单地调用 setaffinity 。换句话说,从
移动到使用
[当然使用 999999 不太好,但是可以用自动确定 cpu 数量并根据需要重置关联掩码的程序来代替。]
Provided you have sufficient privileges, you could simply call setaffinity before execing in the child. In other words, from
move to use
[Of course using 999999 is not nice, but that can be substituted by a program which automatically determined the number of cpus and resets the affinity mask as desired.]