[Toybox] [PATCH] taskset: Document and add test for 0 PID

Jesse Rosenstock jmr at google.com
Thu Jul 3 08:12:03 PDT 2025


Since util-linux/util-linux at 6c87a3a (taskset: Accept 0 pid for current
process), util-linux's taskset has accepted a 0 PID for the taskset
process.

https://github.com/util-linux/util-linux/commit/6c87a3ac5ee96194e0e461db24b1bbfe369bdb59

Toybox has always had this behavior, inherited from sched_getaffinity(2)
and sched_setaffinity(2).

https://www.man7.org/linux/man-pages/man2/sched_getaffinity.2.html

Document the zero PID behavior and use it to simplify the tests.

Note that toybox and util-linux show different output.  I'm not sure if
this should be considered a bug or not.

% ./toybox taskset 1 ./toybox taskset -p 0
pid 0's current affinity mask: 1
% ./taskset 1 ./taskset -p 0
pid 2140985's current affinity mask: 1
---
 tests/taskset.test   | 11 ++++-------
 toys/other/taskset.c |  1 +
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/tests/taskset.test b/tests/taskset.test
index acd2869fbc..06ba64f512 100755
--- a/tests/taskset.test
+++ b/tests/taskset.test
@@ -14,16 +14,13 @@
 MASK=$(printf %x $(((1<<CPUS)-1)))
 LAST=$(printf %x $((1<<(CPUS-1))))

-# Yes, taskset -p gets the arguments backwards. Because gnu.
+# pid 0 means the taskset process itself.
 testing 'set mask to all' \
-  '(taskset -p $MASK $BASHPID >/dev/null; taskset -p $BASHPID | sed
"s/.*: //")'\
-  "$MASK\n" '' ''
+  '(taskset $MASK taskset -p 0 | sed "s/.*: //")' "$MASK\n" '' ''
 testing 'set mask to first' \
-  '(taskset -p 1 $BASHPID >/dev/null; taskset -p $BASHPID | sed "s/.*: //")'\
-  '1\n' '' ''
+  '(taskset 1 taskset -p 0 | sed "s/.*: //")' '1\n' '' ''
 testing 'set mask to last' \
-  '(taskset -p $LAST $BASHPID >/dev/null; taskset -p $BASHPID | sed
"s/.*: //")'\
-  "$LAST\n" '' ''
+  '(taskset $LAST taskset -p 0 | sed "s/.*: //")' "$LAST\n" '' ''

 # alas procps-ng always says "-" for -o cpu so fetch the field from /proc
 testing 'run on first' \
diff --git a/toys/other/taskset.c b/toys/other/taskset.c
index cc33d94e3b..8ec726e9f6 100644
--- a/toys/other/taskset.c
+++ b/toys/other/taskset.c
@@ -26,6 +26,7 @@

     Mask is a hex string where each bit represents a processor the process
     is allowed to run on. PID without a mask displays existing affinity.
+    A PID of zero means the taskset process.

     -p Set/get the affinity of given PID instead of a new command
     -a Set/get the affinity of all threads of the PID


More information about the Toybox mailing list