是否可以防止子级继承父级的CPU/核心亲缘关系? [英] Is it possible to prevent children inheriting the CPU/core affinity of the parent?

查看:84
本文介绍了是否可以防止子级继承父级的CPU/核心亲缘关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Java程序,我在Linux上这样做特别有趣.已经有一些问题表明您无法使用Java进行控制,还有一些RFE已被Sun/Oracle关闭.

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.

我了解原因,这是默认设置的原因.看起来某些版本的Windows 可能允许某些控制,但大多数不是.我原以为Linux可以控制它,但是这不是一个选择. /p>

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.

推荐答案

您还可以做的是,在fork()之后更改子级与父级之间的亲和力.顺便说一句,我假设您使用的是Linux,其中的某些内容(例如,使用sysconf()检索内核数在不同的OS和Unix风格上将有所不同...).此处的示例获取了CPU父进程,并尝试确保所有子进程都以循环方式安排在不同的内核上.

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
   }
}

这篇关于是否可以防止子级继承父级的CPU/核心亲缘关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆