Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Created July 18, 2015 18:44
Show Gist options
  • Save devendranaga/71bdc85f61f1194c179f to your computer and use it in GitHub Desktop.
Save devendranaga/71bdc85f61f1194c179f to your computer and use it in GitHub Desktop.
take a cpu online / offline in linux (tested on Fedora 22)
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
void usage()
{
printf("./cpuenable <online / offline> <cpu number>\n");
exit(1);
}
int main(int argc, char **argv) {
int ret;
char buff[300];
int online;
int cpu;
if (argc != 3) {
usage();
}
cpu = atoi(argv[2]);
if (!strcmp(argv[1], "online")) {
online = 1;
} else if (!strcmp(argv[1], "offline")) {
online = 0;
} else
usage();
sprintf(buff, "echo %d > /sys/device/system/cpu%d/online", online, cpu);
int ret;
ret = system(buff);
if (ret != 0) {
printf("failed to take cpu%d %s\n", cpu, argv[1]);
return ret;
} else
printf("cpu%d taken %s\n", cpu, argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment