Talk C together (132nd back: C language instance -- Process and thread from the memory perspective)
Hello, everyone. The example of the C program memory layout we mentioned in the previous article is: process and thread from the memory perspective. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
Look, we have just introduced the memory layout of the C program. Let's take a look at the process and thread from the memory perspective.
Both processes and threads can be loaded into the memory to run. Therefore, their memory layout is identical to that of other C Programs. The memory layout of processes and threads is also divided into code, Data, heap, and stack.
For a process, the parent-child process only shares the content in the Code zone. The parent-child process has its own data zone, heap zone, and stack zone. They can only access the content in their own process space. This is also the reason why multiple processes need to communicate in other ways when running, such as using pipelines to communicate between two processes. For threads, multiple threads share the memory resources of the main process, including the Code zone, data zone, heap zone, and stack zone. However, each thread has its own stack zone and is shared by different threads. After the thread shares the resources of the main process, the communication between threads becomes easier. They can directly use the resources of the main process, and they do not need to use resources using third-party tools. Each process has its own process space, which needs to be allocated to the process during process creation. Therefore, it takes more time to create the process. Each thread is sharing the memory space of the main process. When creating a thread, you only need to allocate the stack zone to them. Other partitions are shared, therefore, the thread creation time is faster. This is also the reason why the thread is more efficient than the process. The process memory space is private, while the thread memory space is shared. Therefore, inter-thread communication is more efficient than inter-process communication. This is the biggest difference between a thread and a process. This is also the main reason why everyone prefers a thread.
Let's talk about the examples of processes and threads from the memory perspective. I want to know what examples will be provided later, and I will try again.