In linux every process is given a 4GB of virtual address space considering a 32-bit architecture

2.8k views Asked by At

Hi I am new to Linux Kernel Development. So want some clearity for the following statement.

*> In memory, every process is given a 4GB of virtual address space

considering a 32-bit architecture. The lower 3GB virtual addresses is accessible to the user space portion of the process and the upper 1GB is accessible to the kernel space portion.*

  1. Does it mean that each process in linux is allocated that much memory space 1GB+3GB?
  2. If yes then there are hundreds of process in linux , so 100*4GB space from where the system gets so much of memory space?
  3. What it has relation with the kernel stack and user stack ?
  4. Does every process in linux has kernel and user stack ?
2

There are 2 answers

3
user3344003 On

Does it mean that each process in linux is allocated that much memory space 1GB+3GB?

No. All processes share the same kernel space. In addition, these are the maximum theoretical limits. They can be further restricted by system settings, process settings, and the size of the page file. Even if a system allowed a process to grow to the maximum side, processes generally start small and have to grow to reach the maximum.

If yes then there are hundreds of process in linux , so 100*4GB space from where the system gets so much of memory space?

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

What it has relation with the kernel stack and user stack ?

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

Does every process in linux has kernel and user stack ?

Yes. There is one for each thread. Kernel stacks tend to be small.

9
cptaffe On

Intro

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

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

Paging

Memory can be mapped (put at an address) in pages which is the architecture dependent size of a memory chunk. So if you want to put memory at some address so the process can use it, then you'd have to pick a page aligned number (a multiple of the page size) and place at least one page there.

Protection

Virtual memory also allows for memory protection which sets what permissions the process will have. When a process's executable memory is mapped (the instructions it executes to do stuff) it is read/execute only. This means that the processor can execute that memory, and you can read it, but you can't write to it.

When a process is loaded from disk (in Linux with the exec system call) it is placed in memory with several regions of memory already mapped. These regions are the executable code from the program, data sections, and stack. The process can request more memory to be mapped later by using the mmap or brk system calls.

When a process tries to access memory it does not have mapped, it triggers the infamous SEGFAULT error, and the kernel will kill your program. Other times, the hardware will fault but the program does have memory mapped, this is because the kernel unmapped it to save it until it is needed. What happens here is the process stops running, the kernel remaps the memory, and your process starts running again like nothing happened.

Address Space

So the size of the address space is just the upper limit of the memory you could have if the program had mapped every address it possibly could to actual RAM memory.

The one gig address space to the kernel part is about the process's meta-data that the kernel keeps track of. For instance, it will keep a listing of open files, and mapped memory in the process headers. It will also keep thread headers there. Again, all of it is not mapped, only what it needs.

Note that each process has its own universe of addresses, it never sees what another process has mapped at those addresses. This way, the process can act as if it is the only process on the machine, mapping memory to any address it chooses.

Also note, the 4gb number is because the hardware that does addressing supports only 32 bit numbers, the largest number one can hold in a 32 bit number is 2^32 = 4,294,967,296. This is 4 gb. So one can only map 4gb of addresses to memory.

This is just a crappy intro, please do some googling on virtual memory.