This is the first day of my participation in the August Challenge. For details, see:August is more challenging

preface

Talking about Java concurrency is a must have for every programmer, as much a test of a programmer’s fundamentals as data structures and design patterns. This activity is also borrowed to summarize the Java concurrent knowledge framework and how to use Java in our usual projects. This article mainly introduces the basic computer theory and Java concurrent programming some common operating systems some confusing concepts

Java Concurrency Fundamentals

1. Modern computer theoretical model

Modern computer models are based on the von Neumann computer model.

The five core components of the computer

  1. Controller (Control), is the central nervous system of the whole computer, its function is to explain the Control information prescribed by the program, Control according to its requirements, schedule programs, data, address, coordinate the work of various parts of the computer and access to memory and peripherals.
  2. The function of the Datapath is to carry out various arithmetic operations and logical operations on the data, that is, to process the data.
  3. Memory, “Memory,” the function of Memory is to store programs, data, various signals, commands, and other information, and provide these information when needed.
  4. Input system (Input system), Input device is an important part of the computer, Input device and output device together you as an external device, referred to as peripheral, the role of the Input device is to program, original data, text, characters, control commands or data collected on the spot and other information Input to the computer. Common input devices are keyboard, mouse and so on.
  5. Output system: Output equipment and input equipment is also an important part of the computer, it is the intermediate result or final result of the external computer, a variety of data symbols and characters in the machine or a variety of control signals and other information Output.

2. Processes and threads

2.1 process

Resource allocation unit. Creation is slow and context switch is expensive

2.2 the thread

The basic unit of CPU scheduling. Process resources are shared between threads.

Note: There is no concept of threads under The Linux operating system, threads are just a concept that is easy to understand to cater to developers.

3 Thread classification

There are two main types of threads

  • User-level threads
  • Kernel-level threads
  • Mixed thread model

The basics introduced here are easier to understand when you understand concurrency principles and some of the improvements that open source frameworks make to the infrastructure.

3.1 Kernel level threads

Kernel-level Thread: The kernel-level Thread model completely relies on the kernel-level Thread (KLT) provided by the operating system Kernel to realize multi-threading. Under this model, thread switching scheduling is completed by the system kernel, which is responsible for mapping tasks executed by multiple threads to each CPU for execution.

Programs do not use kernel threads directly, but use a high-level interface of kernel threads — Light Weight Process (LWP). A Light Weight Process is a thread in the common sense. Since each lightweight Process is supported by a kernel thread, only internal threads are supported first. In order to have lightweight processes. This 1:1 relationship between lightweight processes and kernel threads is called the one-to-one threading model.

Advantages:

(1) When there are multiple processors, multiple threads of a process can execute simultaneously. (2) Because the kernel level thread only has a very small data structure and stack, switching speed is fast, of course, it itself can also be realized with multi-threaded technology, improve the speed of the system.

Disadvantages:

(1) The thread runs in user mode, while the scheduling and management of the thread is implemented in the kernel. The transfer of control from one thread to another thread requires a mode switch from user mode to kernel mode and then to user mode, which occupies system resources. (it must be monitored by the kernel)

3.2 User-level threads

User-level thread is a thread that is implemented in the user program without kernel support. Its kernel switching is controlled by the user-mode program without kernel interference.

Broadly speaking, a Thread as long as it’s not a kernel Thread, can be considered a User Thread, the User Thread (UT), as a result, from this definition, a lightweight process also belong to the User Thread, but the realization of lightweight process is always based on the kernel, should undertake many operating system calls, efficiency will be limited.

In the narrow sense, the user thread refers to the implementation which is completely based on the user space thread library, and the system kernel cannot perceive the existence of the thread. User threads are created, synchronized, destroyed, and scheduled entirely in user mode, without kernel help. If the program is implemented properly, such threads do not need to switch to kernel mode, so operations can be very fast and inexpensive, or they can support a larger number of threads. Some multithreading in high-performance databases is implemented by user threads. This 1: N relationship between the process and the user thread is called a one-to-many threading model.

Advantages:

(1) The scheduling of threads does not require the direct participation of the kernel, and the control is simple. (2) Can be implemented in operating systems that do not support threads. (3) Only one thread is running in the same process at the same time. If one thread uses the system call and blocks, the whole process will be suspended, which can save more system resources.

Disadvantages:

(1) A user-level thread blocking will cause the entire process to block. (2) User-level threads cannot take advantage of the system’s multiprocessing, and only one user-level thread can be executed.

3.3 Mixed thread model

In addition to relying on the kernel thread implementation and being implemented entirely by the user program itself, there is another way to use kernel threads together with user threads.

In this hybrid mode, the ratio of user threads to lightweight processes is variable, i.e., N: M. Many UNIX family operating systems, such as Solaris, HP-UX, and others, provide an implementation of the N: M threading model.

Thread model

4.1. A 1:1 relationship is called a one-to-one threading model

Threads that have always been created using apis or system calls are typically one-to-one threads. For example, Linux creates a thread using Clone, and Win creates a thread using CreateThread.

4.2.1 :N One-to-many threading model

Multithreading in some high-performance databases is implemented by user threads.

4.3. N:M mixed thread model

In this hybrid mode, the ratio of user threads to lightweight processes is variable, i.e., N: M. Many UNIX family operating systems, such as Solaris, HP-UX, and others, provide an implementation of the N: M threading model.

5. Kernel and user mode

What are kernel mode and user mode? Simply put, a thread is in user mode when it is running in user space and in user mode when it is running in kernel space.

5.1. What is kernel space and user space?

Typically, the 32-bit Linux kernel virtual address space is divided into 0-3G for user space and 3-4G for kernel space (note that the kernel can only use 1G of linear addresses). Note that this is the 32-bit kernel address space partition; the 64-bit kernel address space partition is different. (For details, refer to: High-end memory details)

Take, for example, 4 GIGABytes of memory

5.2. Why is kernel space and user space divided?

Of all the instructions in the CPU, some are very dangerous and will crash the system if used incorrectly, such as clearing memory, setting the clock, and so on. If all programs were allowed to use these instructions, the probability of a system crash would be greatly increased.

Therefore, the CPU divides instructions into privileged and non-privileged instructions. For those dangerous instructions, only the operating system and its related modules are allowed to use them, and ordinary applications can only use those instructions that do not cause disaster.

For example, Intel cpus have four privilege levels: Ring0 to Ring3. In fact, Linux only uses Ring0 and Ring3 runlevels (the same is true for Windows).

A process is said to be running in user mode when it is running at the Ring3 level, and in kernel mode when it is running at the Ring0 level.

5.3. How to Perform state transition?

For example, under the user mode program, need to perform some only kernel permissions some operations, such as memory, network read and write, and so on, need to be converted to the kernel state to do

    1. System calls to enable applications to access kernel-managed resources such as CPU, memory, and I/O. The kernel must provide a common set of access interfaces called system calls.
    1. Exception. When the CPU executes the program running in user mode, some unknown exceptions occur. At this time, the current running process will be triggered to switch to the kernel-related program that handles this exception, and the kernel mode will be switched, such as page missing exception.
    1. Peripheral interrupt. When the peripheral device completes the operation requested by the user, it will send the corresponding interrupt signal to the CPU. At this time, the CPU will pause the execution of the next instruction to be executed and instead execute the corresponding handler of the interrupt signal. Then the process of conversion naturally also occurred from the user mode to the kernel mode switch. For example, when a hard disk read/write operation is complete, the system switches to the disk read/write interrupt handler to perform subsequent operations.

Reference documentation

Why do you need to distinguish between kernel space and user space on a Linux system? Linux user space and kernel space — High level memory Java threading model modern operating system PS: Understanding the operating system underlying implementation mechanism, will let us more easily understand the essence of some high performance framework and design clever!