Processes and threads
Process:
Is a basic concept in Windows system, it contains a program to run the resources needed. A running application is treated as a process in the operating system, and a process can contain one or more threads. Threads are the basic unit used by the operating system to allocate processor time. Multiple threads can execute code simultaneously in a process. The process is relatively independent, a process can not access the data of another process (unless the use of distributed computing), the failure of one process will not affect the operation of other processes, Windows system is to use the process to divide the work into multiple independent areas. A process can be thought of as the basic boundary of a program. Is a running routine of the application program, is a dynamic execution process of the application program.
Thread:
It is the basic unit of execution in a process and the basic unit of CPU time allocated by the operating system. A process can contain several threads, and the first thread executed in the process entrance is regarded as the main thread of the process. In.net applications, the Main() method is used as the entry point, and a Main thread is automatically created when this method is called. Threads are mainly composed of CPU registers, call stacks, and Thread Local Storage (TLS). The CPU register mainly records the state of the current thread, the call stack is mainly used to maintain the memory and data called by the thread, and TLS is mainly used to store the state information of the thread.
The difference between processes and threads
The main difference between processes and threads is that they are different ways of managing operating system resources. Processes have independent address Spaces, a crash of one process in protected mode does not affect other processes, and threads are just different execution paths within a process. Thread has its own stack and local variables, but there is no separate address space between threads, a thread dead is equal to the whole process dead, so multi-process procedures than multithreaded procedures robust, but in the process switch, the cost of resources is larger, the efficiency is poor. However, for concurrent operations that require simultaneous and variable sharing, only threads, not processes, can be used.
- In short, a program has at least one process, and a process has at least one thread.
- The scale of thread partition is smaller than that of process, which makes the concurrency of multithreaded program high.
- In addition, the process has an independent memory unit during execution, while multiple threads share the memory, thus greatly improving the efficiency of the program.
- Threads are different from processes in their execution. Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot execute independently and must depend on the application, which provides multiple thread execution control.
- From a logical point of view, the meaning of multithreading is that multiple parts of an application can be executed simultaneously. However, the operating system does not treat multiple threads as multiple independent applications to achieve process scheduling and management and resource allocation. This is an important difference between a process and a thread.
Sync and Async
Synchronous:
Synchronization is when a function call is made that does not return or continue until the result is received.
Asynchronous:
Asynchrony is the opposite of synchronization, when an asynchronous procedure call is made, the caller can continue to perform subsequent operations without receiving the result. When the call is complete, the caller is generally notified through status, notifications, and callbacks. For asynchronous calls, the return of the call is not controlled by the caller.
The difference between synchronous and asynchronous: After a request is issued, whether to wait for the result before continuing to perform other operations.
Blocking and non-blocking
The concepts of blocking and non-blocking are related to the state of a program (thread) waiting for a message to be notified (whether synchronous or asynchronous). That is to say, blocking and non-blocking are mainly in terms of the state of the program (thread) waiting for a message notification.
Blocking and non-blocking are concerned with the state of the program while it waits for the result of the call (message, return value).
A blocking call means that the current thread is suspended until the result of the call is returned. The calling thread does not return until it gets the result.
A non-blocking call does not block the current thread until the result is not immediately available.
Concurrent parallel
Concurrency: In an operating system, a period of time when several programs are running on the same processor but only one program is running on the processor at any point in time. When there are multiple threads in operation, if the system has only one CPU, it can not really carry out more than one thread at the same time, it can only divide the CPU running time into a number of time segments, and then allocate the time segments to each thread execution, in a period of time thread code running, other threads are suspended. This method is called Concurrent.
Parallel: When the system has more than one CPU, the operation of the thread may be non-concurrent. When one CPU executes a thread, another CPU can execute another thread. The two threads can run simultaneously without occupying CPU resources. This method is called Parallel.
The key to concurrency is that you have the ability to handle multiple tasks, not necessarily all at once. The key to parallelism is your ability to handle multiple tasks at once.