是否可以防止子进程继承父进程的 CPU/核心关联性?

发布于 10-10 09:34 字数 552 浏览 7 评论 0原文

我对在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔2024-10-17 09:34:53

如果您有足够的权限,您可以在子进程中执行之前简单地调用 setaffinity 。换句话说,从

if (fork() == 0)
        execve("prog", "prog", ...);

移动到使用

/* simple example using taskset rather than setaffinity directly */
if (fork() == 0)
        execve("taskset", "taskset", "-c", "0-999999", ...);

[当然使用 999999 不太好,但是可以用自动确定 cpu 数量并根据需要重置关联掩码的程序来代替。]

Provided you have sufficient privileges, you could simply call setaffinity before execing in the child. In other words, from

if (fork() == 0)
        execve("prog", "prog", ...);

move to use

/* simple example using taskset rather than setaffinity directly */
if (fork() == 0)
        execve("taskset", "taskset", "-c", "0-999999", ...);

[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.]

万水千山粽是情ミ2024-10-17 09:34:53

您还可以做的是,在 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
   }
}

What you could also do, is change the affinity of the child from the parent, after the fork(). By the way, I'm assuming you're on linux, some of this stuff, such as retrieving the number of cores with sysconf() will be different on different OS's and unix flavors.... The example here, gets the cpu of the parent process and tries to ensure all child processes are scheduled on a different core, in round robin.

/* 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
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文