#include <stdlib.h>
#include <stdio.h>
#include <mach/mach.h>

int main (int argc, const char * argv[])
{
	int cpuCount;
	kern_return_t error;
	host_name_port_t thisHost;
	host_priv_t privHost;
	processor_set_basic_info_t psetInfo;
	processor_set_name_t psetName;
	natural_t psetCount=PROCESSOR_SET_BASIC_INFO_COUNT;
	
	/*let's start by counting the CPUs*/
	thisHost=mach_host_self();
	error=host_get_host_priv_port(thisHost, &privHost);
	if(error!=KERN_SUCCESS)
	{
		mach_error("in host_get_host_priv_port()",error);
		exit(EXIT_FAILURE);
	}
	
	error=processor_set_default(thisHost,&psetName);
	if(error!=KERN_SUCCESS)
	{
		mach_error("in processor_set_default()",error);
		exit(EXIT_FAILURE);
	}
	
	psetInfo=malloc(sizeof(processor_set_basic_info_t));
	error=processor_set_info(psetName,PROCESSOR_SET_BASIC_INFO,&privHost,(processor_set_info_t)psetInfo,&psetCount);
	if(error!=KERN_SUCCESS)
	{
		mach_error("in processor_set_info()",error);
		exit(EXIT_FAILURE);
	}
	
	cpuCount=psetInfo->processor_count;
	printf("I'm running on a %d-way machine.\n",cpuCount);
	
	exit(EXIT_SUCCESS);
}
