how many stacks does the kernel maintain/keep track of...say if there are 12 kernel threads and around 50 user processes??

Dani AI

Generated

Brief practical answer (for the example): the kernel keeps a kernel-mode stack for every schedulable thread (so 12 kernel threads + one kernel stack for each user thread — if each of the 50 user processes is single-threaded that’s ~62 kernel stacks), and on top of that there are per-CPU interrupt/exception stacks and a few special stacks the kernel uses. ’s short reply pointed in the right direction; the fuller picture below explains what the kernel actually allocates and what lives in user space. (kernel.org)

Where the “user” stacks live: each process/thread’s user-mode stack is just part of that task’s address space (tracked by the process’s mm_struct and its vm_area_struct list). Those user stacks are ordinary VM mappings (not separate kernel-owned kernel stacks). The kernel only records the address-space metadata (vm areas, start_stack, etc.). (kernel.org)

How kernel stacks are allocated and how big they are: kernel-mode stacks are allocated per task (historically via the thread_union/thread_info layout, or via vmalloc-backed stacks when CONFIG_VMAP_STACK is used). The actual THREAD_SIZE is architecture- and config-dependent (it’s defined in the arch headers by THREAD_SIZE_ORDER; many modern x86_64 configs use multiple pages, but older docs show different defaults), so exact bytes depend on kernel version and config. The kernel also provides per-CPU IRQ/exception stacks and hardware IST stacks on x86_64 for special exceptions. (stackoverflow.com)

Quick checks and caveats: on a running system total schedulable contexts (and thus kernel stacks) can be counted by enumerating threads (for example, ps -eL or ls /proc/<pid>/task | wc -l to count threads for a process). Remember to add per-CPU IRQ/exception stacks (one per CPU) and any vmalloced-kernel-stack behavior controlled by kernel config. Kernel stacks are intentionally small and stack-overflow/guard handling exists (double-fault/IST and vmalloc guard pages), so stack-size/config matters for kernel code and drivers. (kernel.org)

Recommended Answers

All 3 Replies

Each process, user thread, and kernel thread should have its own stack. The kernel will switch between them as necessary and context changes require.

Each process, user thread, and kernel thread should have its own stack. The kernel will switch between them as necessary and context changes require.

yup i knew bout this...but i had my doubts bout the processes... whether the kernel maintains...individual stacks from the kernel's perspective...

yup i knew bout this...but i had my doubts bout the processes... whether the kernel maintains...individual stacks from the kernel's perspective...

From what I read, this is the case, that the kernel maintains these structures.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.