Java multi-process (detailed)

meaning of process

        A simple understanding is a program that is running, a program that is running. A program that is not running is not called a process. If the same program is run multiple times, multiple processes may be generated.

        The programs we usually refer to are some exe executable files, and the process will be involved only after the program is run.

The difference between a process and a program

        A program is an executable file, just a thing on the hard disk (static)

        If you double-click the program, the operating system will load the data and instructions in the executable file into the memory, and let the CPU execute the instructions here, complete a series of related tasks, and start running (dynamic) is the process

        The process can work, which means that the process can consume certain hardware resources. Executable programs only occupy hard disk space, while processes consume cpu resources, memory resources, hard disks, and network bandwidth.

process management

        For the management of processes in computers, the core idea is to describe first, then organize 

        1. Description

        A special structure will be used to record various attributes in a process.

        This structure is called PCB (process control block) (PCB in Linux, in the source code is a task_struct structure)

(General name, the structure used to describe the process in various systems can be called PCB)

        2. Organization

        Will use a series of data structures to effectively organize multiple processes, and facilitate traversal, search, and summary data at any time

        For Linux, it is usually organized in a way such as a doubly linked list

        When using a doubly linked list for organization

                1. To view the list of processes, the essence is to traverse this linked list.

                2. To create a process is to create a PCB structure and insert it into the linked list

                3. To destroy a process is to delete and release the PCB structure from the linked list

What are the properties in PCB

        1. The identification of the PID process

                At the same time on the same system, the pid of each process is different, which is equivalent to the identity of the process and is used to distinguish the process

        2. Memory pointer

                It expresses what kind of memory resources the process corresponds to, such as what memory resources should be stored

                The main storage is the instructions and data loaded from the exe executable file

        3. File descriptor table

                related to hard disk resources

                The hard disk is hardware, and applications generally cannot directly access the hardware layer.

                In fact, the operating system is abstracted into the concept of a file. The program operates on the file, and the file is actually stored on the hard disk. Using this method, the program indirectly operates the hard disk.

                Each process will have a file descriptor table to record which files the current process is using

                When the operating system opens a file, a "file descriptor" will be generated, and the file descriptor table will be used to organize the file descriptors

        4. Accounting information

                It is equivalent to a statistical information, which will count how long the process has been executed in the cpu and how many instructions have been executed. It is a comprehensive overview of the scheduling work of the process and prevents processes with higher priority from executing for too long.

Properties related to process scheduling in PCB

        1. Status

                Here are two common states (not all)

                        1. Ready state, the process is ready to be executed by the CPU at any time

                        2. Blocked state/sleep state, the process is not ready to be dispatched to the CPU

                states are interchangeable

        2. Priority

                When the system schedules processes, it is not completely fair. It will also decide the trade-off of time allocation according to the different priorities, so that system resources can be allocated to more important processes.

        3. Context

                These processes are run in turn, and if one run cannot be completed, it is necessary to ensure that the next time the CPU runs, it can continue to run from the position where it was last run, which is equivalent to saving and reading files.        

parallelism and concurrency

        1. Parallel

                At the same time, two processes run on the logical cores of two CPUs at the same time

        2. Concurrency

                Two processes run alternately on one CPU core

                Due to the extremely fast speed of the CPU switching process, the two processes are executed serially on the micro level, and the two processes are executed simultaneously on the macro level

Why not directly allocate memory to the process

        A long time ago, the operating system directly allocated memory to the process, but if the code in the process had a bug, such as an out-of-bounds pointer access in the code in the process, it might modify the data in other processes, causing other processes to crash , which is very unreasonable and will greatly affect the stability of the system, so now do not directly allocate memory to the process

virtual address

        1. The meaning of virtual address

                The virtual address is not the address that actually exists in the physical memory, but only exists in the process. Different processes can have the same virtual address, but they correspond to different physical addresses. The virtual address and physical address operating system can Make flexible conversions

        2. The role of virtual address

                After this setting, the effective virtual address of each process is a fixed range

                When a process uses memory with a virtual address, it needs to convert the virtual address into a physical address. During this conversion process, a check can be made on whether the virtual address is valid, which is very safe.

        3. Effect of virtual address

                With the blessing of the virtual address space, the processes are independent. Each process has its own virtual address space. One process cannot directly access or modify the memory in the virtual address space of another process, which strengthens the stability of the system.

The essence of interprocess communication

        Find a public area that can be accessed by multiple processes, and use the public area to complete data exchange.

        

                

Guess you like

Origin blog.csdn.net/q322359/article/details/131859676