Perl中的CPU /内核数
发布时间:2020-12-15 21:30:28 所属栏目:大数据 来源:网络整理
导读:如何获取Perl中的CPU或内核数量.我想要这样做,决定动态创建线程数.下面我创建了3个线程.但是我想根据该机器中的核心数创建线程. #!/usr/bin/perl -wuse threads;use Thread::Semaphore;my $semaphore = Thread::Semaphore-new();`enter code here`my $cur_di
如何获取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,"th2" ); $thr3 = threads->create( &;changemode,"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 $fnamen"); 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: %sn",scalar($cpu->identify) || 'N/A'; printf "CPU speed is %s MHzn",$cpu->speed || 'N/A'; printf "There are %d CPUsn",$cpu->count || 1; printf "CPU load: %sn",$cpu->load || 0; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |