While designing any system usually we might confuse between thread and process usage . We think should we use thread or process for our requirement. Which will be the best choice.?
So Here are some guidelines to help you……
- All threads inside a program always run from the same executable binary. Where a child process , may run a from different executable binary by calling an exec function.
- If one process crashes or has a buffer overrun, it does not affect any other process at all, whereas if a thread crashes, it takes down all of the other threads in the process because threads share the same virtual memory space and other resources.
- Threads should be used for programs that need fine-grained parallelism. For example, if a problem can be broken into multiple, nearly identical tasks, threads may be a good choice. Processes should be used for programs that need coarser parallelism.
- Sharing data among threads is quite easy because threads share the same memory. Sharing data among processes needs to the use of IPC mechanisms which is quite complex.
- Processes are more heavy-weight than threads, and have a higher start up and shutdown cost. Interprocess communication (IPC) is also harder and slower than inter-thread communication.
Note : There is also some discussion about saying t in Windows processes are heavy and expensive compared to threads, and in Linux the difference is much smaller.