Code
//
// main.c
// CPUtils
//
// Created by Ryza 14-9-25.
// Copyright (c) 2014年 Ryza. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mach/mach.h>
int main(int argc, const char * argv[])
{
host_t myhost = mach_host_self();
host_t mypriv;
kern_return_t kr;
processor_port_array_t processorPorts;
mach_msg_type_number_t procCount;
kr = host_get_host_priv_port(myhost, &mypriv);
if (kr)
{
printf("host_get_host_priv_port: %dn",kr);
exit(1);
}
kr = host_processors(mypriv, &processorPorts, &procCount);
if (kr)
{
printf("host_processors");
exit(2);
}
int * statue = (int *)malloc(sizeof(int) * procCount);
int mode = 0;
// -1 -> switch off
// +1 -> switch on
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-off") == 0)
{
mode = -1;
}
else if (strcmp(argv[i], "-on") == 0)
{
mode = +1;
}
else
{
for (int valid = 0; argv[i][valid] != ' '; valid++)
{
if (argv[i][valid] - 48 <0 || argv[i][valid] - 48 > 9)
{
mode = 0;
break;
}
}
if (mode == 0) continue;
int processor = atoi(argv[i]);
statue[processor] = mode;
}
}
for (int proc = 0; proc < procCount; proc++)
{
switch (statue[proc]) {
case -1:
{
processor_exit(processorPorts[proc]);
break;
}
case 1:
{
processor_start(processorPorts[proc]);
break;
}
default:
break;
}
if (kr != KERN_SUCCESS && statue[proc] != 0)
{
printf("Unable to %s %dn",statue[proc] == -1 ? "stop" : "start", proc);
}
}
return 0;
}