Talk C chestnuts together (134th back: C language instance-memory details during thread creation)
Hello, everyone. In the previous session, we talked about the example of "memory details during Process Creation". This example is as follows: memory details during thread creation. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
We have introduced the example of creating a thread in the previous chapter. The function used to create a thread is pthread_create. Today, let's talk about the memory details when using this function to create a thread. We will focus on four partitions in the memory layout. This is the same as the memory details described in the previous step.
1. you can use the pthread_create function in the main thread to create a new thread. We call it a subthread. The main thread can create multiple subthreads. 2. the sub-thread has its own stack zone. The content of the stack zone belongs to itself and cannot be used by other sub-threads or even the main thread. 3. the subthread does not have its own code zone, data zone and heap zone, but shares these areas with the main thread. 4. execute a separate thread function when a sub-thread is running. This function is set when a thread is created;
Finally, let's talk about the pthread_create function. In Linux, this function creates a thread through clone. The subthread can own its own stack zone and share the code zone with the main thread, the data zone and heap zone benefit from the clone function. The following is a prototype of the clone function. For details, refer:
Int clone (int (* fn) (void *), void * child_stack, int flags, void * arg ,... /* pid_t * ptid, struct user_desc * tls, pid_t * ctid */);
From the above function prototype, we can see that the fn parameter is used to set the thread to execute the function, and the child_stack parameter is used to create the stack zone of the sub-thread. Other areas in the memory are related to the flags and arg parameters, so we will not detail them.
Let's talk about the example of "memory details during thread creation. I want to know what examples will be provided later, and I will try again.