0

I find it quite confusing to differentiate threads and processes when I run top and then press H or run ps -Hef.

I know that they share the same attributes, which makes them similar but was wondering whether there is anything in the output that can be used to identify what is a process and what is a thread?

They all look like regular processes to me, so it's hard to tell then apart.

user
  • 13
  • 4

2 Answers2

0

I know the question is a bit old, but just as reference for anyone looking at this.

By default, ps doesn't show threads, but you can use the -T flag to see them. This in combination with the -fly option to print the output using long format will give you the information you are looking for:

ps -flye -T 

To distinguish between processes and threads you can use the PID and SPID fields. All the threads share the same PID but have a unique SPID. When PID==SPID, that's the parent process.

For debugging purposes, I find quite useful looking at the threads of a specific process:

$ ps -fly -T -p 193471
S UID         PID   SPID   PPID  C PRI  NI   RSS    SZ WCHAN  STIME TTY          TIME CMD
S root     193471 193471 193466  0  80   0 902460 871117 poll_s 10:46 ?      00:00:17 /usr/bin/python36 xyzzy
S root     193471 193665 193466  4  80   0 902460 871117 futex_ 10:46 ?      00:01:22 /usr/bin/python36 xyzzy
S root     193471 193667 193466  4  80   0 902460 871117 futex_ 10:46 ?      00:01:32 /usr/bin/python36 xyzzy
S root     193471 193669 193466  4  80   0 902460 871117 futex_ 10:46 ?      00:01:26 /usr/bin/python36 xyzzy
S root     193471 193672 193466  4  80   0 902460 871117 futex_ 10:46 ?      00:01:23 /usr/bin/python36 xyzzy
S root     193471 193676 193466  4  80   0 902460 871117 poll_s 10:46 ?      00:01:33 /usr/bin/python36 xyzzy
Victor Henriquez
  • 243
  • 1
  • 3
  • 8
0

I think what you should focus on is the "context of execution". Each processes (or tasks ) have their own address space. Think of threads as a subset of process using the same address space. ps and top won't show this but poke around /proc/<pid>status and /proc/<pid>map. Also maybe try strace and gdb to see what the program is doing. Here's a gdb output for a simple c program that usespthread_create`.

# gdb mythread
;...]

Reading symbols from /root/mythread...(no debugging symbols found)...done.
(gdb) break main
Breakpoint 1 at 0x40080f
(gdb) break hello_world
Breakpoint 2 at 0x400761
(gdb) r
Starting program: /root/mythread 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, 0x000000000040080f in main ()
(gdb) c
Continuing.
The main process id is 12887
[New Thread 0x7ffff77f1700 (LWP 12891)]
[Switching to Thread 0x7ffff77f1700 (LWP 12891)]

Breakpoint 2, 0x0000000000400761 in hello_world ()
(gdb) c
Continuing.
[New Thread 0x7ffff6ff0700 (LWP 12892)]
[Switching to Thread 0x7ffff6ff0700 (LWP 12892)]

Breakpoint 2, 0x0000000000400761 in hello_world ()

User info proc mappings

(gdb) info proc mappings
process 12896
Mapped address spaces:

          Start Addr           End Addr       Size     Offset objfile
            0x400000           0x401000     0x1000        0x0 /root/mythread
            0x600000           0x601000     0x1000        0x0 /root/mythread
            0x601000           0x602000     0x1000     0x1000 /root/mythread
      0x7ffff77f2000     0x7ffff79b5000   0x1c3000        0x0 /usr/lib64/libc-2.17.so
      0x7ffff79b5000     0x7ffff7bb4000   0x1ff000   0x1c3000 /usr/lib64/libc-2.17.so
      0x7ffff7bb4000     0x7ffff7bb8000     0x4000   0x1c2000 /usr/lib64/libc-2.17.so
      0x7ffff7bb8000     0x7ffff7bba000     0x2000   0x1c6000 /usr/lib64/libc-2.17.so
      0x7ffff7bba000     0x7ffff7bbf000     0x5000        0x0 
      0x7ffff7bbf000     0x7ffff7bd6000    0x17000        0x0 /usr/lib64/libpthread-2.17.so
      0x7ffff7bd6000     0x7ffff7dd5000   0x1ff000    0x17000 /usr/lib64/libpthread-2.17.so
      0x7ffff7dd5000     0x7ffff7dd6000     0x1000    0x16000 /usr/lib64/libpthread-2.17.so
      0x7ffff7dd6000     0x7ffff7dd7000     0x1000    0x17000 /usr/lib64/libpthread-2.17.so
      0x7ffff7dd7000     0x7ffff7ddb000     0x4000        0x0 
      0x7ffff7ddb000     0x7ffff7dfd000    0x22000        0x0 /usr/lib64/ld-2.17.so
      0x7ffff7fed000     0x7ffff7ff0000     0x3000        0x0 
      0x7ffff7ff9000     0x7ffff7ffa000     0x1000        0x0 
      0x7ffff7ffa000     0x7ffff7ffc000     0x2000        0x0 [vdso]
      0x7ffff7ffc000     0x7ffff7ffd000     0x1000    0x21000 /usr/lib64/ld-2.17.so
      0x7ffff7ffd000     0x7ffff7ffe000     0x1000    0x22000 /usr/lib64/ld-2.17.so
      0x7ffff7ffe000     0x7ffff7fff000     0x1000        0x0 
      0x7ffffffde000     0x7ffffffff000    0x21000        0x0 [stack]
  0xffffffffff600000 0xffffffffff601000     0x1000        0x0 [vsyscall]
Tux_DEV_NULL
  • 1,093
  • 7
  • 11
  • thanks for answering but was specifically looking for a way to differentiate between processes and threads in the output of top or ps – user Sep 26 '18 at 06:58