table of Contents
History of operating system development
Punched card
Batch Online
Batch offline
Multi-channel technology:
Single channel: a plurality of program execution string
Multi-channel:
Save state switching +
Multiplexing in space
A computer (CPU), a space can be provided to a plurality of uses.
Multiplexed on time
The current program encountered IO operations, it will immediately switch the CPU execute permissions
The current program uses the CPU time is too long, it will immediately switch the CPU execute permissions
Concurrent and Parallel
- Concurrency: Looks like run simultaneously.
- Parallel: Run really sense at the same time.
- Can not be achieved in the case of parallel single-core, in order to achieve parallel, there must be a plurality of CPU
process
Program: a bunch of code.
Process: The process of a bunch of code to run.
Process Scheduling: time slice of the Law + rating feedback queue
Three states of the process:
Ready state
All process is created will first enter the ready state
Runnable
By a process of scheduling each process will be transferred to run state
Blocking state
Program in the operating mode of encounter IO will enter the blocking state
If the IO end, will immediately enter the ready state, does not directly enter the running state
Note: The execution time of the CPU for too long will look like obstruction, in fact, not blocked, depriving its CPU execute permissions, then the process will return to the ready state.
Synchronous and asynchronous
- Synchronization: After submission of a task, still waiting for the end of this mission, the next to submit and execute
- Asynchronous: After submission of a task, do not need to wait and can immediately perform the next task
Blocking and non-blocking
- Blocking: blocking state (encounter IO, enters the blocking state)
- Non-blocking: ready state, running state
Create a process in two ways
from multiprocessing import Process
import time
method one:
def task(): print('子进程开始执行') time.sleep(1) print('子进程结束完毕') # 在windows系统下创建,必须要再__main__执行 - __main__ 下: # target = 执行任务(函数地址) p = Process(target = task) p.start() #告诉操作系统,创建子进程 # join p.join() # 让主进程等待所有子进程结束后才能结束 print('主进程')
Second way:
- 自定义类,继承Process class Myprocess(Process): def run(self): # 此处是子进程的任务 print('子进程开始执行') time.sleep(1) print('子进程结束完毕') - __main__下: p = MyProcess() p.start() # 告诉操作系统,创建子进程 # join p.join() # 让主进程等待所有子进程结束后才结束 print('主进程')
- join () to get the main process after the wait to end all child processes
- Inter-process data is isolated
- Property of process objects
# 可以获取子进程 pid号 current_process().pid --->return Process().pid # 在子进程中打印是子进程的pid号,在父进程中打印父进程的pid号 os.getpid() # 主主进程pid号 os.getppid() current_process()-->p # 终止子进程 p.terminate() # 判断进程是否存活 is_alive
Daemon
After the main course, you have to follow the child process ends
# 注意:必须要在start()之前设置 p.daemon = True p.start() # p.daemon = True 报错
The parent process are two ways to recover the child process PID numbers:
- join
- Normal after the end of the parent process
Zombie process and orphaned (understand):
Zombie process: After the child process, the parent process does not recover
PID
, leading to the child permanently retainedPID
No.Disadvantages: occupation
PID
number, operating system resources occupancyOrphan process: child process has not ended, the parent process ends unexpectedly, causing the child process can not be recycled at the end of the parent process
This makes the child as a "orphan", and then recovered by the operating system comes with the "welfare."
Process mutex
Let concurrent become a serial, at the expense of efficiency, ensure data security
Used when the program concurrent execution, data needs to be modified
queue
It is a FIFO queue
Corresponds to a queue memory space is generated, a plurality of data can be stored, the data is advanced to the top surface
Stack
Stack is a last-out
IPC (interprocess communication)
Isolated from each other when the inter-process data, are to achieve inter-process communication, a queue may be utilized
Producers and consumers
Manufacturer: production data
Consumers: Use of data
In the program: through the queue, add the queue data producers, consumers get the data from the queue
Thread
What is the thread
Threads and processes are virtual units, the purpose is to better describe about something
- Process: Resource Unit
- Thread: implementation units
Why use threads
Saving memory resources
Open process
- Open up a space for the name of each open process takes up a copy of memory resources
- Will comes with a main thread
Open thread
- A process can open multiple threads
- Overhead is much smaller than the thread process
Note: The threads can not achieve parallel, concurrent threads can only be achieved, the process can be implemented in parallel
Thread mutex
Data is shared between threads