views:

127

answers:

2

Hi guys, I'm here to ask you the difference between a process and a thread in linux. I know that a thread for linux is just a "task", which shares with the father process things that they need to have in common (the address space and other important informations). I also know that the two are creating calling the same function ('clone()'), but there's still something that I'm missing: what really happens when a thread exit? What function is called inside the linux kernel? I know that when a process exits calls the do_exit function, but here or somewhere else there should be a way to understand if it is just a thread exiting or a whole process. Can you explain me this thing or redirect to some textbook?? I tried 'Understanding the linux kernel' but I was not satisfied with it. I'm asking this thing because a need to add things to the task_struct struct, but I need to discriminate how to manage those informations for a process and its children.

Thank you.

A: 

The main difference between processes and threads is that proceses run in their own virtual memory space, apart from every other process. That means two processes cannot access each other's data. The only way for two processes to interact is through the operating system somehow (shared memory sections, semaphores, sockets, etc.).

Threads on the other hand all exist within their creating process. That means threads have access to all the same data (variables, pointers, handles, etc.) that any other thread in the same process has. That is the main difference.

There are some implications of this. For instance, when the process terminates for some reason, all its threads go with it. It is also a lot easier to get multi-processing errors like torn data in threads, just because nothing is forcing you to use the OS syncronization functions that you really ought to be using.

T.E.D.
A: 

The exit() syscall exits a single thread, and the exit_group() syscall exits the entire POSIX process ("thread group").

caf
Ok, but I was looking for informations on how the linux kernel handles the termination (exit) of a thread from the kernel side. In particular, I'm interested in looking to the code of the function inside the kernel to understand in detail what it does so that I can add my code... you're saying that the do_exit function of the kernel works just on a thread??
Raffo
@Raffo: Yes, exactly.
caf