Find the number of processors in the system and check their status

Source: Internet
Author: User

# Include <windows. h>
# Include <stdio. h>
# Include <conio. h>
Int main (INT argc, char * argv [])
{
// Initialize the system_info Data Structure
System_info Si;
Getsysteminfo (& Si );
Printf ("currently there are % d processors. /N ", Si. dwnumberofprocessors );
// Check the activity status of each processor
For (DWORD dwpro = 0; dwpro <Si. dwnumberofprocessors; dwpro ++)
{
Printf ("processor % d", dwpro );
If (Si. dwactiveprocessormask & (1 <dwpro ))! = 0)
{
Printf ("active. /N ");
}
Else
{
Printf ("in sleep state. /N ");
}
}
Getch ();
Return 0;
}

 

CPU usage detection (from C ++ builder study-http://www.ccrun.com/article.asp? I = 424 & D = 7jw23a)

# Include <windows. h>
# Include <conio. h>
# Include <stdio. h>

# 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;

// NTDLL! Ntquerysysteminformation (NT specific !)
//
// The function copies the system information of
// Specified type into a buffer
//
// Ntsysapi
// Ntstatus
// Ntapi
// Ntquerysysteminformation (
// In uint systeminformationclass, // information type
// Out pvoid systeminformation, // pointer to buffer
// In ulong systeminformationlength, // buffer size in bytes
// Out Pulong returnlength optional // pointer to a 32-bit
/// Variable that generated es
/// The number of bytes
/// Written to the buffer
//);
Typedef long (winapi * procntqsi) (uint, pvoid, ulong, Pulong );

Procntqsi ntquerysysteminformation;

Void main (void)
{
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;

Printf ("/ncpu usage (press any key to exit ):");
While (! _ Kbhit ())
{
// Get new system time
Status = ntquerysysteminformation (systemtimeinformation, & policimeinfo, sizeof (policimeinfo), 0 );
If (status! = No_error)
Return;

// Get new CPU's idle time
Status = ntquerysysteminformation (systemperformanceinformation, & sysperfinfo, sizeof (sysperfinfo), null );
If (status! = No_error)
Return;

// If it's a first call-Skip it
If (lioldidletime. quadpart! = 0)
{
// Currentvalue = newvalue-oldvalue
Dbidletime = li2double (sysperfinfo. liidletime)-li2double (lioldidletime );
Dbsystemtime = li2double (systimeinfo. likesystemtime)-li2double (lioldsystemtime );

// Currentcpuidle = idletime/systemtime
Dbidletime = dbidletime/dbsystemtime;

// Currentcpuusage % = 100-(currentcpuidle * 100)/numberofprocessors
Dbidletime = 100.0-dbidletime * 100.0/(double) sysbaseinfo. bkenumberprocessors + 0.5;

Printf ("/B % 3d %", (uint) dbidletime );
}

// Store new CPU's idle and system time
Lioldidletime = sysperfinfo. liidletime;
Lioldsystemtime = policimeinfo. likesystemtime;

// Wait one second
Sleep (1000 );
}
Printf ("/N ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.