sched_getcpu(3)

๐Ÿ’… NAME ๐Ÿ’…

sched_getcpu โ€” bestie, this literally tells you which CPU your calling thread is running on right now. Like a "where am I" but for silicon. โœจ

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). Miss thing lives in libc, so you already know her.


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <sched.h>

int sched_getcpu(void);

๐Ÿฆ‹ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

_sched_getcpu():

  • Since glibc 2.14: _GNU_SOURCE
  • Before glibc 2.14:
_BSD_SOURCE || _SVID_SOURCE
    /* _GNU_SOURCE also suffices */

okay so yes, you have to define the right macro or glibc will simply not let you in the group chat. that's just how it is ๐Ÿซถ

๐ŸŒธ DESCRIPTION ๐ŸŒธ

sched_getcpu() returns the number of the CPU on which the calling thread is currently executing.

that's it. that's the whole function. it's giving "one job and she ate" ๐Ÿ‘‘

โญ RETURN VALUE โญ

  • On success: a nonnegative CPU number. slay, you found out where you live. ๐ŸŒˆ
  • On error: -1 is returned and errno is set to indicate the error. ๐Ÿ˜ญ

๐Ÿ’€ ERRORS ๐Ÿ’€

  • ENOSYS โ€” this kernel does not implement getcpu(2). bestie the kernel said no, and honestly? rude.

๐Ÿ”ฅ ATTRIBUTES ๐Ÿ”ฅ

For an explanation of the terms used in this section, see attributes(7).

  • Interface: sched_getcpu()
  • Attribute: Thread safety
  • Value: MT-Safe

MT-Safe means multiple threads can call her at the same time with zero drama. iconic behavior, no notes. ๐ŸŽ€


๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

GNU. we're doing GNU things. periodt.

๐Ÿงš HISTORY ๐Ÿงš

glibc 2.6. so she's been here since 2007, aging like fine wine, lowkey a legend.

๐Ÿ“ NOTES ๐Ÿ“

okay so the call

cpu = sched_getcpu();

is equivalent to the following getcpu(2) call:

int c, s;
s = getcpu(&c, NULL);
cpu = (s == -1) ? s : c;

literally the same vibe, just a glow-up wrapper. iykyk ๐Ÿ’œ

๐ŸŒŸ SEE ALSO ๐ŸŒŸ


that's the whole page bestie, go forth and know which CPU loves you back ๐Ÿ’–

Linux man-pages 6.20, dated 2025-05-17 โ€” mwah, that's the source and the date, sign-off done, no cap ๐Ÿซถโœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms