In this article, we will see the difference between processes and threads used in operating systems. The duty of OS is to manage all the interfaces of the applications that are being run by the computer. The applications make requests with the help of the operating system through an interface-API. Furthermore, GUIs or user-interfaces can be used to make an interaction with the operating system. All computer platforms require the need to develop communication between the computer’s hardware and software. Operating system makes use of numerous different algorithms to perform a variety of different tasks. The terminologies- thread and process are linked to this.
The word process can simply be defined as the execution of a set of particular set of instructions yet to be executed. Therefore, it is only a specific instance of a program when it is being run by a computer. Plus, a program can be linked to various processes. There is ratio of one to many between them. Any process is the executable program code. It is the duty of the operating system to create, schedule and then truncate all the different sort of processes that can further be used by the CPU. The processes that are created by the parent process are called the child processes.
All of the operations related to the process are handled by using Process Control Block. It has all of the information relevant to a process, so it is basically the thinking unit of the process. It contains the ID of the process, its priority, current state etc.
PCB is a type of data structure that is used in kernel side and its functions can be categorized into three types which are as follows:
- Scheduling – It is the strategy to select what process is to be run by the CPU. It checks different parameters and then chooses accordingly. There are different types of scheduling. Some of them are as follows: First come first serve, Shortest job first etc.
- Dispatching – It makes the necessary changes for the execution of the program. A few parameters are to be altered whenever a certain program is in running state, it controls all that.
- Context save – It backs up all the data of a process when it enters a block or resume state.
The state related to a process is its current condition. They are as follows; ready, blocked, running and terminated. These are used to keep a check on the current condition of any process. If we see the point of view of a programmer, the processes are a way so that concurrent execution can take place. This is important because the main and child processes are supposed to have know-how of the details of each other for the sake of doing an assigned task together.
The i/o operations of a process when overlap with the computational activity of the other process, computation speed may be significantly increased.
Characteristics of a process
- To create a new process, system call for each of them is to be created separately
- Each process must have its own space in memory. It should not share data or any other information. It is therefore an isolated execution entity.
- Inter Process Communication is used by the processes so that they can communicate with each other. So, the number of system calls is significantly increased.
- All the processes are to be managed. This can be done by using system calls. This process management uses greater system calls.
- Every process owns its separate heap and stack memory, data, instructions and the memory map.
On the other hand, a thread is a process but a light weight one i.e. a thread is the smallest part of the running instructions being executed that can be handled by the scheduler independently. That means threads lie inside the process. A process is a bigger chunk where as a thread is simply a part of it. Each and every thread is the part of one process only. It can never exist independently without the process. In simpler words, a thread is the path of process execution. Therefore, a process can have a number of threads in it.
Threads can be divided into to categories:
- User level thread
- Kernel level thread
The main purpose of using threads is to divide a process into different parts which browser, a couple of tabs can be opened at the same moment whereas the process is single. This is achieved using threads. Moreover, Microsoft Word also uses the concept of threads. Different threads are used for different purposes i.e. one of the threads can be used for the format the text, another one can be used to insert pictures etc.
Process and Thread:
Processes and threads differ from each other in various ways. The main difference is with respect to the memory space. Threads of the same process use the same memory space whereas each process has its own area in memory. Moreover, threads are linked to each other. No thread can exist independently. The same is true for processes. Therefore, the threads can share a section of code, data and operating system resources with each other. Each thread also has its own registers, program counter and separate stack.
Advantages of Thread over Process
Threads are preferred over processes due to various reasons. Some of them are as follows:
- They ensure fast computation because threads can run in parallel and if one completes its task it gives the desired output which can be further used.
- Time is taken is switching when CPU starts to run a different process whereas in threads this time is relatively less, so the speed is enhanced.
- The CPU is multiprocessor. It can be effectively used by the threads rather than by processes. A number of threads can be executed in parallel by such a processor which thus lessens the overall execution time.
- Some of the resources are shared by the threads that lie within the same process. These resources include the data, code and different files. Each thread has its own stack space and registers.
5. If different threads are to communicate, it can easily be done using threads due to the sharing of same address space. On the other hand, this becomes difficult in processes. It is difficult to communicate between them as certain strategies are to be followed.
- Due to the above-mentioned advantages, the throughput is greatly enhanced. The number of jobs per unit time is greatly increased due to the parallel processing of threads.
|No system call take part in them.||System calls a necessary part of them.|
|Context switching is faster.||Context switching is slower.|
|Code and data are shared by different threads within the same process.||Each process has its own code and data.|
|Threads are linked to each other. They cant be executed independently.||Processes can be executed independently.|
|Threads share the memory space with each other. All the threads of a single process have the same memory area.||Each process has its own space in the memory area.|
|Operating system treats all the user-level threads as a single task.||Operating systems treatment varies from process to process.|
|If a thread at user level gets blocked, rest of the threads also get blocked as they are treated as the single unit by the operating system.||If a process gets blocked, rest of the processes are not affected.