Programming Fundamentals of concurrent processes

1, the operating system development history

Research concurrent programming is actually the underlying principles study of the operating system, so we need from the history of the operating system begins to learn

Manual operation - punchcards

1946 The first computer was born the mid-20th Century 1950s, computer work are still using manual mode. At this time there is no concept of the operating system.Snipaste_2019-12-06_16-49-19

Corresponding to the programmer strip (or cards) has been loaded into the input unit perforated programs and data, and then start the machine to enter into the computer program and data memory, the data is then run through the console switch for starting the program; calculated, the printer outputs the calculation result; result after the user removed the tape and remove (or card), before allowing the next user on the machine.

Manual mode two characteristics:

  (1) user exclusive whole machine. The phenomenon does not occur because resources have been occupied by other users and wait, but the low utilization rate of resources.

  (2) CPU waits for manual operation. CPU utilization is not sufficient.

  1. 1, punch cards
  • Slow speed not read data Laid
  • CPU utilization is very low
  • Single user (a code) using
  1. 2, Batch
  • Extremely slow read data
  • CPU utilization is very low
  • Online (multiple codes) using
  • Efficiency is still low
  1. 3, off-line batch (design principles of modern operating systems
  • Read data faster
  • CPU utilization to improve

2, multi-channel technology

Multi-channel technology (based on the background of monocytes)

  • Single-channel: a path, been walking -------- Serial

    For example: a, b need to use the CPU, a first trial, b used up after waiting a, b in order to use CPU

  • Multi-channel

    For example: a, b need to use the CPU, a first use, b wait until entering a "IO or execution time is too long", will be a (+ save switching state), then the CPU can be used b, b to be encountered in the implementation of "IO or execution time is too long ", then the CPU execute permissions to a, until the end of the two programs

Multiplexing on the space:

Uses a plurality of CPU

Multiplexed on time:

Save state switching +

1, when the execution of the program encounters IO, the operating system will execute permissions CPU deprivation

Advantages: improve the efficiency of the CPU

2, when the program execution time is too long, too long to perform an operating system, the operating system will execute permissions CPU deprivation

Disadvantages: low efficiency program

3, the parallel concurrent

Concurrency: Looks like run

In the single-core (a cpu) case , when performing two a, b program, a first execution when a face IO, b began to scramble execute permissions cpu, and let b execution, they looks like at the same time transport to run on the positive sense.

Parallel: run in the true sense

In the case of multi-core (s cpu) of , when performing two a, b procedures, A and b are performed simultaneously. They are run in the true sense.

Face questions: In the case of single-core parallelism can be achieved? Not work

4, process

1. What is the process?

Process is a resource unit

2, processes and procedures:
procedures: a pair of code files

Process: the process of executing code, called the process

3, the process of scheduling (understand)

1) 先来先服务调度算法(了解)
    - 比如程序 a,b,若a先来,则让a先服务,待a服务完毕后,b再服务。
    - 缺点: 执行效率低。

2) 短作业优先调度算法(了解)
    - 执行时间越短,则先先调度。
     缺点:
     导致执行时间长的程序,需要等待所有时间短的程序执行完毕后,才能执行。

现代操作系统的进程调度算法: 时间片轮转法 + 多级反馈队列  (知道)

3) 时间片轮转法
     - 比如同时有10个程序需要执行,操作系统会给你10秒,然后时间片轮转法会将10秒分成10等分。

4) 多级反馈队列:
     1级队列: 优先级最高,先执行次队列中程序。
     2级队列: 优先级以此类推
     3级队列:

5, synchronous and asynchronous

Synchronous and asynchronous means "submission tasks"

Synchronization (Serial):

Two a, b and executes the program should be submitted, if after a first submission to the Executive, b have to wait for a finished, in order to submit the task.

Asynchronous (concurrent):

Two a, b and execute the program must submit, to submit and execute if a, b without waiting for a finished, you can submit jobs directly.

6, blocking and non-blocking

Blocking (waiting):

Those who encounter will block IO

IO: , input(), output(), time.sleep(3)Transmission of data

Non-blocking (no wait):

In addition to all non-blocking IO, (for example: counting from 1 + 1-1000000)

7 three states, the process of

Ready state:

Synchronous and asynchronous (job submission)

Run state:

Execution time of the program is too long ----> The program returns to the ready state

Non-blocking

Blocking state:

IO encounter

Interview questions: blocking and synchronization is the same as you? And asynchronous non-blocking is the same as you?

  • Submission tasks: synchronous and asynchronous
  • State of the process: blocking and non-blocking
  • Maximize the utilization of asynchronous and non-blocking, CPU's

8, the process of creating two ways:

method one:

from multiprocessing import Process
import time

def task(name):
    print(f'start{name}---')
    time.sleep(3)
    print(f'end{name}---')

if __name__ == '__main__':
    print("开始执行主进程。。。")
    #target=任务(函数地址)--->创建一个子进程
    #异步提交了3个任务
    obj1 = Process(target=task,args=('letin',))#args内是元组,注意加‘,’
    obj2 = Process(target=task,args=('letin',))
    obj3 = Process(target=task,args=('letin',))
    obj1.start()    #.start()告诉操作系统,去创建一个子进程
    obj2.start()
    obj3.start()
    # obj1.join()   #告诉主进程,等待子进程结束后,在结束
                    #主进程是当前程序(程序的执行过程)
    
结果:
开始执行主进程。。。
startletin---
startletin---
startletin---
endletin---
endletin---
endletin---

Second way:

Methods inherited Process

from multiprocessing import Process
import time

class MyProcess(Process):
    def run(self):
        print(f'start...{self.name}的子进程')
        time.sleep(3)
        print(f'end...{self.name}的子进程')

if __name__ == '__main__':
    list1 = []
    
    for line in range(4):
        obj = MyProcess()
        obj.start()
        list1.append(obj)

    for obj in list1:
        obj.join()

结果:
start...MyProcess-4的子进程
start...MyProcess-2的子进程
start...MyProcess-3的子进程
start...MyProcess-1的子进程
end...MyProcess-4的子进程
end...MyProcess-2的子进程
end...MyProcess-3的子进程
end...MyProcess-1的子进程
主进程

Guess you like

Origin www.cnblogs.com/leiting7/p/11997020.html