Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are process stack, thread stack and kernel stack respectively?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the process stack, thread stack and kernel stack respectively". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the process stack, thread stack and kernel stack are respectively.

Process descriptor task_struct

When the thread is created, the CLONE_VM tag is added so that the thread's memory descriptor points directly to the parent process's memory descriptor.

Memory descriptor mm_struct

Process stack: stack

Thread stack: the space allocated using the mmap system call, but what is the system space allocated by mmap? What is the mmap area, or shared memory-mapped area, in the figure above? Does it grow upward or downward?

Mmap is actually the same as heap, in fact, it can be said that they are dynamic memory allocation, but strictly speaking, the mmap area does not belong to the heap area, on the contrary, it will compete with the heap area for virtual address space.

A very important concept to mention here is the delayed allocation of memory, which establishes a physical mapping of an address only when it is actually accessed, which is the basic idea of Linux memory management. When the Linux kernel applies for memory, it only allocates a linear area (that is, virtual memory) to it, but not the actual physical memory; only when the user uses this memory, the kernel allocates specific physical pages to the user, which takes up valuable physical memory. The kernel releases the physical page by releasing the advance area, finding its corresponding physical page, and releasing all of it.

Struct mm_struct {struct vm_area_struct * mmap; / * memory area linked list * / struct rb_root mm_rb; / * VMA formed a red-black tree * /. Struct list_head mmlist; / * linked lists formed by all mm_struct * /. Unsigned long total_vm; / * total pages * / unsigned long locked_vm; / * locked page data * / unsigned long pinned_vm; / * Refcount permanently increased * / unsigned long shared_vm; / * number of shared pages Shared pages (files) * / unsigned long exec_vm / * number of executable pages VM_EXEC & ~ VM_WRITE * / unsigned long stack_vm; / * number of pages in the stack area VM_GROWSUP/DOWN * / unsigned long def_flags; unsigned long start_code, end_code, start_data, end_data; / * code segment, data segment start address and end address * / unsigned long start_brk, brk, start_stack / * start address of stack area, start address and end address of stack area * / unsigned long arg_start, arg_end, env_start, env_end; / * start and end addresses of command line arguments and environment variables * /... / * Architecture-specific MM context * / mm_context_t context / * Architecture special data * / / * Must use atomic bitops to access the bits * / unsigned long flags; / * status flag bit * /... / * Coredumping and NUMA and HugePage-related structures * /}

Why you need to distinguish between these stacks is actually a design problem. Here is a summary of some of the ideas I have seen for your discussion:

Why do you need a separate process kernel stack?

When all processes are running, they may fall into the kernel state through system calls and continue to execute. Suppose that when the first process A falls into the kernel state, you need to wait for the data of the network card to be read, and actively call schedule () to let CPU;. At this time, the scheduler wakes up another process B, and coincidentally, process B also needs the system call to enter the kernel state. Then the problem arises. If there is only one kernel stack, the stack pressing operation that occurs when process B enters the kernel state will inevitably destroy the existing kernel stack data of process A. once the kernel stack data of process An is destroyed, it is very likely that the kernel state of process A cannot be returned to the corresponding user state correctly.

Why do you need a separate thread stack?

At this time, if the stack pointer esp of A1 is the initial value 0x7ffc80000000, once a function call occurs in thread A1, it is bound to destroy the data that the parent process A has entered the stack.

If the stack pointer of thread A1 is the same as the last updated value of the parent process, esp is 0x7ffc8000FF00, then after thread A1 makes some function calls, the stack pointer esp increases to 0x7ffc8000FFFF, and then thread A1 sleeps; the scheduler changes to parent process An again to execute, then should the stack pointer of the parent process be 0x7ffc8000FF00 or 0x7ffc8000FFFF? No matter which value the stack pointer is set to, there will be a problem, won't it?

There is no distinction between a thread and a process in the Linux scheduler. When the scheduler needs to wake up the "process", it must restore the context of the process, that is, the process stack; but the thread and the parent process completely share an address space, and if the stack is the same, it will encounter the following problems. If the initial value of the stack pointer of the process is 0x7ffc80000000; parent process An executes first, and after calling some functions, the stack pointer esp is 0x7ffc8000FF00, and the parent process sleeps actively; then the scheduler wakes up child thread A1:

Do processes and threads share a kernel stack?

No, dup_task_struct is called when threads and processes are created to create task-related structures, and the kernel stack is also alloc_thread_info_node in this function. So although threads and processes share an address space mm_struct, they do not share a kernel stack.

Why do I need to interrupt the stack separately?

In fact, this problem is wrong, the ARM architecture does not have a separate interrupt stack.

The difference between a stack and a stack in process space:

Space size: stack system specified size is limited to 8m (M level), stack is continuous space; heap has no limit, is discontinuous storage space, linked by linked list.

Allocation: the heap is dynamically allocated and recycled in the programmer's code, and memory leakage will occur if there is no recycling; there are two ways of stack allocation: static allocation and dynamic allocation. Static allocation is done by the compiler, such as the allocation of local variables. Dynamic allocation is allocated by the alloca function, but the dynamic allocation of the stack is different from the heap. Its dynamic allocation is released by the compiler without our manual implementation.

Allocation efficiency: the stack allocates special registers to store the address of the stack, and special instructions are executed on and off the stack; the heap is executed by calling library functions

Stack: in a function call, the first one to enter the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), followed by the parameters of the function, which in most C compilers enter the stack from right to left, followed by local variables in the function. Note that static variables are not on the stack.

When this function call ends, the local variable goes off the stack first, then the parameters, and finally the pointer at the top of the stack points to the original address, that is, the next instruction in the main function, from which the program continues to run.

Heap: the size of the heap is usually stored in a byte at the head of the heap. The details in the heap are arranged by the programmer.

In short, the stack is more efficient than the heap, but not as flexible as the heap, giving priority to the use of the stack, large memory using the heap.

Char a [] = "hello"; the capacity of the / / character array an is 6 characters and its content is hello. The content of a can be changed, for example, a [0] ='X'

Char * p = "world"; / / the pointer p points to the constant string "world" (located in the static storage area with the content world). The content of the constant string cannot be modified.

/ * kernel space * stack: grow down, size system setting ~ 8m, contiguous space, compiler automatically allocates * Memory Mapping Seg: stack sharing, thread stack * stack: grow up, size hardware fixed, discontiguous space Programmer malloc*BSS:Block Started by Symbol, uninitialized global and static variables (static data area) * data segments: store initialized global variables, static variables (global and local), const constant data (constant data area) * code segments: store machine instructions executed by CPU, the code area is shareable and read-only. The size of this part of the area has been determined before the program runs * * / # include int astato; / / data segment: global initialization variable char * p1; / / BSS: global uninitialized variable void main () {int bhandle / stack char s [] = "abc"; / / stack char * p2; / / stack char * p3 = "123456" / / 123456\ 0 is in the constant area (code snippet) and p3 is on the stack. Static int segment: global (static) initialization area * p1 = (char*) malloc (10); / / the allocated 10-byte area is on the heap * p2 = (char*) malloc (20); / / the allocated 20-byte area is on the heap. Strcpy (p1, "123456"); / / 123456\ 0 is placed in the constant area, and the compiler may optimize it in the same place as "123456\ 0" in the direction of p3. } / / const constant or right constant such as "123456" is placed in the data segment or the code segment?

Malloc and free are the standard library functions of Candlespace C language, and new/delete is the operator of C++. They can all be used to request dynamic memory and free memory. But new/delete calls the object's constructor and destructor.

At this point, I believe that everyone has a deeper understanding of "process stack, thread stack, kernel stack", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report