In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is defunct zombie thread". In daily operation, I believe many people have doubts about what is defunct zombie thread. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "what is defunct zombie thread"! Next, please follow the editor to study!
Preface
At present, we occasionally discuss the problem of RT-Thread thread exit, such as mainthread, how to deal with after return? Whether resources such as RAM are released.
Recently, looking at the thread-related kernel source code, based on the kernel object rt_object management method, sort out the processing flow after thread exit.
Rt_thread_defunct, the linked list structure of zombie threads, why it is called zombies, I looked it up in the dictionary. Function to reclaim the memory (heap) resources of the deleted thread.
Thread initialization and creation
Rt_thread_init: statically initialize a thread, thread structure, thread stack, are global variables. After rt_thread_detach, the kernel object of this thread is removed from the kernel container linked list, [but] the thread structure, thread stack, because it is static global, cannot be released. If you want to initialize and use this thread next time, you can still initialize it using the existing thread structure and thread stack after the detach. Characteristics of static threads: after initialization, the footprint of memory will not change.
Rt_thread_create: create a thread dynamically. Need to enable: RT_USING_HEAP, heap management. Both the created thread [structure] and the [thread stack] are dynamically applied. When you delete this thread, you need to call rt_thread_delete. After deletion, the memory (heap) space occupied by the thread's [structure] and [thread stack] can be released. After deletion, this thread no longer exists. If you want to turn it on again, you need to recreate it. Characteristics of dynamic threads: after deletion, memory resources can be released.
Resource recovery of threads
RT-Thread only reclaims thread memory resources from rt_thread_create.
When a thread is statically initialized or created dynamically, it registers: rt_thread_exit, and when the thread exits, rt_thread_exit is called.
In rt_thread_exit, the first step: remove the thread from the scheduling list. Step 2: the static thread will call: rt_object_detach to remove the thread kernel object from the kernel object container; the dynamic thread will add the thread's structure pointer (operation handle) to the rt_thread_defunct zombie thread linked list instead of immediately releasing the memory occupied by the thread. The operation of the zombie thread is performed in the idle thread. Step 3: perform thread scheduling and switch threads. In the execution of the idle thread, it should be ensured that the switching thread is scheduled immediately after the thread is deleted, and the resource recovery of the thread does not need to be given too high priority.
In idle threads: rt_thread_idle_excute is responsible for checking whether the rt_thread_defunct zombie thread linked list is empty, and if not, performing memory release operations. Remove from the thread list, release the thread stack, and release the kernel structure. Note that only dynamically created threads perform this operation.
/ * from: rt_thread_idle_excute fragment * / * remove defunct thread * / rt_list_remove (& (thread- > tlist)); / * release thread's stack * / RT_KERNEL_FREE (thread- > stack_addr); / * delete thread object * / rt_object_delete ((rt_object_t) thread); main thread exit
The above combs the exit of the thread, the processing flow of the zombie thread, and the exit of the main thread, which can basically be combed.
Static mainthread, not enabled: RT_USING_HEAP, memory resources will not be released after exiting.
Dynamic mainthread enabled: after RT_USING_HEAP, after exiting, memory resources are freed.
Compared with the actual release size of memory resources, it is found that dynamic application will take up a little extra memory resources, such as 12 bytes, which will be combed in depth after memory management.
Resource recovery for dynamic threads:
When RT_USING_HEAP main exists: thread stack size is 2048
Msh > freetotal memory: 89568used memory: 10656maximum allocated memory: 10656
When main does not exist:
Msh > freetotal memory: 89568used memory: 8456maximum allocated memory: 10656
Thread stack of main, resources after return: 2200 Bytes space
2048 Stack space + 12Byte (rt_malloc management occupancy) 128Byte rt_thread structure size, sizeof (struct rt_thread) + 12Byte (rt_malloc management footprint, kernel object). Total: 2048 / 12 / 128 / 12 = 2200. Summary
Familiar with RT-Thread thread initialization, creation, detachment (deinitialization), deletion and other operations.
Familiar with dynamically created threads, delete, add zombie thread linked list, memory resource recovery process.
Understand the exit process of ordinary threads such as main threads.
Follow up with questions to study the following knowledge points:
Where is the workflow of the scheduler executed? How does it work?
Thread timer, each thread will create one, after the thread is deleted, the timer resource recovery process.
Timer of rt_thread_delete / * thread, using detach mode, timer resource recovery flow * / rt_timer_detach (& (thread- > thread_timer)); / * timer resource recovery process * /
When I create a thread, I change the name of the thread (name) to RT_NULL, and it still works, but does it matter?
In the rt_thread structure: the linked list of kernel objects is used to add kernel object containers, members: the workflow of tlist.
Historical problem: rt_thread structure:
/ * * Thread structure*/struct rt_thread {/ * rt object * / char name [RT _ NAME_MAX]; / * < the name of thread * / rt_uint8_t type; / * * < type of object * / rt_uint8_t flags / * * < thread's flags * / # ifdef RT_USING_MODULE void * module_id; / * * < id of application module * / # endif rt_list_t list; / * * < the object list * /
Can it be changed to the following:
/ * Thread structure * / struct rt_thread {struct rt_object parent; / * * < inherit from rt_object * / at this point, the study on "what is a defunct zombie thread" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 248
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.