Download code

Jump to: navigation, search

Back to CPU_usage_(C,_Windows_XP)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

build.log

1 /tmp/litprog3126709/cpuusage.c:16:21: fatal error: windows.h: No such file or directory
2 compilation terminated.


cpuusage.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/CPU_usage_(C,_Windows_XP)?oldid=19027
14 */
15 
16 #include <windows.h>
17 #include <stdio.h>
18 
19 //------------------------------------------------------------------------------------------------------------------
20 // Prototype(s)...
21 //------------------------------------------------------------------------------------------------------------------
22 CHAR cpuusage(void);
23 
24 //----------------------------------------------------------------------------------------------------------------
25 // cpuusage(void)
26 // ==============
27 // Return a CHAR value in the range 0 - 100 representing actual CPU usage in percent.
28 //----------------------------------------------------------------------------------------------------------------
29 
30 CHAR cpuusage()
31 {
32   FILETIME               ft_sys_idle;
33   FILETIME               ft_sys_kernel;
34   FILETIME               ft_sys_user;
35 
36   ULARGE_INTEGER         ul_sys_idle;
37   ULARGE_INTEGER         ul_sys_kernel;
38   ULARGE_INTEGER         ul_sys_user;
39 
40   static ULARGE_INTEGER	 ul_sys_idle_old;
41   static ULARGE_INTEGER  ul_sys_kernel_old;
42   static ULARGE_INTEGER  ul_sys_user_old;
43 
44   CHAR  usage = 0;
45 
46   GetSystemTimes(&ft_sys_idle,    /* System idle time */
47                  &ft_sys_kernel,  /* system kernel time */
48                  &ft_sys_user);   /* System user time */
49 
50   CopyMemory(&ul_sys_idle  , &ft_sys_idle  , sizeof(FILETIME)); // Could been optimized away...
51   CopyMemory(&ul_sys_kernel, &ft_sys_kernel, sizeof(FILETIME)); // Could been optimized away...
52   CopyMemory(&ul_sys_user  , &ft_sys_user  , sizeof(FILETIME)); // Could been optimized away...
53 
54   usage  =
55           (
56            (
57             (
58              (
59               (ul_sys_kernel.QuadPart - ul_sys_kernel_old.QuadPart)+
60               (ul_sys_user.QuadPart   - ul_sys_user_old.QuadPart)
61              )
62              -
63              (ul_sys_idle.QuadPart-ul_sys_idle_old.QuadPart)
64             )
65             *
66             (100)
67            )
68            /
69            (
70             (ul_sys_kernel.QuadPart - ul_sys_kernel_old.QuadPart)+
71             (ul_sys_user.QuadPart   - ul_sys_user_old.QuadPart)
72            )
73           );
74 
75   ul_sys_idle_old.QuadPart   = ul_sys_idle.QuadPart;
76   ul_sys_user_old.QuadPart   = ul_sys_user.QuadPart;
77   ul_sys_kernel_old.QuadPart = ul_sys_kernel.QuadPart;
78 
79   return usage;
80 }
81 //------------------------------------------------------------------------------------------------------------------
82 // Entry point
83 //------------------------------------------------------------------------------------------------------------------
84 int main(void)
85 {
86   int n;
87   for(n=0;n<20;n++)
88   {
89     printf("CPU Usage: %3d%%\r",cpuusage());
90     sleep(500);
91   }
92   printf("\n");
93   return 0;
94 }
95 
96 


hijacker
hijacker
hijacker
hijacker