In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to analyze the thread stack based on linuxthreads-2.0.1, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
A thread is essentially an execution flow in a process. We know that a process has a code segment, and a thread is actually a piece of code in a process code segment. One implementation of a thread is implemented as a process. Create a new process by calling clone, and then execute a code snippet in the parent process code snippet. Files, memory and other information are shared. Because the memory is shared, threads cannot share the stack, otherwise the address of the stack will be mapped to the same physical address, which will affect each other, so each thread will have its own independent stack. The range of the stack is set when the clone function is called. Let's take a look at the thread's stack through the linuxthreads code. There is a _ _ pthread_initialize function in linuxthreads, which is executed before the main function is executed. The stack range of the main thread is set in this function.
/ / CURRENT_STACK_FRAME is the sp register. Align by STACK_SIZE siz
_ _ pthread_initial_thread_bos = (char *) (long) CURRENT_STACK_FRAME-2 * STACK_SIZE) & ~ (STACK_SIZE-1))
_ _ pthread_initial_thread_bos saves the top of the stack of the main thread.
Then when we first call pthread_create to create the thread, we call the pthread_initialize_manager function to initialize the manager thread. Manager threads are threads that manage other threads.
/ / allocate a piece of memory on the heap for the manager thread's stack, top of the stack
_ _ pthread_manager_thread_bos = malloc (THREAD_MANAGER_STACK_SIZE)
/ / High address is the bottom of the stack
_ _ pthread_manager_thread_tos = _ _ pthread_manager_thread_bos + THREAD_MANAGER_STACK_SIZE
Then call the clone function to set the stack of the manager thread.
_ _ clone (_ _ pthread_manager,__pthread_manager_thread_tos, CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, (void *) (long) manager_pipe [0])
Finally, set the stack of the created thread in the function pthread_handle_create, and the pthread_handle_create function is the function that is called when the pthread_create function is called.
/ / THREAD_STACK_START_ADDRESS is _ _ pthread_initial_thread_bos, which is the top of the stack of the main thread
# ifndef THREAD_STACK_START_ADDRESS
# define THREAD_STACK_START_ADDRESS _ _ pthread_initial_thread_bos
# endif
# define THREAD_SEG (seg) ((pthread_t) (THREAD_STACK_START_ADDRESS-(seg) * STACK_SIZE)-1)
# define SEG_THREAD (thr) (size_t) THREAD_STACK_START_ADDRESS-(size_t) (thr+1)) / STACK_SIZE)
Pthread_t new_thread = THREAD_SEG (sseg)
Mmap ((caddr_t) ((char *) (new_thread+1)-INITIAL_STACK_SIZE), INITIAL_STACK_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_GROWSDOWN,-1,0)
From the above code, we can see that the stack of the newly built thread is under the top of the main thread's stack (that is, the address is less than the top of the main thread's stack). When creating a thread, first calculate the stack address of the new thread, and then call mmap to draw this address. Otherwise, you will segmentfault when you visit. Finally, call clone to create the process (thread) and set the top and bottom positions of the stack.
_ _ clone (pthread_start_thread, new_thread, (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | PTHREAD_SIG_RESTART), new_thread)
The memory layout is as follows.
From the stack distribution above, we can also know which thread the currently executing code belongs to.
Static inline pthread_t thread_self (void)
{
# ifdef THREAD_SELF
THREAD_SELF
# else
Char * sp = CURRENT_STACK_FRAME
/ / greater than the initialization stack is the main thread
If (sp > = _ _ pthread_initial_thread_bos)
Return & _ _ pthread_initial_thread
/ / this is the space requested by the manager thread itself
Else if (sp > = _ _ pthread_manager_thread_bos)
& & sp < _ _ pthread_manager_thread_tos)
Return & _ _ pthread_manager_thread
Else
/ / sp must fall within the stack range of some thread. STACK_SIZE-1 makes the low n-bit all-1, or sp adds 1 to the high address, STACK_SIZE alignment, minus a pthread_t to get tcb.
Return (pthread_t) ((unsigned long int) sp | (STACK_SIZE-1)) + 1)-1
# endif
}
This is how linuxthreads gets the current thread.
The above is based on how linuxthreads-2.0.1 analyzes the stack of threads. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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: 0
*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.