]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
taskset: Accept 0 pid for current process master
authorJesse Rosenstock <jmr@google.com>
Fri, 27 Jun 2025 07:47:35 +0000 (09:47 +0200)
committerJesse Rosenstock <jmr@google.com>
Wed, 2 Jul 2025 20:04:40 +0000 (22:04 +0200)
This is useful to print the current mask without using `$$`: `taskset -p 0`.

It is also helpful to test taskset: `taskset -c 1-4 taskset -p 0`.
This is not easy with `$$`.

sched_setaffinity(2)/sched_getaffinity(2) accept 0 for the calling
thread, so this seems consistent.

As an implementation detail, we replace 0 with getpid(), so the existing
pid != 0 <==> "will exec" logic continues to work unchanged.

A reasonable alternative would be to interpret just `taskset` (currently
an error) as printing the current mask.  This seems less orthogonal,
and a better use may be found for plain `taskset` in the future.

Signed-off-by: Jesse Rosenstock <jmr@google.com>
schedutils/taskset.1.adoc
schedutils/taskset.c

index 938434737247f6dbf270f57df0308311f096c849..45622613bd5a8115eecabee5ddd18eb606048d70 100644 (file)
@@ -77,6 +77,7 @@ Interpret _mask_ as numerical list of processors instead of a bitmask. Numbers a
 
 *-p*, *--pid*::
 Operate on an existing PID and do not launch a new task.
+If PID is zero, then operate on the *taskset* process.
 
 include::man-common/help-version.adoc[]
 
@@ -134,6 +135,13 @@ taskset: failed to set pid 14's affinity: Invalid argument +
 $ echo $? +
 1 +
 
+== EXAMPLES
+
+Print the current CPU affinity as a list.
+
+$ taskset -pc 0 +
+pid 1355988's current affinity list: 0-47 +
+
 == AUTHORS
 
 Written by Robert M. Love.
index b52cd4338b8005979bdb5510c560b6144c227c24..7e2ecc33652912b158074dd5095a954bcbddb03f 100644 (file)
@@ -186,7 +186,18 @@ int main(int argc, char **argv)
                        all_tasks = 1;
                        break;
                case 'p':
-                       pid = strtopid_or_err(argv[argc - 1], _("invalid PID argument"));
+                       /*
+                        * strtopid_or_err() is not suitable here; 0 can be
+                        * passed.
+                        */
+                       pid = strtos32_or_err(argv[argc - 1],
+                                             _("invalid PID argument"));
+                       if (pid == 0)
+                               pid = getpid();
+                       /*
+                        * After this point, pid == 0 means "no pid" and that
+                        * we will exec a command.
+                        */
                        break;
                case 'c':
                        ts.use_list = 1;