#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <sched.h> int getcpu(unsigned int *_Nullable cpu, unsigned int *_Nullable node);
cpu に格納された情報が最新だと保証できるのは、システムコールが呼ばれ た時点だけである。sched_setaffinity(2) を使って CPU affinity が固定 されていない限り、カーネルはいつでも CPU を変更してもよい (スケジューラ はキャッシュが有効に働くように CPU の移動を最小限にしようとするので、 通常は CPU が変更されることはないが、起きる可能性はある)。 cpu や node で返された情報が呼び出しが返った時点ですでに 最新の状況と異なる可能性があり、呼び出し元はこの可能性を考慮して おかなければならない。
int getcpu(unsigned int *cpu, unsigned int *node,
struct getcpu_cache *tcache);
The tcache argument is unused since Linux 2.6.24, and (when invoking the system call directly) should be specified as NULL, unless portability to Linux 2.6.23 or earlier is required.
In Linux 2.6.23 and earlier, if the tcache argument was non-NULL, then it specified a pointer to a caller-allocated buffer in thread-local storage that was used to provide a caching mechanism for getcpu(). Use of the cache could speed getcpu() calls, at the cost that there was a very small chance that the returned information would be out of date. The caching mechanism was considered to cause problems when migrating threads between CPUs, and so the argument is now ignored.