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.

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]
  • 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 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.