threads and processes

Difference between process and thread?

(1) A process is an independent unit of resource allocation and scheduling, and a thread is the basic unit of CPU scheduling
(2) The same process can include multiple threads, and threads share the resources of the entire process (registers, stacks, contexts) , a process consists of at least one thread.
(3) The creation of the process calls fork or vfork, and the creation of the thread calls pthread_create. After the process ends, all threads it owns will be destroyed, and the end of the thread will not affect the end of other threads in the same process
(4) Thread It is a two-level light process. The time required for its creation and destruction is much smaller than that of the process. All execution functions in the operating system are completed by creating threads.
(5) Synchronization and mutual exclusion are generally performed during execution in threads. , because they share all the resources of the same process
(6) Threads have their own private attributes TCB, thread id, registers, hardware context, and processes also have their own private attributes process control block PCB, these private attributes are not shared, use to identify a process or a thread flag

blocking and non-blocking

A blocking call means that before the call result returns, the caller will enter a blocking state and wait. It will only return after getting the result.
A non-blocking call means that the function will not block the current thread and return immediately until the result cannot be obtained immediately.
The 5 states a thread/process goes through, created, ready, running, blocked, terminated. The transition conditions of each state are shown in the figure above. There is a blocking state, which means that when a function is called in a thread, an IO request is required, or if competing resources are temporarily unavailable, the operating system will block the thread to avoid wasting CPU resources. , wait until the resource is obtained, and then become the ready state, waiting for the CPU to schedule and run.
write picture description here

Synchronous and Asynchronous

Synchronous: When a synchronous call is issued, the call does not return until the result is obtained.

Asynchronous: After issuing an asynchronous call, the caller does not get the result immediately, the call returns.

The definition of synchronization looks very similar to blocking, but synchronization and blocking are two concepts. When a synchronous call is made, the thread is not necessarily blocked. Although the call does not return, it is still in the running state, and the CPU is probably still executing this process. A piece of code, and if it blocks, it will definitely not run this code in the CPU. This is the difference between synchronization and blocking. Synchronization is definitely possible, blocking is definitely not there.
The definitions of asynchronous and non-blocking are similar. The difference between the two is that asynchronous means that the result will not be returned immediately when the call is made. The thread may be blocked or not blocked. It does not matter. Non-blocking means that when calling, the thread will definitely not enter the blocking state.

There are 4 combinations of the above two groups of concepts.

Synchronous blocking call: the result is not returned, the thread enters the blocking state and waits.

Synchronous non-blocking call: no result is not returned, the thread is not blocked and has been running on the CPU.

Asynchronous blocking call: Go to another thread, let other threads block and wait for the result, and do not block yourself.

Asynchronous non-blocking call: go to another thread, and the other thread has been running until the result is obtained.

parallel

Parallelism means that multiple programs run on multiple CPUs at the same time, and many programs can run at the same time at any one time without interfering with each other.
write picture description here

Concurrency

Concurrency means that multiple programs run on one CPU, and the CPU switches quickly between multiple programs. Microscopically, it does not run at the same time. Only one program is running at any time, but macroscopically, it looks like multiple programs are running at the same time. Because the CPU switching speed is very fast, the time slice is 64ms (switching every 64ms, different operating systems have different times), the human reaction speed is 100ms, you haven't reacted yet, the CPU has already switched several programs.
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326041642&siteId=291194637