Saturday, February 8, 2014

Multiple threads v/s Multiple processes

A legitimate question occurs that is thread is just like a process, why don't we create multiple processes? Why do we create multiple threads?

There are a few reasons.

Consider it just like an evolutionary process. First there were just processes. It lead to the below shortcomings.

1. If we create multiple processes which should share same resources, coding for it was a cumbersome process. 
2. Memory wise too, creating a new processes requires more resources, than creating a separate thread, as essentials (memory, files etc) are shared by all threads.

It lead to the creation of threads. The code remains a single unit. And multiple sub-processes are spawned. 

You can consider it like, if 2 small packages can be bundled up and send via courier. Why to send two different couriers.

Two different couriers require:
1. Creating two separate records and tracking numbers.
2. Two separate outer packing material is required.
3. Packing material comes in standard size, hence small package are also packed into big envelopes, which is wastage.

Single courier
1. If delivery address is same, it's better to package them together.
2. Single record and tracking.
3. Single packing material and less wastage.

Three Elephants v/s One Elephant and Two rats

Understand it by another example. When we can do with one elephant and two small rats. It is better than three big elephants.



No comments:

Post a Comment