• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Ruby设置CPU的Affinity

Linux 水墨上仙 1287次浏览

Ruby设置CPU的Affinity

require 'rubygems'
require 'inline'
# By Peter Cooper - http://www.rubyinside.com/
# Oodles of inspiration and examples from
# http://www-128.ibm.com/developerworks/linux/library/l-affinity.html
class LinuxScheduler
  inline do |builder|
    builder.include '<sched.h>'
    builder.include '<stdio.h>'
    builder.c '
      int _set_affinity(int cpu_id) {
        cpu_set_t mask;
        __CPU_ZERO(&mask);
        __CPU_SET(cpu_id, &mask);
        if(sched_setaffinity(getpid(), sizeof(mask), &mask ) == -1) {
          printf("WARNING: Could not set CPU Affinity, continuing as-is\n");
          return 0;
        }
        return 1;
      }
    '
  end
  # cpu_id is 0-based, so for core/cpu 1 = 0, etc..
  def self.set_affinity(cpu_id)
    self.new._set_affinity(cpu_id.to_i)
  end
end
# Set this process's CPU affinity
LinuxScheduler.set_affinity(ARGV.first)
# Hog up all the CPU time on that processor
1000000.times { b = rand(100) ** 100 }


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Ruby设置CPU的Affinity
喜欢 (0)
加载中……