[OS / Linux] Process, thread, memory management

Lack of knowledge

From the SRE business, I feel that I lack knowledge of the OS mechanism and Linux mechanism every day. Recently, I've been reading books around it and gradually supplementing my knowledge. This article will be the output when it is complemented. If you have any suggestions, I would appreciate it if you could comment.

In writing this article, I mainly referred to the following books! Infrastructure engineer's textbook 2 Technology and knowledge effective for skill improvement

Processes and threads


It refers to the minimum execution unit on the OS. The process is executed by allocating CPU resources.

Elements that make up the process

State transition

There are "Running Town", "Running", and "Hibernate" states. Each time a process is created, it transitions to various states.

Create a new process

When you create a new process, you start by copying (called forking) a process that already exists. You can also check it with the command, so please check it. pstree

If you type the ls command, it will be executed in the following order.

1.Type the ls command
2.bash process is forked
3.Child process generation(Virtual memory can be allocated.)
4.The contents of the child process become the ls command by the exec system call


It is an execution unit of a task that can be created multiple times in a process so that parallel processing can be performed.


Difference from process

Although it is the same structure, thread is realized by passing a flag such as whether to copy the memory space when building.

When creating a Process, all the information of the parent process is passed on to the child process, and virtual memory is reserved for the child process, so it is a relatively heavy process.

As mentioned in the article below, in the historical background, there were only processes at first, no threads. And when creating a process, the parent process forks the child process and all the information is inherited, which is inefficient. I think that it is ** threads ** that can share the memory space of the process and realize parallel processing.

[Parallel processing, this and that of parallel processing --Qiita](https://qiita.com/Kohei909Otsuka/items/26be74de803d195b37bd#%E4%B8%A6%E5%88%97%E5%87%A6%E7%90%86 % E3% 81% A7% E3% 81% A9% E3% 81% 86% E3% 81% 84% E3% 81% 86% E3% 81% A8% E3% 81% 8D% E3% 81% AB% E3 % 83% 91% E3% 83% 95% E3% 82% A9% E3% 83% BC% E3% 83% 9E% E3% 83% B3% E3% 82% B9% E3% 81% 8C% E3% 81 % 82% E3% 81% 8C% E3% 82% 8B% E3% 81% AE% E3% 81% 8B% E7% 90% 86% E8% AB% 96)


A mechanism that allows rewriting from only one thread

Memory management

When you create a process, virtual memory is allocated and the process runs.

Overall picture


Physical memory

Refers to main memory (main memory / RAM).

Virtual memory

It is a mechanism that makes the system look as if it has more memory than it actually is. When a process makes memory access, it goes through virtual memory.

The Linux Kernel

MMU The MMU (Memory Management Unit) built into the CPU converts the virtual memory address and the physical memory address. The MMU comes in with information called ** paging table ** and uses it to perform the conversion. Also, this correspondence is called mapping. [[Illustration] The essence, mechanism, and merits of virtual memory (virtual memory) ~ Swap, MMU, paging table ~ \ | SE guideline](https://milestone-of-se.nesuke.com/sv-basic/ architecture / virtual-memory-and-swap /)


Suppose a process runs out of physical memory when it asks the OS to allocate memory. In that case, the already allocated physical memory space is released to the swap area. (Swap out) Conversely, when the expelled data is needed, the data is returned from the swap area to the physical memory space. (Swap-in) Of the virtual memory, the area used by the HDD is the swap area.

System call

The OS provides a function (API) called "system call" for general programs, and in principle it is supposed to be accessed indirectly by that system call.

I think it's an image of the interface. ・ It mediates access to the Linux kernel.

[[Illustration] User space and kernel space for beginners, system call, MMU / memory protection, mechanism \ | SE guidepost](https://milestone-of-se.nesuke.com/sv-basic/architecture/user -space-kernel-space /)

Linux kernel

The OS is the mediator between hardware and software. The Linux kernel is a core function of such an OS.

This explanation in the article below is pretty nice.

[For beginners] What is the Linux kernel? --Qiita

image.png Quoted from below [File: Layer-of-System.png](https://uc2.h2np.net/index.php/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB : Layer-of-System.png)

Kernel space and user space

User space is the memory area used by applications (software running on the OS). Kernel space refers to the memory area used by the kernel.

This is a separate kernel space and user space within the MMU.

Why you need to separate

If anyone can access the kernel space, it will lead to access to the OS. This can destroy the OS itself at the same time. Therefore, I dare to divide the space in this way.

If you want to use kernel space

User-mode processes do not have access to kernel space. However, when I want to use it, I can switch between user mode and kernel mode with a function called ** Context Switch **.

Introduction to Linux Kernel User Space and Kernel Space \ | Introduction to Linux Device Driver Development

Recommended Posts

[OS / Linux] Process, thread, memory management
Linux process
Jupyter process management
Linux (command memory)
[Linux] [C] Get CPU time per process / thread
Summarize what you learned about OS memory management
OS and Linux distribution
Boot time memory management
Memory Management »Concepts overview
[Translation] Spark Memory Management since 1.6.0
Linux main package management system