Essential knowledge review
-
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
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! ! !
Multi-channel technical illustration
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 need 110s, multi-channel only need to do that a long task switching saves time
Example: Playing a game while eating and saving state
Switch + save state
Switch (CPU) is divided into two situations
1. When a program encounters IO operation, the operating system will deprive the CPU to execute permissions of the program
effects: increase CPU utilization and does not affect the performance of programs
2. When When a program occupies the CPU for a long time, the operation attraction will also deprive the program of the CPU execution authority
.
Process theory
Required knowledge
The difference between program and process
A program is a bunch of code lying on the hard disk. A "dead"
process means that the program is being executed and is "alive."
Process scheduling
-
First come first serve scheduling algorithm: good for long jobs, not for short jobs
- Short job priority scheduling algorithm: good for short jobs, no benefit for long jobs
- Time slice rotation method: The time slice divides the fixed time into multiple parts, each part represents a time slice
- Multi-level feedback queue: priority
Two pairs of important concepts
Synchronous and asynchronous
-
-
"" "Describes how to submit tasks" ""
-
Synchronization: After the task is submitted, it waits for the return result of the task in situ, and does not do anything during the waiting process (dry etc.) The feeling at the program level is stuck
-
Asynchronous: After the task is submitted, it does not wait for the return result of the task, and directly does other things
-
-
- "" "The running state of the program described" ""
-
- Blocking: blocking state
- Non-blocking: ready state, running state
Ideal state: we should let our code always switch between ready state and running state
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 create an object the p-process = (target = Task, args = ( ' Jason ' ,)) # container type even if there is only one element recommend use commas to separate # 2 open process p.start () # Tell the operating system to help you create a process to asynchronous Print ( ' primary ' ) # inherited second way to class from multiprocessing Import Process Import Time class MyProcess (Process): DEF RUN (Self): Print ( ' the Hello bf Girl ' ) time.sleep ( 1 ) print ( ' get out! ' ) if __name__ == ' __main__ ' : p = MyProcess () p.start () print ( ' Master ' )
- The creation process is to apply for a piece of memory space in memory and throw the code that needs to be run into it.
- A process corresponds to a separate memory space in memory
- Multiple processes correspond to multiple independent memory spaces in memory
- The data between processes cannot be directly interacted with by default.If you want to interact, you can use third-party tools and modules.
join method
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 () # Just tell the operating system to create a process # # time.sleep (50000000000000000000) # # p.join () # The main process waits for the subprocess p to finish running Then continue to execute # p1.join () # p2.join () # p3.join () start_time = time.time () p_list = [] for i in range (1, 4 ): p = Process (target = task, args = ( ' Subprocess % s ' % i, i)) p.start () p_list.append (p) for p in p_list: p.join () print('主', time.time() - start_time)
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)