Operating system and process scheduling

1. Computer operating system

1. Operating System

The operating system is the collective name for a group of software that manages computer resources. The operating systems we often come into contact with in our daily life are: windows、IOS、Android、鸿蒙,以及Linux系统Wait, what is an operating system? How do computers work?

A computer is made up of software and hardware that work together; in fact, the operating system can be seen as a set of software between software and hardware, which mainly plays two roles:

  • Provide a stable operating environment for software
  • Manage various hardware devices

2. Schematic diagram of computer system

The following figure is a schematic diagram of a computer system. The operating system belongs to the system software. You can learn about it:
Computer composition diagram
Of course, what we are most concerned about when learning JavaWeb is not its hardware. For now, it should not be how the operating system is implemented. The content we are most closely concerned about is the process management module

2. Process

1. Process/Task

To put it simply, a running 程序(.exe)process is a process. From the perspective of operating system scheduling, when a process is running, it needs to apply for resources from the operating system, so a process is the 资源分配basic unit of the operating system.
Insert image description here
Each of the above tasks is a process. We can see that there are more than a hundred processes. With so many processes running at the same time, in order to ensure orderly execution on the computer, process management needs to be introduced ~

manage:

  • First describe: clearly express what characteristics this thing has. The operating system is mainly implemented through C/C++. The description here actually uses structures/classes; the structure of the process is called "PCB" (process control block ) ) process block.
  • Reorganization: Use a data structure (doubly linked list) to organize many such structures/objects together
  • //Create process: first create the PCB, and then load the PCB into the doubly linked list
  • //Destroy the process: find the PCB of the linked list and delete it from the linked list
  • //View the task manager: just traverse the linked list

2. Process control block abstraction (PCB Process Control Block)

A process has many attributes, so some of the more critical attributes are stored in the PCB. The main attributes are as follows:

class PCB {
    
    
    // 进程的唯一标识 —— pid;
    // 进程关联的程序信息,例如哪个程序,加载到内存中的区域等    
    // 分配给该资源使用的各个资源
    // 进度调度信息(下面讲解)
}

illustrate:

  • Identity PID: A process has a unique identity
  • Memory pointer: *Indicates which part of the memory is used by the current process (the process consumes certain hardware resources).
  • File description table: During the running of the program, every time the process opens a file, a file descriptor will be added, and the file descriptor will be stored in a sequence table to form a file description table.
  • Auxiliary attributes: This type of attributes is mainly used to 辅助进程调度describe attributes related to CPU resources.

3. Process Scheduling

The computer cannot do without the CPU when executing programs. The number of cores of the CPU is equivalent to the number of people working, and the number of threads is equivalent to the efficiency of work.
Insert image description here
As shown in the figure above, common CPUs nowadays are mostly six cores and twelve threads, eight cores and sixteen threads, etc. , on the other hand, the number of our processes is often in hundreds. Note that what we want is to do it at the same time! ! ! So how do they perform so many tasks?

  • Parallelism: At the same time, everyone can do their own thing without disturbing each other. The processes are parallel.
  • Concurrency: First do A for a while, then do B for a while, then do C for a while, and then do A~. As long as the switching is fast enough, we consider ABC to be concurrent.

Through parallelism + concurrency, we can realize that the CPU executes hundreds of processes at the same time . Note:并行和并发只有在微观上有区分,宏观上我们是区分不了的,因此我们将并发+并行统称为并发。

The auxiliary attributes of process scheduling usually include the following:

  • process status
  • process priority
  • Accounting information for the process
  • process context

illustrate:

  • Process status: describes the next scheduling method of this process;阻塞状态:改进程暂时无法在CPU上执行;2.就绪状态:随时可以在CPU上执行
  • Priority: Processes must have order and time allocation when scheduling
  • Accounting information: counts the execution time of the process on the CPU and uses it as a reference for adjustment.
  • Context: Generally speaking, it refers to the archiving and loading of games; specifically, it refers to the execution of intermediate values ​​and computer instructions executed by the CPU during execution.

4. Memory Management

The memory management mentioned here mainly refers to the virtual address space . In fact, the memory address accessed by each process is not real. Instead, a page table is used to map the virtual address space to the real address space.
Insert image description here
As shown in the figure, the process directly accesses Memory, if there are code bugs such as array out-of-bounds, etc., it is very likely that an unrelated process will cause a bug in another process. Obviously, this implementation cost is extremely bad, and the usage cost is also very high. In order to allow each process to Without interfering with each other, a "virtual address space" appears. Each process can only access its own address space, successfully completing the isolation between processes.

The virtual address mainly refers to the process that when accessing the address, the operating system will map it to the real memory address through an intermediate station "page table". If bugs such as wild pointers and array out-of-bounds occur, an error will be reported by the page table. The contents of the real memory address cannot be modified, thus causing no interference to other processes.

Processes cannot interfere with each other, which greatly improves the stable operation of the operating system!

5.Inter Process Communication

Modern applications often cannot complete a complex business requirement through a process independently. They always require process and process to cooperate to achieve the purpose of the application. In this way, there is a need for "information exchange" between processes. The need for inter-process communication emerged at the historic moment.
Currently, the process communication mechanisms provided by mainstream operating systems are as follows:

  1. pipeline
  2. Shared memory
  3. document
  4. network
  5. Signal amount
  6. Signal

Among them, network is a relatively special IPC mechanism. In addition to supporting communication between two processes on the same host, it also supports communication between processes on different hosts within the same network.

Guess you like

Origin blog.csdn.net/m0_65038072/article/details/130631611