Process
Process is a program that is ready for execution in CPU. When a program is loaded into memory, it becomes ready for execution and competes with other process to access CPU. Thus when a program is loaded into memory it becomes process. Computers nowadays can do many things at the same time. They can be writing to a printer, reading from a disc and scanning an image. Operating system is responsible for running many processes, usually, on the same CPU. In fact, only one process can be run at a time so the operating system has to share the CPU between the processes that are available to be run, while giving the illusion that the computer is doing many things at the same time. This approach can be directly contrasted with the first computers. They could only run one program at a time.
Now, the main point of this part is to consider how an operating system deals with processes when we allow many to run. To achieve true parallelism we must have multiprocessor system when n processors can execute n programs simultaneously. True parallelism cannot be achieved with singl CPU. In single processor CPU switched from one process to another process rapidly. In 1 sec CPU can serve 100's of processes giving the illusion of parallelism to a user which is called
pseudo-parallelism.
Threads
Threads are like mini-processes that operate within a single process. Each thread has its own program counter and stack so that it knows where it is. Apart from this they can be considered the same as process, with the exception that they share the same address space. This means that all threads from the same process have access to the same global variable and the same files. Threads are also called light weight process. The table given below shows the various items that a process has compared to the items that each thread has.
Uses of Thread
- Some application need to perform multiple activities at once that share common data and files. Thus reason behind need of multiple threads.
- Ability of parallel entities, within a process to all share common variables and data.
- Since threads do not have their own address space, it is easier to create and destroy threads than processes. In fact thread creation is around 100 times faster than process creation.
- Thread switching has much less overhead than process switching because threads have few information attached with them.
- Performance gain when there is a lot of computing and lot of I/O as tasks can overlap. Pure CPU bound thread application no advantage through
- Threads are very effective on multiple CPU system for concurrent processing because with multiple CPU's true parallelism can be achieved
Difference between Process and Threads
Thread |
Process |
- Threads are lightweight |
- Processes are heavyweight |
- Threads are runs in address space of processes |
- Processes have their own address space |
- Threads shares files and other data |
- Processes do not share files and other data
|
- Thread switching is faster |
- Process switching is slower |
Threads are easy to create and destroy |
Process are difficult to create and destroy |