Concurrent programming (1)

Introduction to Concurrent Programming Basics

The most important thing in learning concurrent programming is to first understand the concept of processes. A process is literally a process that is being executed. A process can also be said to be an abstract understanding of a running program. A program is just a bunch of code, and a process refers to the running process of a program. The same program executes two programs. Second, those are also two processes, and it is the CPU that is responsible for performing the task.

The realization of all functions in the computer is also done based on hardware, but it would be very troublesome if we humans directly operate the hardware, so after the wisdom of many generations of pioneers, an operating system was formed, and the concept of process is also derived from the operating system .

Simply put, an operating system is a control program that coordinates, manages, and controls computer hardware and software resources. We can complete the overall use of the computer through the operating system, which exists between hardware and software, and is essentially software.

The location of the operating system in the computer:

 

The two functions of the operating system are:

1. Encapsulate complex and ugly hardware operations into beautiful interfaces

2. Make the competition of the process for the hardware in an orderly way (the process will compete for the resources of the CPU)

multi-channel technology

The multi-channel technology originated from the release of the third-generation computing. In the process of performing a task, if the CPU needs to operate the hard disk, it sends an instruction to operate the hard disk. Once the instruction is issued, the mechanical arm on the hard disk slides to read the data to the In the memory, during this period of time, the cpu needs to wait, the time may be very short, but it is very long for the cpu, long enough to allow the cpu to do many other tasks, if we let the cpu switch to the Do other tasks, so that the CPU will not be fully utilized. This is the technical background of the multi-channel technology

Multi-channel technology:

Multi-channel in multi-channel technology refers to multiple programs. The implementation of multi-channel technology is to solve the orderly scheduling problem of multiple programs competing or sharing the same resource (such as cpu). The solution is multiplexing. Multiplexing is divided into multiplexing in time and multiplexing in space.

Spatial reuse : divide the memory into several parts, and put each part into a program, so that there are multiple programs in the memory at the same time.

 

Time multiplexing : when one program is waiting for I/O, another program can use the cpu. If enough jobs can be stored in the memory at the same time, the cpu utilization rate can be close to 100%, similar to our elementary school mathematics institute. learning co-ordination methods . (After the operating system adopts multi-channel technology, it can control the switching of processes, or compete for the execution authority of the CPU between processes. This switching will not only be carried out when a process encounters io, but a process will occupy the CPU for too long. It will also switch, or the execution authority of the cpu is taken away by the operating system)

 

The biggest problem with spatial multiplexing is that the memory between programs must be divided. This division needs to be implemented at the hardware level and controlled by the operating system. If the memory is not divided from each other, one program can access the memory of another program,

The first loss is security. For example, your qq program can access the memory of the operating system, which means that your qq can get all the permissions of the operating system.

The second loss is stability. When a program crashes, it may also reclaim the memory of other programs. For example, if the memory of the operating system is reclaimed, the operating system will crash .

 

Summarize:

    1. Generation background: for single core, achieve concurrency
    ps:
    The current host is generally multi-core, so each core will use multi-channel technology
    There are 4 cpus, a program running on cpu1 encounters io blocking, it will wait until the io ends and then reschedule, it will be scheduled to 4
    Any one of the CPUs is determined by the operating system scheduling algorithm.
    
    2. Spatial reuse: For example, there are multiple programs in memory at the same time
    3. Time multiplexing: multiplexing the time slice of a cpu
       Emphasis: When encountering io cuts, it takes up too long to cut the CPU. The core is to save the state of the process before cutting, so that
            To ensure that the next time you switch back, you can continue to run based on the position that was cut last time.

multi-Progress

Concurrency and Parallelism

Whether it is parallel or concurrent, in the eyes of the user, it is running 'simultaneously'. Whether it is a process or a thread, it is just a task. It is the CPU that really does the work. The CPU does these tasks, and a CPU can only perform a task

一 并发:是伪并行,即看起来是同时运行。单个cpu+多道技术就可以实现并发,(并行也属于并发)

 

 

二 并行:同时运行,只有具备多个cpu才能实现并行

 

        单核下,可以利用多道技术,多个核,每个核也都可以利用多道技术(多道技术是针对单核而言的

 

        有四个核,六个任务,这样同一时间有四个任务被执行,假设分别被分配给了cpu1,cpu2,cpu3,cpu4,

 

        一旦任务1遇到I/O就被迫中断执行,此时任务5就拿到cpu1的时间片去执行,这就是单核下的多道技术

 

        而一旦任务1的I/O结束了,操作系统会重新调用它(需知进程的调度、分配给哪个cpu运行,由操作系统说了算),可能被分配给四个cpu中的任意一个去执行

 

多道技术概念回顾:内存中同时存入多道(多个)程序,cpu从一个进程快速切换到另外一个,使每个进程各自运行几十或几百毫秒,这样,虽然在某一个瞬间,一个cpu只能执行一个任务,但在1秒内,cpu却可以运行多个进程,这就给人产生了并行的错觉,即伪并发,以此来区分多处理器操作系统的真正硬件并行(多个cpu共享同一个物理内存)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324810711&siteId=291194637