[PATCH v2] taskset: Accept 0 pid for current process

Jesse Rosenstock <[email protected]>
Newsgroups org.kernel.vger.util-linux
Message-ID <CAMZQ0rLM9UYMupEX4WLmi-J9mh0jGhruzDw3OwpU8yEf0+2E_Q@mail.gmail.com>
Fixed comment style and expanded man page example.

https://github.com/util-linux/util-linux/compare/2c585534e8350aaaa2a1958a619d67febb613dc7..7591728c7f2a4c04c7faa0cc1141ee6df9ecc45b

https://github.com/util-linux/util-linux/pull/3637

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 <[email protected]>
---
 schedutils/taskset.1.adoc |  8 ++++++++
 schedutils/taskset.c      | 14 +++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/schedutils/taskset.1.adoc b/schedutils/taskset.1.adoc
index 9773303f73..c919f82ec6 100644
--- a/schedutils/taskset.1.adoc
+++ b/schedutils/taskset.1.adoc
@@ -77,6 +77,7 @@

 *-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 @@
 $ 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.
diff --git a/schedutils/taskset.c b/schedutils/taskset.c
index b52cd4338b..dfcf291edb 100644
--- a/schedutils/taskset.c
+++ b/schedutils/taskset.c
@@ -186,7 +186,19 @@
                        all_tasks = 1;
                        break;
                case 'p':
-                       pid = strtopid_or_err(argv[argc - 1],
_("invalid PID argument"));
+                       /*
+                        * Like strtopid_or_err() but accept 0 for this process,
+                        * like sched_getaffinity()/sched_setaffinity() do.
+                        */
+                       pid = (pid_t) str2num_or_err(
+                               argv[argc - 1], 10, _("invalid PID argument"),
+                               0, SINT_MAX(pid_t));
+                       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;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.