table of Contents
History of operating system development
1) punched card
A computer room, a card can only be used
drawback: CPU utilization lowest
2) online batch processing system
It supports multiple users to use a computer room
3) offline batch processing system
High-speed disk: improve reading speed file
Benefits: increase CPU utilization
4) Multi-channel technology
Single Channel
When using a plurality of CPU serial
You must be allowed after the end of a program, a program before proceeding to the next
Multi-channel technology
Multiplexing on the space:
CPU may be provided to a plurality of users to use
Multiplexed on time:
Switching + Save
IO operations:
input()
print()
time.sleep()
1) If the CPU has encountered an IO operation, the program will execute the current CPU use right immediately disconnected
Advantages: High CPU utilization
2) If a program uses the time course of the CPU, the CPU will immediately execute the programs using the right disconnection
Concurrent and Parallel
Concurrent: refers looks like running simultaneously, a plurality of programs save state continuously switching +
Parallel: run on a real sense, in the case of multi-core (multiple CPU), the simultaneous execution of multiple programs
process
Procedures and processes
Program: a bunch of code
A bunch of code running process: Process
Process Scheduling
FCFS scheduling
a, b program, if a program first-come, first occupied CPU
Cons: First use a program, such as program b must use a program can be used after the end of the CPU
Short operating priority scheduling
a, b procedures, who short time, the first priority scheduling using the CPU
Disadvantages: If a program using the longest time, there are N uses a short time, you must wait before you can use all the short time after the end of program
Round-robin method
CPU execution time of 1 second, N program loading, to one second plurality of equally divided into N time slices
Classification feedback queue
The execution priority level is divided into multiple layers
Level 1: the highest priority.
Level 2: The second priority, and so on
.........
Three state of the process
Ready state
when all processes created will enter the ready state, ready to dispatchRunnable
processes after dispatch, enter the running stateBlocking state
Those who encounter IO operation process, will enter the blocking state
if the IO end, you must re-enter the ready state
Synchronous and asynchronous:
It refers to the submission of the task
Synchronize
If two tasks required to submit, in the submission of a task,
After the execution of the task must wait for the end, in order to continue to submit to perform a second task
import time def test(): # IO操作 # time.sleep(3) # 计算操作 num = 1 for line in range(1000000000000000000000000): num += 1 if __name__ == '__main__': test() print('hello tank')
asynchronous
If two tasks required to submit, in the submission of a task,
You do not need to wait and can be submitted immediately and perform a second task
Blocking and non-blocking
Obstruction:
Blocking state, you will encounter blocking IO
Non-blocking
Ready state
Runnable
Interview questions: synchronous and asynchronous, blocking and non-blocking is the same concept it?
He stressed: not the same concept can not be mixed into a talk!
Maximize improve CPU utilization:
minimize unnecessary IO operations
Create a process of two conditions
join, can be recycled to the main process child process
''' join方法: 用来告诉操作系统,让子进程结束后,父进程再结束。 ''' # from multiprocessing import Process # import time # # # def task(name): # print(f'{name} start...') # time.sleep(2) # print(f'{name} over..') # # # if __name__ == '__main__': # p = Process(target=task, args=('jason', )) # p.start() # 告诉操作系统,开启子进程 # p.join() # 告诉操作系统,等子进程结束后,父进程再结束。 # print('主进程') from multiprocessing import Process import time def task(name, n): print(f'{name} start...') time.sleep(n) print(f'{name} over..') if __name__ == '__main__': p1 = Process(target=task, args=('jason', 1)) p2 = Process(target=task, args=('egon', 2)) p3 = Process(target=task, args=('sean', 3)) p1.start() p2.start() # 告诉操作系统,开启子进程 p3.start() # 告诉操作系统,开启子进程 p1.join() # 告诉操作系统,等子进程结束后,父进程再结束。 p2.join() p3.join() print('主进程')
The main process ends normally, the child process will be recycled to the main process
''' 进程间数据相互隔离: 主进程与子进程会产生各自的名称空间。 ''' from multiprocessing import Process x = 100 def func(): print('执行func函数...') global x x = 200 if __name__ == '__main__': p = Process(target=func) p.start() print(x) print('主')
And orphaned zombie process (to understand)
Zombie process: refers to the child process has ended, but there are PID number, not destroyed
Disadvantages: occupation PID number, operating system resources occupancy
Orphan process: it refers to the child process is still running, but the parent process ends unexpectedly
operating system optimization mechanisms: a orphanage, to help you recover the child without a father
Daemon
Refers to the end of the primary process, all processes that followed the end of the process to generate and recycle
from multiprocessing import Process
from multiprocessing import current_process
import time
def task(name):
print(f'{name} start...', current_process().pid)
time.sleep(5)
print(f'{name} over..', current_process().pid)
print(f'管家{name}')
if __name__ == '__main__':
p1 = Process(target=task, args=('jason', ))
# 添加守护进程参数
p1.daemon = True # True代表该进程是守护进程
p1.start()
print(f'egon 驾鹤西去...')