In an operating system, when some applications execute their program then it has to utilize the multi-tasking capability of the operating system to complete the program. Therefore to utilize this multi-tasking facility two techniques are process and threads. The difference between the process and thread is it’s like creating a clone of the program and then dividing the task between these two clones and the thread is like creating a duplicate part of the program in which the multi-tasking characteristics can happen.
The process is created with the help of system calls and the call that creates a child process of the program is a fork(). This system call will ask the kernel of the operating system to create a child process of the program. Whereas in the case of a thread, the thread is created by the application program itself with the help of API and this is the responsibility of the user to create the threads but the creation of the process is the responsibility of the operating system. There are also two types of threads one is the user-level thread and the other one is the kernel-level thread. Here, in this case, we are referring to the user-level thread. Therefore it is the responsibility of the application itself to create different threads.
The operating system treats different processes differently whereas the threads are treated similarly. The programs will have different processes and each of these different processes ids whereas for when a program creates multiple threads then these threads have the same process ids.
When a program is divided into different processes then each process is assigned with their separate data, code, and registers and they perform their operations independently whenever they are called via the fork() system calls. But in the case of threads, two or more threads will have the same data and code but the register will be different only. Therefore the threads are divided from each other with registers and therefore they have the same process ids. Also for this reason the overhead of the process is much more than the overhead of the threads. When the fork() system call is called then it creates an exponential number of copies of the processes of the program but in threads as they share the resources the number of overheads is less and also it is much more effective.
The context switching in the process is much slower than the context switching in the threads. Whenever the context switches in the processes, it refers to the switching from one process to another process in this case the first process register and codes and data is stored in the operating system temporarily, and then when it switches to the other process to attend to its data, codes, and execution. This context switching takes a great amount of time for the operating system as it has to save all the configuration of the process in one register and then execute the other. The thread on the other hand has the same code and data but the difference between the various threads is the different registers that it has. And the only switching occurs in the thread is switching of the register which proves to be a lot faster than the process.
The other important difference between process and threads is that when the parent process is blocked then the child process will keep on running and therefore you can also say that the process is independent of each other as they have their memory and code. But when the parent thread is blocked then all the child threads will come to a halt immediately and this will halt the operation. As the threads share the memory and resources and address therefore if one stops the other thread will also stop. Therefore the threads are dependent on each other whereas the processes are independent of each other.