28.7.2. getcpu system call and the sched_getaffinity glibc wrapper

Examples:

Returns the CPU that the process/thread is currently running on:

So when running a multicore program, we may see that each thread can be running on a different core.

The cores in which the process runs can be fixed with sched_setaffinity as shown at: userland/linux/sched_getaffinity.c.

So when I run it with main thread + 4 threads on a 4 core CPUs:

./userland/linux/sched_getcpu.out 4

I see random outputs like:

7
2
1
5

and:

5
0
2
1

Due to the way that gem5 syscall emulation multithreading however, the output is more deterministic in that case, see that section for further details.