0

This question already has an answer here:

As I have read, a "thread" in Linux is just a process.

Say that a process created two "threads", now when the process is terminated, the two "threads"/processes that are associated with the process will also be terminated.

Where the information about the "threads"/processes that are associated with a process (that created them) is stored?

marked as duplicate by Stephen Kitt linux May 6 '18 at 20:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

The informations are stored in memory in the task structure called task_struct associated to each process/thread. All those informations are as usual made available in the pseudo-filesystem /proc as described in man 5 proc.

When a process is killed and will disappear, the kernel just parses in memory the list of threads associated to the process (conveniently visible in /proc/[pid]/task/) to kill them.

The process/thread representation model in linux doesn't separate much between process and thread, they are all tasks. For example they share the same task namespace, as seen in /proc/ which organizes pid (process id) or tid (thread id) the same. But there are differences, eg getpid() will return the thread's pid, while gettid() will return the thread's tid. They can return different values when a thread call them.

Relevant to the question, are those pseudo-files mapping to task_struct contents:

  • /proc/[pid]/task/

    This is a directory that contains one subdirectory for each thread in the process. The name of each subdirectory is the numerical thread ID ([tid]) of the thread (see gettid(2)). Within each of these subdirectories, there is a set of files with the same names and contents as under the /proc/[pid] directories. For attributes that are shared by all threads, the contents for each of the files under the task/[tid] subdirectories will be the same as in the corresponding file in the parent /proc/[pid] directory (e.g., in a multithreaded process, all of the task/[tid]/cwd files will have the same value as the /proc/[pid]/cwd file in the parent directory, since all of the threads in a process share a working directory). For attributes that are distinct for each thread, the corresponding files under task/[tid] may have different values (e.g., various fields in each of the task/[tid]/status files may be different for each thread), or they might not exist in /proc/[pid] at all. In a multithreaded process, the contents of the /proc/[pid]/task directory are not available if the main thread has already terminated (typically by calling pthread_exit(3)).

  • /proc/[pid]/status

    Tgid: Thread group ID (i.e., Process ID).

    This pseudo-file has a Tgid entry, the same value for all threads of a process (aka the tid's pid).

More informations can be seen in man 7 pthreads with a description of the current NPTL implementation.

-1

Every thread, except for the first (init) has a parent thread. You can see the relationship best using the f flag to ps, this way: "ps faux" (no quotes).

-1
/bin/ps -eo pid,nlwp,tid,args -L

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