In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. The implementation of threads and processes differs between operating systems, but in most cases a thread is 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 dynamically allocated variables and non-thread-local global variables at any given time.
Difference between Process and Thread
Process means any program is in execution. Process control block controls the operation of any process. Process control block contains information about processes for example Process priority, process id, process state, CPU, register, etc. A process can creates other processes which are known as Child Processes. Process takes more time to terminate and it is isolated means it does not share memory with any other process.
The process can have the following states like new, ready, running, waiting, terminated, suspended.
Thread is the segment of a process means a process can have multiple threads and these multiple threads are contained within a process. A thread have 3 states: running, ready, and blocked.
Thread takes less time to terminate as compared to process and like process threads do not isolate.
Difference between Process and Thread:
|1.||Process means any program is in execution.||Thread means segment of a process.|
|2.||Process takes more time to terminate.||Thread takes less time to terminate.|
|3.||It takes more time for creation.||It takes less time for creation.|
|4.||It also takes more time for context switching.||It takes less time for context switching.|
|5.||Process is less efficient in term of communication.||Thread is more efficient in term of communication.|
|6.||Process consume more resources.||Thread consume less resources.|
|7.||Process is isolated.||Threads share memory.|
|8.||Process is called heavy weight process.||Thread is called light weight process.|
|9.||Process switching uses interface in operating system.||Thread switching does not require to call a operating system and cause an interrupt to the kernel.|
|10.||If one server process is blocked no other server process can execute until the first process unblocked.||Second thread in the same task could run, while one server thread is blocked.|
|11.||Process has its own Process Control Block, Stack and Address Space.||Thread has Parents’ PCB, its own Thread Control Block and Stack and common Address space.|
Threads in Operating Systems
What is a Thread?
A thread is a path of execution within a process. A process can contain multiple threads.
A thread is also known as lightweight process. The idea is to achieve parallelism by dividing a process into multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc. More advantages of multithreading are discussed below
Process vs Thread?
The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter (PC), register set, and stack space.
Threads vs. processes pros and cons
Threads differ from traditional multitasking operating-system processes in several ways:
- processes are typically independent, while threads exist as subsets of a process
- processes carry considerably more state information than threads, whereas multiple threads within a process share process state as well as memory and other resources
- processes have separate address spaces, whereas threads share their address space
- processes interact only through system-provided inter-process communication m`1echanisms
- context switching between threads in the same process typically occurs faster than context switching between processes
Systems such as Windows NT and OS/2 are said to have cheap threads and expensive processes; in other operating systems there is not so great a difference except in the cost of an address-space switch, which on some architectures (notably x86) results in a translation lookaside buffer (TLB) flush.
Advantages and disadvantages of threads vs processes include:
- Lower resource consumption of threads: using threads, an application can operate using fewer resources than it would need when using multiple processes.
- Simplified sharing and communication of threads: unlike processes, which require a message passing or shared memory mechanism to perform inter-process communication (IPC), threads can communicate through data, code and files they already share.
- Thread crashes a process: due to threads sharing the same address space, an illegal operation performed by a thread can crash the entire process; therefore, one misbehaving thread can disrupt the processing of all the other threads in the application.
"Difference between Process and Thread" by MKS075, Geeks for Geeks is licensed under CC BY-SA 4.0
"Thread in Operating System" by chrismaher37, Geeks for Geeks is licensed under CC BY-SA 4.0
"Thread (computing)" by Multiple Contributors, Wikipedia is licensed under CC BY-SA 3.0