From an operating system perspective, multiple 'execution contexts' or 'tasks' need to be created to maximise CPU utilisation. So, if you wait for some resource or I/O, the scheduler schedules another task so that the CPU does not waste any of the cycles. Creating such a 'task' or 'execution context' (or 'process,' in Linux terminology) is generally an expensive operation, because it involves the replication of the complete address space as well as replicating all the resources (such as open file descriptors, etc) by the parent 'process'.

Name:  Linux Basic concepts.jpg
Views: 44
Size:  6.1 KB

One more setback also occurs at the system level when one or more 'processes' want to talk. They can talk only through some kind of FIFO/ pipe/socket or 'mmap', and all of them either involve the copying of data from the user space to kernel space and vice-versa, or the use of page table manipulation. The operating system designers have thought of one more way to solve this problem with a lower penalty,
and tat's w e the concept of threads comes in. The crear of threads also means creating separate 'execution contexts', but with less penalty.

Threads share common 'data sections', 'heap' and system resources such as the environment, file descriptors, etc, and hence the creation and deletion of a thread is very 'lightweight' as compared to the creation of processes. Since the 'process address space' is shared between the threads in a given process, the communication between threads also does not have as many penalties. depicts this point more clearly.