是否可以防止子级继承父级的CPU/核心亲缘关系?

编程入门 行业动态 更新时间:2024-10-28 02:30:44
本文介绍了是否可以防止子级继承父级的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/核心亲缘关系?

本文发布于:2023-10-26 21:39:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:亲缘   可以防止   核心   关系   CPU

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!