Related to linux kernel before ~4.9 version

(before the following patch: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=15f4eae70d365bba26854c90b6002aaabb18c8aa)

Every process have to be represented in some way in the kernel in order to manage resources, scheduling, credentials and more…

There are 2 main structures:

1. task_struct

2. thread_info

thread_info have a pointer to task_struct.

With this structures the kernel able to identify the processes by their PIDs, check permissions by cred structure located inside task_struct, check threads state, and more information about the process that written in those structures.

Process VS Thread

In the kernel there is no differences between Process to Thread representations. From programmer’s point of view there are logical differences between processes and threads.

So every process and every thread represented by individual task_struct and thread_info structures.

Each task_struct (of process or thread) have unique PID value (because thread represented as process and process represented as thread, there is no differences from kernel’s point of view).

thread_info location

thread_info structure located at the end of the kernel’s stack in order to optimise the access to thread‘s’ properties.

The thread_info structure smaller than task_struct, and have pointer to task_struct, so the kernel can efficiently and easily access thread’s properties with few calculations on esp register by masking out the 13 least significant bits of the stack pointer to obtain the thread_info structure (kernel stack size & alignment 8KB).

How does threads share the same resources ?

Every thread represented by individual task_struct/thread_info, So how does few threads know that they are permitted to share resource with each other ?

In the task_struct structure there are PID and TGID fields. When new process ‘A’ created, assigned PID and TGID (representing “group id” of threads) values are the same. When thread ‘B’ created under process ‘A’, it gets unique PID (‘B’ PID) but the value of TGID is the value of process’s ‘A’ PID/TGID .

This way few thread knows that they are under the same process (TGID “group”) and permitted to share resources.

Thanks for reading !!

When I have few questions about the same topic I search answers in different places. At the end I like to summarise the results In one paper, so i have just post here the paper for others.

You can Follow me in:

https://github.com/TomHatskevich

https://twitter.com/TomHatskevich