blob: a098b769d580c66cdff2c35177aba91b9eb23582 [file] [log] [blame]
<html lang="en">
<head>
<title>Processor Resources - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Resource-Usage-And-Limitation.html#Resource-Usage-And-Limitation" title="Resource Usage And Limitation">
<link rel="prev" href="Memory-Resources.html#Memory-Resources" title="Memory Resources">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Processor-Resources"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Memory-Resources.html#Memory-Resources">Memory Resources</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Resource-Usage-And-Limitation.html#Resource-Usage-And-Limitation">Resource Usage And Limitation</a>
<hr>
</div>
<h3 class="section">22.5 Learn about the processors available</h3>
<p>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
<p><a name="index-g_t_005fSC_005fNPROCESSORS_005fCONF-2787"></a><a name="index-sysconf-2788"></a>
<pre class="smallexample"> sysconf (_SC_NPROCESSORS_CONF)
</pre>
<p class="noindent">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
<p><a name="index-g_t_005fSC_005fNPROCESSORS_005fONLN-2789"></a><a name="index-sysconf-2790"></a>
<pre class="smallexample"> sysconf (_SC_NPROCESSORS_ONLN)
</pre>
<p class="noindent">returns the number of processors which are currently inline (i.e.,
available).
<p>For these two pieces of information the GNU C library also provides
functions to get the information directly. The functions are declared
in <samp><span class="file">sys/sysinfo.h</span></samp>.
<!-- sys/sysinfo.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>get_nprocs_conf</b> (<var>void</var>)<var><a name="index-get_005fnprocs_005fconf-2791"></a></var><br>
<blockquote><p>The <code>get_nprocs_conf</code> function returns the number of processors the
operating system configured.
<p>This function is a GNU extension.
</p></blockquote></div>
<!-- sys/sysinfo.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>get_nprocs</b> (<var>void</var>)<var><a name="index-get_005fnprocs-2792"></a></var><br>
<blockquote><p>The <code>get_nprocs</code> function returns the number of available processors.
<p>This function is a GNU extension.
</p></blockquote></div>
<p><a name="index-load-average-2793"></a>Before starting more threads it should be checked whether the processors
are not already overused. Unix systems calculate something called the
<dfn>load average</dfn>. This is a number indicating how many processes were
running. This number is average over different periods of times
(normally 1, 5, and 15 minutes).
<!-- stdlib.h -->
<!-- BSD -->
<div class="defun">
&mdash; Function: int <b>getloadavg</b> (<var>double loadavg</var>[]<var>, int nelem</var>)<var><a name="index-getloadavg-2794"></a></var><br>
<blockquote><p>This function gets the 1, 5 and 15 minute load averages of the
system. The values are placed in <var>loadavg</var>. <code>getloadavg</code> will
place at most <var>nelem</var> elements into the array but never more than
three elements. The return value is the number of elements written to
<var>loadavg</var>, or -1 on error.
<p>This function is declared in <samp><span class="file">stdlib.h</span></samp>.
</p></blockquote></div>
</body></html>