In this tutorial, we’ll briefly define two ambiguous terms, process and thread. After that, we’ll look at the differences between them.
A process is the execution of a program. It includes the program itself, data, resources such as files, and execution info such as process relation information kept by the OS. The OS allows users to create, schedule, and terminate the processes via system calls.
A thread is a semi-process. It has its own stack and executes a given piece of code. Unlike a real process, the thread normally shares its memory with other threads. Conversely, processes usually have a different memory area for each one of them.
There are three different types of threads that we can implement: kernel-level threads, user-level threads, and hybrid threads.
3. Differences Between Processes and Threads
Although there is no reason to think that threads and processes are separate entities, some specific properties of these two entities make them differ from one another. We can see a brief relation between processes and threads:
3.1. Some Specific Characteristics of Processes
Processes are unique in that they don’t share data and information; they’re isolated execution entities. In short, a process has its own stack, memory, and data.
In order to create more than one process, we need to use separate system calls. Additionally, more system calls are required for process management.
Finally, to cooperate with more than one process, we need to use Inter-Process Communication (IPC) mechanisms. This situation leads to an increase in the number of system calls as well.
3.2. Some Specific Features of Threads
Unlike processes, threads share data and information. They do, however, have their own stack.
We can create more than one thread by using just one system call. To further simplify things, thread management requires few or even no system calls because we don’t need extra mechanisms such as IPC to maintain communication between threads.
3.3. Key Differences Between a Process and a Thread
4. Advantages and Disadvantages of Using Threads Rather Than Processes
A thread group is a set of threads executing inside the same process. As we mentioned earlier, they share the same memory; therefore, they can access the same global variables, the same set of file descriptors, and the same heap memory.
All of these threads execute in parallel. The use time slices or, if the machine has more than one processor, then truly in parallel.
One of the advantages of using a thread group instead of a process group is that many operations can be performed in parallel. This allows events to be handled as they arrive.
For instance, if we have one thread handling database queries and one thread handling a user interface, we can execute the query demanded by the user while still responding to user input.
Another advantage of using a thread group over a process group is related to context switching. Context switching between threads is much faster than context switching between processes.
Context switching refers to how the system switches from running one process or thread to another running process or thread. Furthermore, communication between two threads is usually faster and simpler to implement than communications between two processes.
Let’s look at the relation and differences between processes and threads:
Since threads in a group use the same memory space, whenever one of them corrupts the contents of its memory, the contents of other threads’ can be corrupted as well. When it comes to processes, the OS usually protects them from one another. Even if one of them corrupts its own memory space, other processes are not affected.
Another benefit of using processes over threads is that they can run on different machines. On the other hand, threads normally have to run on the same machine.