Threads – Part of a Process in Computers
Threads are a small part (small part instruction) of a single process (complete instruction). For example, assume that a process Process_1 can is dividing into 10 threads. So Process_1 = 10 Threads. When we finish executing these 10 threads we can say that Process_1 has been processed/completed.
To understand more let us assume that there are 3 brothers and a single grocery list which says to buy: 1 Milk Bottle, 1 Biscuit, and 1 Jar of 300g Nutella. Assume adding an item to the cart and checking it out takes ‘1 minute’. If only one brother is assigned this job it takes him 1+1+1=’3 minutes’. Let us see if 2 of them goes, then, one buys Milk and Biscuits ‘2 minutes‘, the other buys Nutella at the same time ‘1 minute‘, so total time is 2 minutes. Now all 3 then each one of them buys each item at the same time so total time is 1 minute. This sharing of resources (using brothers) and executing tasks (buying items) at the same time is the biggest advantage of Threads.
Threads are able to share their resources but Processes cannot share the resources. This is because Threads are part of same process/group (same family as in above example, brothers). But Processes are entirely different (different family) from each other. The data may be incorrect if it shared between two processes because the processing on it hasn’t been completed.
Threads are a component of a process. Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources. In particular, the threads of a process share its executable code and the values of its variables at a time.
Thread themselves are an independent set of smaller instructions for the processor. This includes the use of Program Counter which loads the next instruction in an orderly fashion. It also uses Stack Pointer which points to a unique memory address or else the Threads may interfere with each other.
Threads are not part of hardware they are an implementation of software making use of available hardware in such a way that it is always in use and never idle.
Control Flow like – Switch, function calls, loops, branching, affects Threads. This is because Threads use Program Counter (PC), and control statements change the way Program Counter loads/points to the next instruction in a predefined way. Usually, PC determines its next instruction to execute the instruction it is currently pointing to.
The sequence of values the PC takes on forms a path of execution weaving through the program code, giving rise to the name “thread”.
Threads VS Processes
Processes are independent and carry considerably more state information, threads, while threads are subsets of a process and have separate address spaces.
Threads are subsets of a process, and share process state as well as memory and address space.
Context switching between threads in the same process is typically faster than context switching between processes.
Resources used when creating Threads
A thread is smaller than a process. Thread creation typically uses fewer resources than
process creation. Creating a process requires allocating a process control block (PCB), a rather large data structure. The PCB includes a memory map, list of open files, and environment variables. Allocating and managing the memory map is typically the most time-consuming activity.
Creating either a user or kernel thread involves allocating a small data structure to hold a register set, stack, and priority.
Threads shares resources like memory of the process to which they belong to.
The advantage of code sharing: It allows an application to have several different threads of activity all within the same address space.
Threads share resources of their parent process, like memory. The new process requires allocation of new address space(memory), which is costly. When sharing memory, the inter-process communication between process is expensive comparing to threads.