table of Contents
- Concurrent Programming
- 1. Concurrent Programming
- 2. How concurrency:
- 3. What is the process
- 4. Multi-channel technology
- 5. Concurrent important concepts
- 6. The process of three states
- 7. The process of creation and destruction
- 8. The difference between the processes and procedures
- 9. hierarchy process
- 10.PID and PPID
- 11.python how to use multiple processes
- 12.join function
- 13. The process of object common attributes
- 14. zombie process and orphaned
Concurrent Programming
1. Concurrent Programming
Why concurrency?
The default implementation is a serial program, that program from top to bottom, line by line execution order, the execution of the current task must be completed before the next one so resulting in low efficiency
Concurrency can perform multiple tasks simultaneously to improve the efficiency of the program
2. How concurrency:
1. The core principle is the multi-process multi-channel technology
2. Multithreading
3. coroutine
3. What is the process
It refers to the process of running programs, operating system scheduling and resource allocation basic unit.
Multi-process refers to the same time there are more programs are loaded into memory and executed
4. Multi-channel technology
The core principle is multi-process multi-channel technology
1. spatial multiplexing
Unified time, loaded into memory multiple tasks between multiple processes memory areas need to be isolated from each other, this separation is to isolate the physical level, its purpose is to ensure the security number
2. The time-multiplexed
It means that the operating system will do to perform switching between multiple processes
In both cases the task switching
1. When a process encounters an IO operation will automatically switch
2. When a task execution time exceeds a threshold forced handover
Note: You must save the state before the handover, in order to perform subsequent recovery
The conclusion is simple switch plus save
With multi-channel technology, the computer can concurrently handle multiple tasks simultaneously
5. Concurrent important concepts
Serial program executed sequentially from top to bottom
Concurrent quickly switch between different tasks
Parallel simultaneously performed must have real multicore CPU
These three concepts are described for processing tasks
Blocking refers encountered an IO operation, one state can not continue to execute code when the
Non-blocking means that the program did not encounter a state IO operations
It is a state of the program
6. The process of three states
Obstruction ready for operation
Which is operational and ready unblocked
7. The process of creation and destruction
For general-purpose computer must have the ability to create and destroy processes
create
1. The user interactive requests, double click
2. Call the open process interface consists of a running program. For example subprocess
3. start a batch job
4. The system initialization
destroy:
1. task is completed Opt-Out
2. Force the end of the taskkill kill involuntary
3. The program encountered an exception
4. serious errors such visits should not have access memory
8. The difference between the processes and procedures
Program is a bunch of code in a file
The process is the code read from the hard disk into memory and then perform generated
Process is generated by the program, a program can generate multiple processes
9. hierarchy process
Have a parent-child relationship in the process of linux, is a tree structure, they can find each other to each other
There is no hierarchy in the windows, the parent process can handle the transfer of the child process
10.PID and PPID
PID is the number of the current process, PPID is the parent process ID
Access PID and PPID:
import os
os.getpid()
os.getppid()
11.python how to use multiple processes
Create a child process of the way
1. Import Process class of multiprocessing, instantiate the class to perform the specified task target
This function is performed automatically import multiprocessing 2. Process class inherits the task of this class override the run method will be executed in turn run into the process
12.join function
Examples of process facilitator will use the join function main process waits for the child process is finished before proceeding
13. The process of object common attributes
if __name__ == '__main__':
p = Process(target=task,name="老司机进程")
p.start()
p.join()
# print(p.name) #进程名
# p.daemon #守护进程
# p.join()
# print(p.exitcode) # 获取进程的退出码 就是exit()函数中传入的值
# print(p.is_alive()) # 查看进程是否存活
# print("zi",p.pid) # 获取进程id
# print(os.getpid())
# p.terminate() #终止进程 与strat 相同的是 不会立即终止,因为操作系统有很多事情要做
# print(p.is_alive())
14. zombie process and orphaned
Orphan processes: when the parent process has ended and the child process is still running child process called orphan process, there is need for its existence, no adverse effects.
Zombie process: when a process has ended, however, it still has some data exist, this time called zombie process.