Obtain the C code for CPU usage-general Linux technology-Linux programming and kernel information. The following is a detailed description.
CODE: # include # Include # Include # Define SystemBasicInformation 0 # Define SystemPerformanceInformation 2 # Define SystemTimeInformation 3 # Define Li2Double (x) (double) (x). HighPart) * 4.294967296E9 + (double) (x). LowPart )) Typedef struct { DWORD dwUnknown1; ULONG uKeMaximumIncrement; ULONG uPageSize; ULONG uMmNumberOfPhysicalPages; ULONG uMmLowestPhysicalPage; ULONG uMmHighestPhysicalPage; ULONG uAllocationGranularity; PVOID pLowestUserAddress; PVOID pMmHighestUserAddress; ULONG uKeActiveProcessors; BYTE bKeNumberProcessors; BYTE bUnknown2; WORD wUnknown3; } SYSTEM_BASIC_INFORMATION; Typedef struct { LARGE_INTEGER liIdleTime; DWORD dwSpare [76]; } SYSTEM_PERFORMANCE_INFORMATION; Typedef struct { LARGE_INTEGER liKeBootTime; LARGE_INTEGER liKeSystemTime; LARGE_INTEGER liExpTimeZoneBias; ULONG uCurrentTimeZoneId; DWORD dwReserved; } SYSTEM_TIME_INFORMATION; Typedef LONG (WINAPI * PROCNTQSI) (UINT, PVOID, ULONG, PULONG ); PROCNTQSI NtQuerySystemInformation; Void main () { SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo; SYSTEM_TIME_INFORMATION extends imeinfo; SYSTEM_BASIC_INFORMATION SysBaseInfo; Double dbIdleTime; Double dbSystemTime; LONG status; LARGE_INTEGER liOldIdleTime = {0, 0 }; LARGE_INTEGER liOldSystemTime = {0, 0 }; NtQuerySystemInformation = (PROCNTQSI) GetProcAddress (GetModuleHandle ("ntdll"), "NtQuerySystemInformation "); If (! NtQuerySystemInformation) return; // Get number of processors in the system Status = NtQuerySystemInformation (SystemBasicInformation, & SysBaseInfo, sizeof (SysBaseInfo), NULL ); If (status! = NO_ERROR) return; Int amount = 0; // Printf ("\ n your CPU usage is :"); While (! _ Kbhit ()) { // Set the new system time: Amount ++; Status = NtQuerySystemInformation (SystemTimeInformation, & policimeinfo, sizeof (policimeinfo), 0 ); If (status! = NO_ERROR) return; Status = NtQuerySystemInformation (SystemPerformanceInformation, & SysPerfInfo, sizeof (SysPerfInfo), NULL ); If (status! = NO_ERROR) return; If (liOldIdleTime. QuadPart! = 0) { DbIdleTime = Li2Double (SysPerfInfo. liIdleTime)-Li2Double (liOldIdleTime ); DbSystemTime = Li2Double (SysTimeInfo. liKeSystemTime)-Li2Double (liOldSystemTime ); DbIdleTime = dbIdleTime/dbSystemTime; DbIdleTime = 100.0-dbIdleTime * 100.0/(double) SysBaseInfo. bKeNumberProcessors; Int c = (int) dbIdleTime; Printf ("\ B \ B \ B % d --- % d % ", amount, c ); } // Change the new CPU usage and system time LiOldIdleTime = SysPerfInfo. liIdleTime; LiOldSystemTime = policimeinfo. liKeSystemTime; Sleep (1000 ); } Printf ("\ n "); } |