C language: Process vs thread, how to choose?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.