系统调用—sysconf

代码:

 static inline unsigned int getCoresNum(void)
{
    return sysconf(_SC_NPROCESSORS_ONLN);
}

The use of threads or processes with shared memory allows an application to take advantage of all the processing power a system can provide. If the task can be parallelized the optimal way to write an application is to have at any time as many processes running as there are processors. To determine the number of processors available to the system one can run

  sysconf (_SC_NPROCESSORS_CONF)

which returns the number of processors the operating system configured. But it might be possible for the operating system to disable individual processors and so the call

 

  sysconf (_SC_NPROCESSORS_ONLN

returns the number of processors which are currently online (i.e., available).

或者直接调用linux提供的函数:

Function:  int  get_nprocs_conf  (void)

Preliminary: | MT-Safe | AS-Unsafe heap lock | AC-Unsafe lock fd mem | See POSIX Safety Concepts.

The get_nprocs_conf function returns the number of processors the operating system configured.

This function is a GNU extension.

Function:  int  get_nprocs  (void)

Preliminary: | MT-Safe | AS-Safe | AC-Safe fd | See POSIX Safety Concepts.

The get_nprocs function returns the number of available processors.

This function is a GNU extension.

Before starting more threads it should be checked whether the processors are not already overused. Unix systems calculate something called the load average. This is a number indicating how many processes were running. This number is an average over different periods of time (normally 1, 5, and 15 minutes).

Function:  int  getloadavg  (double loadavg[], int nelem)

Preliminary: | MT-Safe | AS-Safe | AC-Safe fd | See POSIX Safety Concepts.

This function gets the 1, 5 and 15 minute load averages of the system. The values are placed in loadavggetloadavg will place at most nelem elements into the array but never more than three elements. The return value is the number of elements written to loadavg, or -1 on error.

This function is declared in stdlib.h.

posted on 2019-02-22 16:03 Rex_Zhang 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/rex-2018-cloud/p/10418945.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值