Threads
- A thread is a single sequential flow of execution of the tasks of a process.
- A thread is a lightweight process and the smallest unit of CPU utilization. Thus a thread is like a little miniprocess.
- Each thread has a thread id, a program counter, a register set and a stack.
- A thread undergoes different states such as new, ready, running, waiting and terminated similar to that of a process.
- However, a thread is not a program as it cannot run on its own. It runs within a program.
Process
- In most systems, a process is a unit of work done.
- A process is a set of sequential steps that are required to do a particular task.
- A process is an instance of a program in execution.
- For example, in the windows environment, if we edit two text files, simultaneously, in notepad, then it means we are implementing two different instances of the same notepad program. For an operating system these two instances are separate processes of the same application.
- A process needs certain resources such as CPU time, memory files, and I/O devices to accomplish its task. These resources are allocated to the process either when it is created or while it is executing.
The various similarities between threads and processes are:
- Like processes, each thread has its own program counter and stack to keep track of where it is.
- Threads share CPU just as processes do.
- Like processes, threads (within a process) also run sequentially.
- Threads can also create child threads.
- Threads also have almost the same states as processes – ready, running, blocked, terminated.
- If one thread is blocked, the other thread in the same process can run, in exactly the same way that when one process blocks, another process in the same machine can run.
The various differences between threads & processes are:
- Each process has its own distinct address space in the main memory. On the other hand, all the threads in a process share the same address space.
- Threads require less system resources than processes.
- Unlike processes, threads are not independent of one another.
- Processes may originate from different users, and may be hostile to one another whereas an individual task will multiple threads will always be owned by a single user.
- New threads take less time for creation than new processes.
- It takes less time to terminate a thread than to terminate a process.
- It takes less time to switch between two threads within the same process than to switch between two processes.
- Processes are more robust in comparison to threads.
- Threads enhance efficiency in communication between different executing programs. As threads within same process share memory and files, they can communicate with each other without invoking the operating system’s kernel.
Difference between threads and processes
S. No. | Process | Thread |
1. | A process (or task) is a unit of resource ownership. | A thread is a unit of dispatching. |
2. | A process is heavyweight. | A thread is lightweight. |
3. | Each process has its own distinct address space in memory. | All threads is a process share the same address space. |
4. | Process require more system resources than threads. | Threads require less system resources than process. |
5. | Process are independent of one another. | Unlike threads processes, are not independent of one another. |
6. | A process does not depend on thread’s state. | A thread is dependent on the process state (running, ready etc.) |
7. | A process has protected access other processes (for IPC), files and I/O processor, resources (devices and channels). | Access to memory and resources of its process (task) shared with all other threads in task. |
8. | More complications arise when many processes run concurrently. | Multiple threads may run concurrently with ease. |
9. | Processes used Inter-process Communication (IPC) method to communicate with each other. | Threads of same process can communicate directly with each other. |
10. | New process takes more time for creation. It also takes more time to terminate a process. | New threads take less time for creation than new processes. Moreover threads also take less time to terminate. |
11. | It takes more time to switch between two processes. | It takes less time to switch between two threads of a same process. |
12. | Processes are more robust in comparison to threads. | Threads are less robust. |
13. | Alteration of process is not an easy task. | A number of threads in a program can be altered during the course of execution. |
14. | A process is primarily concerned with job or application. | Thread is primarily concerned with scheduling and dispatching. |