Process vs Thread, how to choose?
We write programs, in the end is the use of multithreading or multiple processes? There is a difference, and the efficiency that can be achieved by using different mechanisms is not the same. How do you choose a mechanism that suits our own procedures? Here are some common choice of view, but also just to provide you with reference, the specific design of the time or to deal with their own.
¨ all threads in a program are executed in the same running space. The subprocess of a program is run in another execution space, and this is done by calling the EXEC function.
¨ the failure of one thread in the same process can affect other threads, because all threads share the same virtual memory space and other resources. For example, a thread that writes to a pointer that is not initialized can affect other threads. A process that is out of the question does not affect other processes, because they operate in different process spaces respectively.
¨ creating a new process requires a memory copy operation, which adds additional burden to the system, and threads do not need this copy process. However, since the implementation of the current operating system is only when the memory needs to change the part of the copy changes, so the impact here is relatively small.
¨ threads are often used in situations where there is a need for better synchronization operations. For example, a thread is a good choice if a problem can be decomposed into tasks that are almost equivalent to synchronous processing. Processes are suitable for applications where there is no need for strict synchronization.
It is convenient to share data between ¨ threads because different threads are inherently sharing the same storage space. (However, the competition is very carefully handled here.) and sharing data between processes requires the use of some IPC mechanisms, such as pipelines, shared memory, sockets, and so on.