#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#define __USE_GNU
#include <pthread.h>
#include <sys/syscall.h>
#include <sched.h>
#include <errno.h>
void * singal_cpu(void *para)
{
int index, ret;
cpu_set_t cpuset;
index = (int)para;
CPU_ZERO(&cpuset);
CPU_SET(index, &cpuset);
ret = sched_setaffinity(syscall(SYS_gettid), sizeof(cpu_set_t), &cpuset);
if (ret == -1)
{
fprintf(stderr, "set cpu[%d] affinity error:%s
", strerror(errno) );
return NULL;
}
while (1){}
//fprintf(stderr, "thread: %d pid: %lu
", index, syscall(SYS_gettid) );
return NULL;
}
#define THREAD_CNT 1
int main(int argc, char **argv)
{
int i;
pthread_t pids[THREAD_CNT];
for (i = 0; i < THREAD_CNT; i++)
{
pthread_create(&pids[0], NULL, singal_cpu, (void *)i);
}
sleep(atoi(argv[1]));
return 0;
}