Kernel Space & User Space


The Random Access Memory can be logically divided into two distinct regions namely - the kernel space and user space.

When a process is created and its virtual address space is divided into user-space and a kernel-space where user space region contains data, code, stack, heap of the process & kernel-space contains things such as the page table for the process, kernel data structures and kernel code etc. To run kernel space code, control must shift to kernel mode (0x80 software interrupt for system calls) & kernel stack is basically shared among all processes currently executing in kernel space.

The maximum size of the address space depends on the length of the address register on the CPU.

                         

Kernel Space

Kernel can access all parts of Memory, and to access some part of the kernel, the user processes have to use the predefined system call such as "read", "write", "open".

The system calls act as an interface between the user processes and the kernel processes. The access rights are placed on the kernel space in order to stop the users from messing up with kernel.

When a system call occurs, a software interrupt is sent to the kernel. The CPU may handover the control temporarily to the associated interrupt handler routine. The kernel process which was halted by the interrupt resumes after the interrupt handler routine finishes its job.

User Space

On systems with 32-bit address registers, the maximum size of address space is 232 bytes, or 4 GiB. Similarly, on 64-bit systems, 264 bytes can be addressed.
Such address space is called virtual address space. It is not actually related to physical RAM size.
On Linux platforms, virtual address space is divided into kernel space and user space.
An architecture-specific constant called task size limit, or TASK_SIZE, marks the position where the split occurs:
the address range from 0 up to TASK_SIZE-1 is allotted to user space;
the remainder from TASK_SIZE up to 232-1 (or 264-1) is allotted to kernel space.
On a particular 32-bit system for example, 3 GiB could be occupied for user space and 1 GiB for kernel space.
Each application/program in a Unix-like operating system is a process; each of those has a unique identifier called Process Identifier (or simply Process ID, i.e. PID). Linux provides two mechanisms for creating a process: 1. the fork() system call, or 2. the exec() call.
A kernel thread is a lightweight process and also a program under execution. A single process may consist of several threads sharing the same data and resources but taking different paths through the program code. Linux provides a clone() system call to generate threads.
Example uses of kernel threads are: data synchronization of RAM, helping the scheduler to distribute processes among CPUs, etc.
Some architectures strongly suggest an address space split; for example, on x86-64, the virtual address space is split into two halves, growing from either end of the address space. On such architectures, the kernel follows the suggestion; on x86-64, the bottom half of the address space is used for user-space allocations, the top half for kernel allocations. 

FAQs

1) Why can't we handle the memory requirements with RAM? or what is virtual memory?
Today, most personal computers (PCs) come with at least 8 GB (gigabytes) of RAM. But, sometimes, this is not enough to run several programs at one time. This is where the virtual memory comes in. A system using virtual memory uses a section of the hard drive(SSD) to emulate RAM. With virtual memory, a system can load larger or multiple programs running at the same time, enabling each one to operate as if it has more space, without having to purchase more RAM because RAM is expensive.
2) Difference between Virtual memory and virtual address space?
Virtual memory is a memory management technique developed for multitasking kernels. Virtual address space is a memory mapping mechanism available in modern operating systems.
3) Difference between fork and exec?

The fork, on the one hand, generates new processes while simultaneously preserving its parent process. The exec, on the other hand, creates new processes but doesn't preserve the parent process simultaneously.

References  

Gaurav Sen Channel




Comments