5

in Linux threads are called light weight processes. Whether process or thread, they are implemented by task_struct data structure.

1> So, in that sense how kernel distinguishes between thread and process?

2> when context switching happens, how do threads get less overhead in context switching? because prior to this thread, another thread from another process may be running. So kernel should load all resources even if resources are shared between threads of a processes.

thor
  • 21,418
  • 31
  • 87
  • 173
user1434287
  • 381
  • 3
  • 16

3 Answers3

5

how kernel distinguishes between thread and process.

From http://www.kernel.org/doc/ols/2002/ols2002-pages-330-337.pdf and from Linux - Threads and Process

This was addressed during the 2.4 development cycle with the addition of a concept called a ’thread group’. There is a linked list of all tasks that are part of the thread group, and there is an ID that represents the group, called the tgid. This ID is actually the pid of the first task in the group (pid is the task ID assigned with a Linux task), similar to the way sessions and process groups work. This feature is enabled via a flag to clone().

and

In the kernel, each thread has it's own ID, called a PID (although it would possibly make more sense to call this a TID, or thread ID) and they also have a TGID (thread group ID) which is the PID of the thread that started the whole process.

Simplistically, when a new process is created, it appears as a thread where both the PID and TGID are the same (new) number.

When a thread starts another thread, that started thread gets its own PID (so the scheduler can schedule it independently) but it inherits the TGID from the original thread.

So a main thread is a thread with the same PID and TGID and this PID is a process PID. A thread (but not a main thread) has different PID but the same TID.

Community
  • 1
  • 1
5

Inside kernel, each process and threads have a unique id (even threads of same process) which is stored in pid variable and threads of same process also share a common id which is stored in tgid variable, and is returned to user when getpid() is invoked therefore allowing kernel to distinguish them as different entities which are schedulable in themselves.

When a thread is preempted by another thread of same process, since various segments such as .text, .bss, .data, file descriptors etc. are shared and hence allows a fast context switch compared to when different processes are context switched, or when threads of different processes are context switched.

Rachit Tayal
  • 1,190
  • 14
  • 20
0

It seems you mixed some concepts together, implemented by the same data structure does not mean they run in the same way.

you can read what-is-the-difference-between-a-process-and-a-thread to clarify your comprehension about process and thread firstly.

Community
  • 1
  • 1
gzh
  • 3,507
  • 2
  • 19
  • 23