gpt4 book ai didi

perl - Perl 中的 CPU/内核数

转载 作者:行者123 更新时间:2023-12-03 21:20:59 24 4
gpt4 key购买 nike

如何在 Perl 中获取 CPU 或核心数。我希望这个来决定动态创建线程数。下面我创建了 3 个线程。但我想根据该机器中的内核数创建线程。

#!/usr/bin/perl -w
use threads;
use Thread::Semaphore;

my $semaphore = Thread::Semaphore->new();`enter code here`
my $cur_dir = "D:\\qout";
opendir( CURDIR, "$cur_dir" );
my @file_list : shared = readdir(CURDIR);
closedir(CURDIR);


$thr1 = threads->create( \&changemode, \@file_list, "th1" );
$thr2 = threads->create( \&changemode, \@file_list, "th2" );
$thr3 = threads->create( \&changemode, \@file_list, "th3" );

sub &changemode {

my ($file_list) = shift;
my ($message) = shift;
my ($i) = shift;
while (@{$file_list}) {
my $fname;
$semaphore->down();
if (@{$file_list}) {
$fname = shift(@{$file_list});
}
$semaphore->up();
print("$message got access of $fname\n");
system ("csh -fc \"chmod +w $fname\"");
#sleep (2);
}
}


$thr1->join();

$thr2->join();

$thr3->join();

最佳答案

查看 CPAN 模块,例如 Sys::Info::Device::CPU

   use Sys::Info;
use Sys::Info::Constants qw( :device_cpu );
my $info = Sys::Info->new;
my $cpu = $info->device( CPU => %options );

printf "CPU: %s\n", scalar($cpu->identify) || 'N/A';
printf "CPU speed is %s MHz\n", $cpu->speed || 'N/A';
printf "There are %d CPUs\n" , $cpu->count || 1;
printf "CPU load: %s\n" , $cpu->load || 0;

关于perl - Perl 中的 CPU/内核数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18360374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com