5

In manpage of ps

tid         TID          the unique number representing a dispatchable
                         entity (alias lwp, spid).  This value may also
                         appear as: a process ID (pid); a process group
                         ID (pgrp); a session ID for the session leader
                         (sid); a thread group ID for the thread group
                         leader (tgid); and a tty process group ID for
                         the process group leader (tpgid).

   tgid        TGID      a number representing the thread group to which
                         a task belongs (alias pid).  It is the process
                         ID of the thread group leader.

In Ubuntu, tid and tgid seem always the same as pid, for both user processes, and kernel threads (I run ps -o pid,tid,tgid,cmd) Is it true in Linux, and why?

Is it true in other Unix such as System V or BSD?

Thanks.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
Tim
  • 98,580
  • 191
  • 570
  • 977

1 Answers1

5

You need to task ps to show thread information; otherwise it only lists processes:

ps -eL -o pid,tid,comm | awk '$1 != $2'

will show all the threads, apart from each process’ main thread, i.e. entries in the process table where pid and tid are different. The significant option is -L: without that, ps will only list entries where pid and tid are identical.

On FreeBSD, the equivalent option is -H. I haven’t checked other BSDs, or System V.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • 1
    Hmm…this actually doesn't give an answer. Are they the same or not. – 炸鱼薯条德里克 Dec 29 '18 at 20:52
  • 1
    @炸鱼薯条德里克 `awk '$1 != $2'` should give a strong hint, but I’ve clarified that part. – Stephen Kitt Dec 29 '18 at 20:54
  • Thanks. "On FreeBSD, the equivalent option is -H". BSD style option doesn't have dash, does it? – Tim Dec 29 '18 at 21:45
  • 1
    @Tim: Linux _ps_ doesn't use a dash, but the actual FreeBSD _ps_ accepts both kinds (because it doesn't have to distinguish: _all_ options in FreeBSD _ps_ are "BSD style" options) and the [manual page](https://www.freebsd.org/cgi/man.cgi?ps(1)) actually documents them with a dash. – u1686_grawity Dec 29 '18 at 22:23