Different operating systems often have different meanings for terms such as process, thread, task, program, and so on.
In the Neutrino OS, we typically use only the terms process and thread. An application typically means a collection of processes; the term program is usually equivalent to process.
A thread is a single flow of execution or control. At the lowest level, this equates to the program counter or instruction pointer register advancing through some machine instructions. Each thread has its own current value for this register.
A process is a collection of one or more threads that share many things. Threads within a process share at least the following:
- variables that aren't on the stack
- signal handlers (although you typically have one thread that handles
signals, and you block them in all the other threads)
It isn't safe to use floating-point operations in signal handlers.
- signal ignore mask
Threads don't share such things as stack, values for the various registers, SMP thread-affinity mask, and a few other things.
Two threads residing in two different processes don't share very much. About the only thing they do share is the CPU. You can have them share memory between them, but this takes a little setup (see shm_open() in the C Library Reference for an example).
When you run a process, you're automatically running a thread. This thread is called the main thread, since the first programmer-provided function that runs in a C program is main() . The main thread can then create additional threads if need be.
Only a few things are special about the main thread. One is that if it returns normally, the code it returns to calls exit() . Calling exit() terminates the process, meaning that all threads in the process are terminated. So when you return normally from the main thread, the process is terminated. When other threads in the process return normally, the code they return to calls pthread_exit() , which terminates just that thread.
Another special thing about the main thread is that if it terminates in such a manner that the process is still around (e.g. it calls pthread_exit() and there are other threads in the process), then the memory for the main thread's stack is not freed up. This is because the command-line arguments are on that stack and other threads may need them. If any other thread terminates, then that thread's stack is freed.