30 min To read process threads, synchronous asynchronous, blocking non-blocking, concurrency parallel

Source: Internet
Author: User
Tags ming ticket

Basic Concepts1Processes and Threads

Process ( Process):

is a a basic concept in Windows systems that contains the resources required to run a program. A running application is considered a process in the operating system, and a process can include one or more threads . A thread is the basic unit in which the operating system allocates processor time, and multiple threads can execute code concurrently in a process. Processes are relatively independent, and one process cannot access the data of another process (unless distributed computing is used), and the failure of one process to run does not affect the operation of other processes, andWindows systems use processes to divide the work into separate areas. A process can be understood as a basic boundary of a program. is a running routine for an application that is a dynamic execution of the application.

thread ( Thread):

is the basic execution unit in the process, which is the operating system assignment the basic unit of CPU time, a process can contain several threads, and the first thread that executes at the process gate is considered the main course of the process. in. NET applications, the main () method is used as the portal, and when this method is called, the system automatically creates a main thread. Threads are primarily composed of CPU registers, call stacks, and thread local memory (thread locallyStorage,TLS). the CPU registers primarily record the state of the currently executing thread, and the call stack is primarily used to maintain the memory and data that the thread invokes,andTLS is primarily used to hold the thread's state information.

The difference between a process and a thread

The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process dead, so the multi-process program is more robust than multithreaded programs, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.

1) in short, a program has at least one process , and a process has at least one thread .

2) The thread partition scale is smaller than the process, which makes the multi-thread procedure high concurrency.

3) in addition, the process has a separate memory unit during execution, while multiple threads share memory, which greatly improves the efficiency of the program operation.

4) threads are still different from the process in the execution process. Each separate thread has a program run entry, sequence of sequence execution, and exit of the program. However, threads cannot be executed independently, and must be dependent on the application, which provides multiple threads of execution control.

5) from a logical point of view, the meaning of multithreading is that in an application, there are multiple execution parts that can be executed concurrently. However, the operating system does not consider multiple threads as separate applications to implement scheduling and management of processes and resource allocation. This is the important difference between processes and threads.

2Synchronization (Sync) and asynchronous (Async)

Synchronous:

The so-called synchronization, that is, when a function call is issued, the call does not return or continue to perform subsequent operations until the result is obtained.

Simply put, synchronization is one thing you have to do, and you can do the next thing before you finish it.

For example: b/S mode of the form submission, the specific process is: Client submission request , wait for the server processing , processing completed return, In this process the client (browser) cannot do other things.

Asynchronous:

Asynchronous versus synchronous, when an asynchronous procedure call is issued, the caller can proceed with the subsequent operation until the result has been obtained. When this call is complete, the caller is generally notified by state, notification, and callback . For asynchronous calls, the return of the call is not controlled by the caller.

For the three ways to notify callers, the following is the case:

State

That is, listening to the status of the callee (polling), the caller needs to check every time, the efficiency will be very low.

Notice

When the callee executes, a notification informs the caller that there is no need to consume too much performance.

Callback

Similar to notifications, when the callee executes, it invokes the callback function provided by the caller.

For example: the Ajax request in B/S mode , the process is: the client issues Ajax request --Server processing - after processing completes the client callback, you can still do other things after the client (browser) makes the request.

The difference between synchronous and asynchronous:

In summary, the difference between synchronous and asynchronous: After a request is issued, you need to wait for the result before you can continue with other operations.

3blocking and non-blocking

blocking and non-blocking both concepts and programs (threads) waiting for message notification (It doesn't matter if it's synchronous or asynchronous ) the state of the time. This means that blocking and non-blocking are primarily the state perspective of the program (thread) waiting for a message notification.

Blocking and non-blocking concerns the state of the program when it waits for the call result (message, return value) .

A blocking call means that the current thread is suspended until the call results are returned. The calling thread will not return until the result is obtained.

A non-blocking call means that the call does not block the current thread until the result is not immediately available.

4Concurrency Parallel

Concurrency: in the operating system, it means that several programs in a period of time have been started and run to completion, and that the programs are running on the same processor, but only one program is running on the processor at any one time point. , If the system has only one CPU, It's impossible to actually do more than one thread at a time, it can only put cpu This way we call it concurrency (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, the other CPU can execute another thread, and two threads do not preempt CPU Resources, which can be done simultaneously, This way we call it parallelism (Parallel)

Differences in concurrency and parallelism:

You eat half of the meal, the phone is coming, you have to eat until after the end of the answer, which means that you do not support concurrency and do not support parallelism.

You eat half of the meal, the phone came, you stopped to pick up the phone, and then continue to eat, which shows that you support concurrency.

You eat half of the meal, the phone is coming, you are on the phone while eating, it shows that you support parallel.

The key to concurrency is that you have the ability to handle multiple tasks, not necessarily at the same time. The key to parallelism is that you have the ability to handle multiple tasks at the same time.

So I think the key point is: "At the same time."

two asynchronous and multithreading differences? (principle article)1What is the difference between async and multithreading?

In fact, asynchronous is the purpose, and multithreading is the way to achieve this. Asynchronous is to say,a after initiating an operation (generally is more time-consuming operations, if not time-consuming operation is not necessary to asynchronous), you can continue to self-processing its own things, do not wait for this time-consuming operation returned.

2the similarities and differences between multithreading and asynchronous operations

Both multithreading and asynchronous operations can achieve the goal of avoiding calling thread blocking, thus improving the responsiveness of the software. Even sometimes we think that multithreading and asynchronous operations are the same concept. However, there are some differences between multithreading and asynchronous operations. These differences create the difference between the timing of using multithreading and asynchronous operations.

3the nature of asynchronous operations

All programs will eventually be executed by the computer hardware, so in order to better understand the nature of the asynchronous operation, we need to understand its hardware base. a friend familiar with computer hardware is sure toDmaThis word is not strange, the hard drive, optical drive technical specifications have a clearDMAmode indicators, in fact, the network card, sound card, video card also hasDMAfunction. DMAis the meaning of direct memory access, that is to say, havingDMAfunction of the hardware in the data exchange with the memory can not consumeCPUResources. JustCPUwhen a command is sent at the time of data transfer, the hardware begins to exchange data between itself and the memory, and after the transfer is complete, the hardware triggers an interrupt to notify the operation to complete. These need not be consumedCPUTime ofI/OThe operation is the hardware foundation of the asynchronous operation. So even inDOSsuch a single process (and the wireless Path concept) system can also initiate asynchronousDMAoperation.

4the nature of threads

A thread is not a function of a computer's hardware, but rather a logical function provided by the operating system, which is essentially a piece of code that runs concurrently in a process, so the thread requires an operating system input CPU resources to run and dispatch.

5advantages and disadvantages of asynchronous operations

because asynchronous operations do not require additional thread burdens and are handled in a callback manner, the processing function does not have to use shared variables (even if it cannot be completely unused), at least to reduce number of shared variables), reducing the likelihood of deadlocks. Of course, asynchronous operations are not flawless. Writing asynchronous operations is a high degree of complexity, the program mainly uses callback method to handle, and ordinary people's way of thinking some first, and difficult to debug.

6advantages and disadvantages of multi-threading

The advantages of multithreading are obvious, the handlers in the thread are still sequential execution, in line with the normal thinking habits, so programming is simple. However, the disadvantage of multithreading is equally obvious, the use of threads (misuse) will bring the burden of context switching to the system. Also, shared variables between threads can cause deadlocks to occur.

Asynchronous and multi-threaded , from the dialectical relationship , asynchronous and multi-threaded and from time an equivalent relationship , Asynchronous is the purpose , Multithreading is just one of the ways we implement Asynchrony . What is asynchronous : asynchronous is when a call request is sent to the callee , the caller does not have to wait for the result to return . implementing Asynchrony can be accomplished using multithreading techniques or handing over to another process

three What is the difference between async, multithreading, and parallelism? (story)

Non-professionals, in the non-professional language to explain it, the analogy is not appropriate, but probably the meaning,

Let me tell you a story first:

that was still ten years ago, there is no 12306 era, we can only buy tickets to the railway station. Because everyone wants to go home for the new year, they do not want to wait, there is only one station, window only so much, headache ah. More headache is, row to the window of the person, a variety of picky, not expensive, not at night, do not arrangement ... with the conductor all kinds of ink, behind the people more anxious, a indignation, scold father dozens.

now assume that the entire city is only 1 trains,1 Conductors, each passenger consultation with the conductor after the need to think about 1 minutes before deciding which train to buy tickets.

1. Async: After the purchase of the person consultation, need to think about 1 minutes, immediately step aside, but do not have to re-queue, when thinking clearly can immediately go to the ticket to buy tickets. While the man was standing next to him, the man in the back hurried up and went on to buy. At this time the team is moving quickly, without blocking, the maximum efficiency of the conductor.

2. Multi-thread: The station opens n window (but still only one person sells tickets), outside at the same time row n teams, the conductor answers the question of the consultant, immediately go to the next window, and then continue to rotate to the next window ..... At which window the man decided, and the conductor flew over to buy him. This time the passengers are relatively simple, but in case the team is thinking about a half-day tangle, the people behind the tragedy.

3. Parallel: Copy n stations, while selling tickets, the ability to buy tickets greatly enhanced. Everyone can also which train station less people, go to that ticket.

visible: In only one railway station, and only a conductor of the case, sold a re-sell one will lead to waste of resources, inefficient, the team stuck dead, it is difficult to move forward. the method of optimization solves the problem that the troop does not move, the ticket rate is low. But it is a good idea to increase the train station, add the window and increase the conductor.

Conclusion:

1. asynchronous and multi-threaded is actually the same efficiency, but the window is not open for example 3 , while many people are going to spend 5 minutes, rather than 1 Minutes to tangle, multithreading efficiency is less than asynchronous, because the conductor still often encountered 3 teams at the same time stuck in the tangle can not buy tickets.

2. The comparison of these 2 Concepts is also a bit inappropriate, because they are not a concept, multi-threaded purpose or to achieve asynchronous, multithreading should be an implementation of asynchronous means. Async should be compared to synchronous comparison.

3. multithreading is relatively simple, but need additional windows, increase costs, and the conductor is more tired this is similar to Apache php, and node. js Under JavaScript , one is multi-threaded, but it is blocked, and the other one is single-threaded asynchronous non-blocking. PHP 's scheme is more consistent with conventional thinking, but compared to the cost of memory,node. js non-blocking, with less resources to complete the same task, but programming more difficult.

4. parallel, similar to the simultaneous use of multi-core CPU of the various cores to compute. Concurrency can be divided into pseudo concurrency, true concurrency. The former such as the concurrency of a single-core processor, which refers to the concurrency of multi-core processors.

5. The ultimate approach is to compute in parallel and asynchronously at each CPU so that each of your cores is fully utilized. But the programming requirements too high, if not intensive computing, such as large-scale finite element computing (more concurrent), or the server processing thousands of access (more asynchronous or multi-threaded), or honest use of traditional methods, after all, the normal program of computing for the current hardware, the problem is not big.

Four What is the difference between blocking non-blocking and synchronous asynchrony? (story)

Understanding synchronous blocking, synchronous non-blocking, asynchronous blocking, asynchronous blocking, asynchronous non-blocking

Synchronous / asynchronous is concerned with the mechanism of message notification, while blocking / non-blocking is concerned with the state of the program (thread) waiting for a message notification.

Take xiaoming to download the file for example, from these two concerns to explain the two sets of concepts again, hoping to better promote the understanding of everyone.

synchronization blocking: Xiao Ming has been staring at the download progress bar, to 100% the time to complete.

Synchronization is reflected in: Waiting for the download to complete the notification;

Blocking is reflected in: Waiting for the download to complete the notification process, can not do other tasks processing;

synchronous non-blocking: After the download task Xiao Ming to do other, every time after a glance to the progress bar, see 100% on completion.

Synchronization is reflected in: Waiting for the download to complete the notification, but in;

Non-blocking is reflected in: Waiting for the download to complete the notification process, to do other tasks, but occasionally will take a glance at the progress bar; "Xiaoming must switch between two tasks and follow the download Progress"

asynchronous blocking: Xiao Ming changed a download to complete the notification function of the software, the download is done "ding" a sound. But Xiaoming still waits for a "ding" sound (looks silly, doesn't it).

asynchronous reflected in: Download complete "Ding" a notice;

blocking is reflected in: Waiting for the download to complete the "Ding" notification process, can not do other tasks to deal with;

asynchronous non-blocking: still is that will "ding" a sound of the download software, xiaoming submitted the download task to go to do other, hear "ding" a sound will know to complete.

asynchronous reflected in: Download complete "Ding" a notice;

non-blocking reflected in: Waiting for the download to complete the "ding" a notification process, to do other tasks, just to receive "ding" sound notification can be; "Software processing download task, Xiao Ming processing other tasks, do not need to pay attention to progress, just receive software" Ding "sound notification, you can"

that is, synchronous / asynchronous is the "Download complete message" notification (mechanism), while blocking / non-blocking is waiting for the "Download complete message" Notification process status (can do other tasks), in different scenarios, synchronization / asynchronous, blocking / non-blocking four combinations are applied.

So, in summary, synchronous and asynchronous are just the mechanisms of concern about how messages are notified, while blocking and non-blocking are concerned with the state of waiting for a message notification. In other words, in the case of synchronization, it is up to the processing message to wait for the message to be triggered, while the asynchronous case is triggered by the trigger mechanism to notify the processing of the message, so in the asynchronous mechanism, the processing of the message and the trigger mechanism between the need for a bridge between the connection:

in Xiao Ming's example, this bridge is the software "ding" sound.

Synchronous / asynchronous vs. blocking / nonblocking #

1 Synchronous blocking form

The efficiency is the lowest,

In the example above, you are concentrating on waiting for the download to complete and do nothing else.

in the actual program: is not the fd set o_nonblock flag bit read/write operation;

2 Asynchronous blocking form

An asynchronous operation can be blocked, except that it is not blocked while processing the message, but is blocked while waiting for a message notification.

For example , the Select function, if the last timeout parameter passed in is NULL, then if none of the events of interest is triggered, The program will always block at this select call.

3 Synchronous non-blocking form

is actually inefficient,

Imagine you're doing something else and you need to look up. Download complete No, if you do other things and observe the download completion of the location as a program of two operations, the program needs to switch between the two different behaviors, efficiency can be imagined is low.

Many people write blocking Read/write operations, but don't forget that you can set the o_nonblock flag bit for FD, This allows the synchronization operation to become non-blocking.

4 asynchronous non-blocking form

Higher efficiency,

because waiting for the download to complete is your ( waiting Person ) the thing, and inform you is the computer ( message triggering mechanism ) , the program does not switch back and forth between two different operations.

At this point, about the process thread, synchronous asynchronous, blocking non-blocking, concurrency parallel has been said almost, there is a bad place to ask the big guy. Also thanks to the online gangster's article helped me understand these concepts.

Reference connection:

https://www.zhihu.com/question/33515481/answer/58849148

Https://www.cnblogs.com/dream844/archive/2012/06/12/2546083.html

Https://www.jianshu.com/p/aed6067eeac9

https://www.zhihu.com/question/28550867/answer/55357128

Friendly Tips Mhq_martin Blog Park address:http://www.cnblogs.com/mhq-martin/This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

30 min To read process threads, synchronous asynchronous, blocking non-blocking, concurrency parallel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.