From 40a81414b08c372d815880644953edaa934b0042 Mon Sep 17 00:00:00 2001 From: Jesse Rosenstock Date: Thu, 3 Jul 2025 17:12:03 +0200 Subject: [PATCH] taskset: Document and add test for 0 PID Since util-linux/util-linux@6c87a3ac5ee9 (taskset: Accept 0 pid for current process), util-linux's taskset has accepted a 0 PID for the taskset process. 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 acd2869f..06ba64f5 100755 --- a/tests/taskset.test +++ b/tests/taskset.test @@ -14,16 +14,13 @@ fi MASK=$(printf %x $(((1</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 cc33d94e..8ec726e9 100644 --- a/toys/other/taskset.c +++ b/toys/other/taskset.c @@ -26,6 +26,7 @@ config TASKSET 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 -- 2.39.5