????????ó?????и?????????????????????????????????????CPU?????????????????????????????????????
???????????CPU
????Linux??????????????????????????CPU??
????#include <sched.h>
????int sched_setaffinity(pid_t pid?? size_t cpusetsize?? const cpu_set_t *mask);
????int sched_getaffinity(pid_t pid?? size_t cpusetsize?? cpu_set_t *mask);
????????
????pid???????id??????pid?0????????????
????cpusetsize??mask???С
????mask?????н????CPU????????????o???????mask
????#define CPU_SET(cpu?? cpusetp) //????cpu
????#define CPU_CLR(cpu?? cpusetp) //???cpu
????#define CPU_ISSET(cpu?? cpusetp) //?ж?cpu
????#define CPU_ZERO(cpusetp) //??????0
???????????
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <sched.h>
void WasteTime()
{
int abc = 10000000;
while(abc--)
{
int tmp = 10000*10000;
}
sleep(1);
}
int main(int argc?? char **argv)
{
cpu_set_t mask;
while(1)
{
CPU_ZERO(&mask);
CPU_SET(0?? &mask);
if (sched_setaffinity(0?? sizeof(mask)?? &mask) < 0) {
perror("sched_setaffinity");
}
WasteTime();
CPU_ZERO(&mask);
CPU_SET(1?? &mask);
if (sched_setaffinity(0?? sizeof(mask)?? &mask) < 0) {
perror("sched_setaffinity");
}
WasteTime();
CPU_ZERO(&mask);
CPU_SET(2?? &mask);
if (sched_setaffinity(0?? sizeof(mask)?? &mask) < 0) {
perror("sched_setaffinity");
}
WasteTime();
CPU_ZERO(&mask);
CPU_SET(3?? &mask);
if (sched_setaffinity(0?? sizeof(mask)?? &mask) < 0) {
perror("sched_setaffinity");
}
WasteTime();
}
}
????????
??????????????г???????????top -p ????id??????f??????j???????????????????????cpu0123?????л???