python process / thread / HS

1. talk of multi-process and multi-threaded HS understanding of 
the process: program (code) is running a process, not running code
called the program, the process is the smallest unit of allocation of system resources, the process has its own separate
memory space , so the data is not shared between processes, large overhead
thread: the smallest unit scheduled for execution, also called the execution path, can not exist independently, it must
dependency process, a process has at least one thread, called the main thread, and multiple threads shared
memory ( data sharing, shared global variable), thereby greatly enhancing the efficiency of the program.
HS: a user is too lightweight thread scheduling HS entirely controlled by the user,
HS has its own stack and register context when switching to schedule Association, the register context
save and stack to another location, when cut back, context restore a previously saved registers
and the stack, the stack is directly overhead operation switch substantially no core, you may not access the locked global
variable, so very fast context switching
2. What is a multi-threaded competition?
threads are independent, the same process of data sharing, when each line Access data sources
occur when a race condition that is: the data is almost occupied by multiple threads, resulting in data confusion, the so-called
thread safe
? How to solve the problem of multiple threads compete lock
? Benefits lock
ensures that certain critical code (shared data resources) only by a thread from the beginning to
the end of a full brother to perform atomic operations to solve the problem of multi-threaded resource competition under
lock harm?
prevents concurrent execution of multiple threads, a piece of code that contains only single-threaded lock actually
Mode execution, efficiency is greatly decreased by
3. What is the lock? What are the lock?
Lock (Lock) is the object of providing the team python thread control,
mutex
reentrant lock
deadlocks
4. What is a deadlock ?
Some child thread in the competition for resources, both waiting for the other to lift the state of occupation of a part of the resources, the result
who are unwilling to unlock, each waiting to dry, the program can not execute it, this is a deadlock
5.GIL lock, global interpreter lock (only in cpython only)
role: to limit multiple threads execute simultaneously, to ensure that only one thread executes the same time, all
cpython in fact, is a pseudo multi-threaded multi-threaded
so often use python in Hiap Seng technology to replace multi-threading, Hiap Seng is a more lightweight thread
switching process and thread is determined by the system, and HS by our programmers decide for themselves,
but when the next switching module encountered gevent consuming operation will switch the
relationship between the three: there are processes, threads, there Hiap Seng
6. What is thread safe? What is a mutex?
each object has a corresponding tag can be called a "mutex". this tag is used to ensure that at
any one time, only one thread Asks the object.
The same process is multi-threaded shared system resources, multiple threads simultaneously on a target
to operate, a thread operation has not ended, another thread has its operations, leading to
the final result error, and the need add to chant operation object mutex to ensure that each thread of
that object are correct result
? 7. Talk about the following concepts synchronous, asynchronous, non-blocking?
Synchronization: There sequencing between multiple tasks, under an executive order to perform a complete
asynchronous: no order between multiple tasks can be executed at the same time, there are when a task
can go after when necessary result of another task performed simultaneously, which is called the callback?
blocking: If the card is the main caller, the caller will not be able to continue down, that the caller blocked the
non-blocking : If the card is not the master, can continue to perform, that is non-blocking
synchronous asynchronous with respect to multi-task, the blocking non-blocking with respect to the terms of the code
8. What is the process of lecturers and orphaned how to avoid the zombies??
orphaned : the parent process exits, the child process is still running in these sub-processes are orphaned
orphan adoption process will be init process (process 1), by the init process to complete their
state collection
zombie process: use fork to create processes the child, if the child process exits, and the parent
does not call status information acquisition process of the child wait or waitpid, then the child process of
descriptors still stored in the system These processes are zombie process
to avoid the zombie methods:
1. grandson twice with fork child process to complete the process task;
2. wait () function allows the parent process to block
3. Using semaphores, signal handler in the call waitpid , so the parent process without obstruction.
9.python of processes and threads usage scenarios?
multiple processes for the cpu-intensive operations (more cpu command, such as the median number of floating-point operations)
Multithreading in IO-intensive operations from (read and write data manipulation teach more like reptiles)
or 10 parallel threads concurrently? Are concurrent or parallel?
Thread is concurrent, parallel processes are
independent of each process, the system the minimum unit of allocation of resources, the same process all
threads share resources
? 11. parallel and concurrent
parallel: the same time multiple tasks simultaneously run
concurrently: the same time interval multiple tasks are running, but will not agree to a time and
run , there is a case of alternately performed
for parallel library has: multiprocessing
concurrency libraries have: threading
program needs to perform read and write more, requires a lot of requests and replies IO operations tasks,
IO concurrent operation of more intensive
CPU processing large program that uses parallel would be better
distinguish 12.IO intensive and CPU intensive?
IO-intensive: the system works in most cases is read to CPU I / O (HDD / memory) / write
CPU-intensive type: most of the time used for calculation, judgment logic operation of the CPU program called CPU-intensive

Guess you like

Origin www.cnblogs.com/zhaoyang110/p/11355645.html