“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

A, processes,

Process is a running activity of a program on a data set in a computer. It is the basic unit of resource allocation and scheduling in the system and the basis of operating system structure.

  • In the early process-oriented computer architecture, a process was the basic execution entity of a program.
  • In modern thread-oriented computer architectures, processes are containers for threads.

A program is a description of instructions, data and their organizational form, while a process is an entity of the program.

1.1 Process Switchover

Instead of blocking and waiting, the CPU switches to another process. When the data load is complete, the CPU receives an interrupt and continues to execute the request.

With a single-core CPU, multiple processes are executed in a short period of time, creating the illusion of parallelism, which can actually be interpreted as concurrent operations.

1.2 Kernel mode and user mode

Processes can be divided into user processes and kernel processes.

For security, the user process is restricted and cannot access or obtain resources at will. Therefore, the kernel process is responsible for managing and allocating resources, and it has the highest authority, while the user process uses the allocated resources. In addition, the operating system must be able to switch back to the kernel process (via interrupts) as soon as necessary for the operating system to feel safe.

1.3 Switching from user mode to kernel Mode

1. When a system call occurs

2. When an exception occurs

3. When the peripherals are interrupted

I won’t explain too much here, but if you are interested, learn the principles of Linux on your own.

Second, the thread

A thread (English: thread) is the smallest unit in which an operating system can schedule operations.

It is contained within the process and is the actual operating unit within the process. A thread is a single sequential flow of control in a process, and multiple threads can be concurrent in a process, each performing a different task in parallel.

2.1 Relationship between processes and threads

1. Multiple threads can exist in a process.

2. Each thread can execute concurrently;

3. Each thread can share the address space, files and other resources in the current thread.

Thread is the basic unit of scheduling, and process is the basic unit of resource ownership;

5. When a process has only one thread, it is considered equal to a thread.

2.2 Thread memory model

  • Before looking at the thread memory model, let’s first look at the hardware memory model of the computer.

    Register: Register component, including general register, special register and control register. General purpose registers can be divided into fixed point number and floating point number, which are used to store the register operands temporarily stored during the execution of instructions and the intermediate (or final) operation results. Universal register is one of the important parts of CPU.

    High-speed multi-level cache: Used to solve the speed difference between CPU core and memory. CPU core is fast, memory is much slower, and cache is faster than memory.

    Cache protocol: The introduction of multi-level cache causes cache inconsistency in the multi-core CPU era. MESI protocol cache is introduced to solve this problem to ensure that the information in the cache is consistent with the information in memory. MESI protocol caching is not covered here.

  • Next, let’s look at the relationship between the Java memory model and the threading model

    How does a Java program write once and run everywhere manifest?

    The JVM memory model masks memory models for different hardware. The JVM memory model specifies that all variables are stored in the JVM’s main memory, which is heap memory, meaning shareable variables (instantiated objects new, arrays, etc.). The concept of main memory and working memory is proposed here. Each thread has its own working memory and stores its own private variables. The thread obtains a copy of the variables from main memory as its own private variables and is not allowed to directly manipulate the variables in main memory. The working memory of a thread is also independent and cannot manipulate variables of other threads.

  • The relationship between hardware memory model and JVM memory model

    As shown in the figure below, the JVM memory model and the hardware memory model are similar, but not identical. However, most of the JVM’s data is stored in hardware main memory, and some is stored in registers or caches, which I’ll briefly explain here.