2011年7月29日 星期五

How to get the CPU core number

Use sched_getcpu to determine the CPU on which the calling thread is running. See man getcpu (the system call) and man sched_getcpu (a library wrapper).

example:

#include <stdio.h>
#include <utmpx.h>
int main()
{
int c, s;
int cpu;
cpu = sched_getcpu();
printf("id=%d\n", cpu);
return 0;
}

Ps. linux version > 2.6.19

references:
How can I get the CPU core number from within a user-space app (Linux, C)?
INTEL DOC