Required knowledge
-
The computer is also called the computer, that is, the brain with electricity. The computer was invented to enable him to work like a human after being powered on, and it is more efficient than human because it can be uninterrupted for 24 hours.
-
The five major components of a computer
Controller
Operator
Memory
input device
Output device
The core of the computer really works is the CPU (controller + arithmetic unit = central processing unit)
-
For a program to be run by a computer, its code must be read from the hard disk to memory, and then the CPU fetches instructions before executing
Operating system history
Reference blog: https://www.cnblogs.com/Dominic-Ji/articles/10929381.html
Manual operation (punched card)
Batch processing (tape storage)
Online batch processing system
Offline batch processing system
Multi-channel technology
Single core achieves concurrent effect
Required knowledge
-
Concurrency
It looks like running concurrently can be called concurrent
-
parallel
Real simultaneous execution
ps:
- Parallel must be considered concurrent
- Single-core computers certainly cannot achieve parallelism, but they can achieve concurrency! ! !
Supplement: We directly assume that a single core is a core, and only one person works, don't consider the number of cores in the CPU
Multi-channel technical illustration
Save the total time spent running multiple programs
Multi-channel technology key knowledge
Taking in space and taking in time
-
Spatial multiplexing
Multiple programs share a set of computer hardware
-
Multiplexing in time
Example: Washing clothes for 30s, cooking for 50s, boiling water for 30s
Single channel needs 110s, multiple channels only need the long switch of task to save time
Example: Playing a game while eating and saving state
Switch + save state
"""
切换(CPU)分为两种情况
1.当一个程序遇到IO操作的时候,操作系统会剥夺该程序的CPU执行权限
作用:提高了CPU的利用率 并且也不影响程序的执行效率
2.当一个程序长时间占用CPU的时候,操作吸引也会剥夺该程序的CPU执行权限
弊端:降低了程序的执行效率(原本时间+切换时间)
"""
Process theory
The difference between program and process
"""
程序就是一堆躺在硬盘上的代码,是“死”的
进程则表示程序正在执行的过程,是“活”的
"""
Process scheduling
-
First come first served scheduling algorithm
"""对长作业有利,对短作业无益"""
-
Short job priority scheduling algorithm
"""对短作业有利,多长作业无益"""
-
Time slice rotation method + multi-level feedback queue
Three state diagram of process running
Two pairs of important concepts
-
Synchronous and asynchronous
"""描述的是任务的提交方式""" 同步:任务提交之后,原地等待任务的返回结果,等待的过程中不做任何事(干等) 程序层面上表现出来的感觉就是卡住了 异步:任务提交之后,不原地等待任务的返回结果,直接去做其他事情 我提交的任务结果如何获取? 任务的返回结果会有一个异步回调机制自动处理
-
Blocking non-blocking
"""描述的程序的运行状态""" 阻塞:阻塞态 非阻塞:就绪态、运行态 理想状态:我们应该让我们的写的代码永远处于就绪态和运行态之间切换
Combination of the above concepts: the most efficient combination is asynchronous non-blocking
Two ways to start the process
Dingxinwan: The way code starts processes and threads is basically the same as code writing. You learn how to start a process and you learn how to start a thread
from multiprocessing import Process
import time
def task(name):
print('%s is running'%name)
time.sleep(3)
print('%s is over'%name)
if __name__ == '__main__':
# 1 创建一个对象
p = Process(target=task, args=('jason',))
# 容器类型哪怕里面只有1个元素 建议要用逗号隔开
# 2 开启进程
p.start() # 告诉操作系统帮你创建一个进程 异步
print('主')
# 第二种方式 类的继承
from multiprocessing import Process
import time
class MyProcess(Process):
def run(self):
print('hello bf girl')
time.sleep(1)
print('get out!')
if __name__ == '__main__':
p = MyProcess()
p.start()
print('主')
to sum up
"""
创建进程就是在内存中申请一块内存空间将需要运行的代码丢进去
一个进程对应在内存中就是一块独立的内存空间
多个进程对应在内存中就是多块独立的内存空间
进程与进程之间数据默认情况下是无法直接交互,如果想交互可以借助于第三方工具、模块
"""
join method
Join is to let the main process wait for the sub-process code to finish running before continuing. Does not affect the execution of other child processes
from multiprocessing import Process
import time
def task(name, n):
print('%s is running'%name)
time.sleep(n)
print('%s is over'%name)
if __name__ == '__main__':
# p1 = Process(target=task, args=('jason', 1))
# p2 = Process(target=task, args=('egon', 2))
# p3 = Process(target=task, args=('tank', 3))
# start_time = time.time()
# p1.start()
# p2.start()
# p3.start() # 仅仅是告诉操作系统要创建进程
# # time.sleep(50000000000000000000)
# # p.join() # 主进程等待子进程p运行结束之后再继续往后执行
# p1.join()
# p2.join()
# p3.join()
start_time = time.time()
p_list = []
for i in range(1, 4):
p = Process(target=task, args=('子进程%s'%i, i))
p.start()
p_list.append(p)
for p in p_list:
p.join()
print('主', time.time() - start_time)
Data isolation between processes
from multiprocessing import Process
money = 100
def task():
global money # 局部修改全局
money = 666
print('子',money)
if __name__ == '__main__':
p = Process(target=task)
p.start()
p.join()
print(money)