On linux, each process is given 4 GB of virtual address space, given the 32-bit architecture

Hi I'm new to Linux Kernel Development. So I want to get some clarity for the next statement.

*> In memory, each process is provided with 4 GB of virtual address space

given 32-bit architecture. The lower virtual addresses are 3GB available for the user portion of the process space and the upper 1 GB available for the user portion of the kernel space. *

  • Does this mean that every process in Linux is allocated that much memory space of 1GB + 3GB?
  • If so, there are hundreds of processes in Linux, so 100 * 4GB of space, where does the system get so much memory space from?
  • What does it have to do with the kernel stack and the user stack?
  • Does every process in Linux have a kernel and a custom stack?
+3


source to share


2 answers


Introduction

Linux, like most modern operating systems, uses virtual pages provided by most modern architectures, including the x86 family.

In a virtual memory system, physical memory (the actual RAM resource) is abstract from the process running on the system. The address space is just numbers where memory can be.

Paging

Memory can be mapped (placed by address) in pages , which depends on the architecture of the memory block. So if you want to put memory at some address so that the process can use it, you will need to select a page-aligned number (multiple of the page size) and place at least one page there.

Protection

Virtual memory also allows you to protect memory, which sets what permissions this process will have. When the executable memory of a process is mapped (the instructions it executes to do things), it is read / execute only. This means that the processor can execute this memory and you can read it, but you cannot write it.

When a process is loaded from disk (on Linux with a system call exec

), it is put into memory with memory regions already mapped. These regions are executable code from the program, data sections, and stack. The process may require more memory to be displayed later using system calls mmap

or brk

.



When a process tries to access memory, it is not mapped, it throws a notorious error SEGFAULT

, and the kernel will kill your program. In other cases, the hardware will be to blame, but the program has a memory card, this is because the kernel canceled it to keep it until it is needed. This is where the process stops, the kernel reassigns the memory, and your process starts up again like nothing happened.

Address space

Thus, the size of the address space is only the upper memory limit that you could have if the program mapped every address it could into actual RAM.

The single-disk address space for the kernel portion is the process metadata that the kernel keeps track of. For example, it will keep a list of open files and mapped memory in process headers. It will preserve the stream headers as well. Again, none of this is displayed, just what he needs.

Note that each process has its own universe of addresses, it never sees what another process has mapped to these addresses. Thus, a process can act as if it were the only process on the machine, mapping memory to whatever address it chooses.

Also note that the 4gb number is due to the fact that the hardware that does the addressing only supports 32-bit numbers, the largest number that a 32-bit number can hold is 2 ^ 32 = 4 294 967 296. That's 4 GB ... Thus, you can map 4gb addresses to memory.

This is just a crappy introduction, please do some virtual memory action.

+3


source


Does this mean that every process in Linux is allocated that much memory space of 1GB + 3GB?

Not. All processes have the same kernel space. In addition, these are the maximum theoretical limits. These can be further limited by system settings, process settings, and page file size. Even though the system has allowed a process to grow to its maximum side, processes usually start small and must grow to reach maximum.

If so, there are hundreds of processes in Linux, so 100 * 4GB of space, where does the system get so much memory space from?

See above. If allowed, such memory space will be on disk in the page file (section).



What does it have to do with the kernel stack and the user stack?

These are separate stacks. The kernel stack is in kernel space, and the user stack is in user space. The processor uses the kernel stack when executing in kernel mode and the user stack in user mode. The switch is automatic as part of the hardware switching between modes.

Does every process in Linux have a kernel and a custom stack?

Yes. There is one for each stream. Kernels tend to be small.

0


source







All Articles