7

I ran the program pstree -p 31872 which printed the following output:

 ruby(31872)─┬─{ruby}(31906)         
             └─{ruby}(32372)

The man page for pstree says:

Child threads of a process are found under the parent process and are shown with the process name in curly braces, e.g.

       icecast2---13*[{icecast2}]

(The above is displayed differently because of the missing -p option, which disables compaction.)

Running pstree 31872 without -p gives:

ruby───2*[{ruby}] 

When I try to observe those PIDS using ps, no results are found. However, the pids, exist in /proc.

My question is, why would threads have different pids? I would expect them to be the same (31872) as the process. The same behavior is observed when running htop.

8

The mistake was to presume those numbers were PIDS, when in fact they are TIDS (thread IDs). See Linux function gettid(2). Reading up on clone(2) gives a lot of extra (and interesting) details.

1

Threads are often different processes. At least in Linux, a "thread" is often a clone of the process which happens to share some memory with the parent process.

However, in some language, threads do stay part of the main process because they are "Green" threads, which are simulated in the language VM. Java and python are famous for this.

  • 1
    Threads belong to processes and share the process's memory space. They are definitely not a separate process from the process that created them. When you say "a thread is often a clone of a process", you might be thinking of the fork system call, which creates a child process, not a thread. – stantona Nov 6 '14 at 1:08
  • No. In Linux, if you want to make something which can be scheduled separately from the main thread, you can either declare a new thread, or fork. In either case, the first thing which happens is a clone, which duplicates the process without making a new memory area for it. If it's forking, the next thing which happens is an allocation of a separated process memory space. See the linux function clone(2) – Hack Saw Nov 6 '14 at 5:57
  • I think you kind of elaborated on what I said, no?? – stantona Nov 6 '14 at 14:40
  • More succinctly, the only difference between real threads and processes, in linux, is that a threads shares memory with another process, and a process doesn't. As far as the proc directory goes, and the machinery behind it, and more importantly the scheduler, it's just another process. – Hack Saw Nov 6 '14 at 20:25

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.