From 310ac278405a063c48265b8706b08de300080fc2 Mon Sep 17 00:00:00 2001 From: Michael Kerrisk Date: Thu, 10 Mar 2016 16:43:52 +0100 Subject: [PATCH] sched_setaffinity.2: Warn that CPU_ALLOC() may allocate a slightly CPU set than requested Reported-by: Florian Weimer Reviewed-by: Florian Weimer Signed-off-by: Michael Kerrisk --- man2/sched_setaffinity.2 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/man2/sched_setaffinity.2 b/man2/sched_setaffinity.2 index fd3298645..39f80b0a4 100644 --- a/man2/sched_setaffinity.2 +++ b/man2/sched_setaffinity.2 @@ -276,6 +276,21 @@ of the required mask using .BR sched_getaffinity () calls with increasing mask sizes (until the call does not fail with the error .BR EINVAL ). + +Be aware that +.BR CPU_ALLOC (3) +may allocate a slightly larger CPU set than requested +(because CPU sets are implemented as bit masks allocated in units of +.IR sizeof(long) ). +Consequently, +.BR sched_getaffinity () +can set bits beyond the requested allocation size, because the kernel +sees a few additional bits. +Therefore, the caller should iterate over the bits in the returned set, +counting those which are set, and stop upon reaching the value returned by +.BR CPU_COUNT (3) +(rather than iterating over the number of bits +requested to be allocated). .SH EXAMPLE The program below creates a child process. The parent and child then each assign themselves to a specified CPU