Talk C chestnuts together (134th back: C language instance-memory details during thread creation)

Source: Internet
Author: User

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.

Related Article

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.