[Turn] understand the process thread, synchronous asynchronous, non-blocking blocking concurrent parallel

A basic concept

1 Processes and Threads

Process (Process):

The basic concept is a Windows system, it contains a resource needed to run the program. A running application is seen as a process in the operating system, the process may include one or more threads . A thread is the basic unit of the operating system allocates processor time, in the process can have multiple threads executing code simultaneously. Between processes are relatively independent, a process can not access the data of another process (unless the use of distributed computing), failure of a process that is running will not affect operation of other processes, Windows systems is the use of the process of the work is divided into more than separate areas. The process can be understood as a basic program of the border. It is running a routine application of a dynamic process of execution of the application.

Thread (Thread):

Is a basic execution unit in the process, is the basic unit of the operating system allocates CPU time, a process can contain several threads, the first thread entry process performed is regarded as the main thread of this process. In .NET applications, are based on Main () method as the entry, the system automatically creates a main thread when calling this method. Thread mainly by the CPU registers, call stacks, and thread local memory (Thread Local Storage, TLS) thereof. The main CPU register state record currently executing thread, the call stack is used to maintain that thread to the memory and data, TLS is mainly used for status information is stored thread.

The difference between processes and threads

The main difference between processes and threads is that they are different operating system resource management. Processes have separate address space, after a process crashes, will not have an impact on other processes in protected mode, and the thread is just a process of different execution paths. Thread has its own stack and local variables, but there is no separate address spaces between the threads, a thread to die is tantamount to the whole process dies, so the program than multi-process multi-threaded programs robust, but in the process of switching, consuming greater resource efficiency to be worse. But for some of the requirements at the same time and have to share the concurrent operation of certain variables, only with thread, the process can not be used.

  1. In short, there is at least one program a process, a process has at least one thread.
  2. Division of thread scales smaller than the process, so that multithreaded programs concurrency high.
  3. In addition, the process has a separate memory unit in the implementation process, and multiple threads of shared memory, thereby greatly enhancing the efficiency of the program.
  4. Threads in the implementation process and the process is different. Each entry has a separate thread running, sequences and procedures performed in the order outlet. But the thread is not able to execute independently, it must exist according to the application, providing multiple execution threads controlled by the application.
  5. From a logical standpoint, meaning that a multi-threaded application program execution section may perform a plurality of simultaneously. But the operating system will not be seen as multiple threads multiple independent applications to achieve scheduling and management and resource allocation processes. This is an important difference between processes and threads.

Synchronizing (Sync) and asynchronous (the Async)

Synchronize:

The so-called synchronization is issued when a function call, until no results, the call will not return or continue to follow up.

In simple terms, the synchronization is a must do one thing, such as a front done in order to do the next thing.

eg: Form submission B / S mode, the specific process is: client submits a request -> Waiting for server processing -> returns processed, in this process the client (browser) can not do anything else.

asynchronous:

Asynchronous and synchronous contrast, when an asynchronous procedure call is issued, until the caller was not a result, subsequent operations can proceed. When the call is completed, usually informs the caller through the state, notification and callback . For asynchronous calls, calls not returned by the caller control.

For three ways to inform the caller, as follows:

  • Status: that of monitoring the state of the caller (polling), the caller needs to check again at regular intervals, would be very inefficient.
  • Notification: When the caller is executed, the notification informing the caller, without consuming too much performance.
  • Callback: notification Similarly, when the callee execution is completed, it will call the callback function provided by the caller.

eg: B / S mode ajax request, the specific process is: the client sends ajax request -> server-side processing -> processed to perform client callbacks, after requesting client (browser), you can still do other thing.

Synchronous and asynchronous difference:

In conclusion, the difference between synchronous and asynchronous: After the request is made, whether to wait for the results in order to continue to perform other operations.

3 blocking and non-blocking

Blocking and non-blocking state when these two concepts and procedures (thread) to wait for message notification (does not matter synchronous or asynchronous) related. That is the main blocking and non-blocking state when the angle of the program (thread) to wait for the message notification.

Blocking and non-blocking concern is the state of the program while waiting for the result of the call (the message, the return value) of.

Blocking call refers to the results before the call returns, the current thread is suspended. Only the calling thread does not return until after the result.

Before non-blocking call refers not get the results immediately, the call does not block the current thread.

4 concurrent parallel

Concurrency:

In the operating system, refers to a time period has started several programs are in the running to run between finished and these programs are running on the same processor, but any one point in time there is only one program running on the processor. When more than one thread to each thread in operation, if the system is only one CPU, then it simply can not be true at the same time more than one thread, it can only CPU time is divided into several periods, then the period of time allocated perform, in a period of the code the thread is running, the other thread is in a suspended state. This way we call concurrent (Concurrent).

parallel:

When the system has more than one CPU, then it is possible to operate the thread non-concurrent. When a CPU executes a thread, the other CPU can execute another thread, two threads and do not seize the CPU resources, may be performed simultaneously, we call such a manner parallel (Parallel)

Concurrency and parallelism difference:

You eat half a meal, phone, and you only have to pick up after eating, which means you do not support concurrent not support parallel.
You eat half a meal, phone, and you pick up the phone stopped, then continued after a meal, it shows your support for concurrency.
You eat half a meal, phone, and your phone while eating side, it shows your support parallel.

The key is you have the ability to concurrently handle multiple tasks, not necessarily at the same time. Parallel key is you have the ability to handle multiple tasks.

So I think they are the most crucial point is: whether it is at the same time .


Two asynchronous and multi-threaded difference? (Principle articles)

1 asynchronous and multi-threaded What is the difference?

In fact, the purpose is asynchronous, and multi-threading is a method to achieve this purpose. Asynchronous mean, after A initiates an action (generally more time-consuming operation, without time-consuming operation is not necessary the asynchronous), we continue to deal with its own self-serving thing, do not dry waiting for this time-consuming operation returns.

Similarities and differences over two threads and asynchronous operations

Both multithreading and asynchronous operations can achieve the purpose of avoiding calling thread is blocked, thereby improving the responsiveness of the software. Sometimes we even think multithreading and asynchronous operations are equivalent concepts. However, multi-threading and asynchronous operations still there are some differences. These timing differences caused by the indiscriminate use of multi-threading and asynchronous operation of.

3 essentially asynchronous operation

All programs will eventually be executed by the computer hardware, so in order to better understand the nature of asynchronous operations, we need to look at its hardware is based. Familiar with computer hardware DMA friends certainly no stranger to the word, hard disk, CD-ROM technical specifications have clear indicators DMA mode, in fact, network card, sound card, graphics card is also a DMA function. DMA is direct memory access means, that is, the hardware has a DMA function of time and memory in the exchange of data may not consume CPU resources. As long as the CPU sends a command at the initiation of data transmission, began their own hardware and memory to exchange data, after the transfer is complete hardware triggers an interrupt to notify the operation is completed. These need not consume CPU time I / O operations hardware base is the asynchronous operation. Even in a single process such as DOS (process concept and wireless) system also may initiate asynchronous DMA operation.

4 threads of nature

Thread is not a function of computer hardware, but a logical function of the operating system, the thread is essentially the process of the code for a run concurrently, so the thread needs to invest operating system CPU resources to run and scheduling.

5 advantages and disadvantages of asynchronous operation

Because asynchronous operation without additional threads burden, and uses a callback approach to treatment, in well-designed, the handler may not have to use shared variables (if not completely do not, at least you can reduce the number of shared variables), reduced dead lock possible. Of course, the asynchronous operation is not perfect. The higher the complexity of writing asynchronous operation, the program mainly used callback way to deal with some way of thinking of ordinary people entering and difficult to debug.

The advantages and disadvantages over six threads

  • Advantages: handler thread is still the order of execution, in line with ordinary people's habits of mind, so programming is simple.
  • Cons: using threads (abuse) additional burden of context switching systems will, and shared variables between threads might lead to deadlock.

Asynchronous and multi-threaded, from the dialectical relationship point of view, asynchronous and multi-threading is not an equal relationship. The purpose is asynchronous, multi-threaded but we have a means to achieve asynchronous.
What is Asynchronous: Asynchronous is returned when a call request is sent to the caller, but the caller without waiting for their results. Asynchronous multi-threading technology can be used or another process is to be handled.


Three asynchronous, multi-threaded and parallel difference? (Story Part)

Non-professionals, to use non-professional language interpreter at it, metaphor is not apt, but probably that a meaning,

Listen to me tell a story:

That was 10 years ago, not years 12306, you can only buy a ticket to the train station to buy. Because we all have New Year back home, still want to wait, the train station is only one window is only so much headache ah. More troublesome is discharged to the window of the man, all kinds of demanding, not expensive, not at night, not with the conductor standing tickets ...... all kinds of ink, the people behind the more anxious, one angry, curse father BS-ing.

Now assume that the entire city is only one train, a conductor, need to think about one minute and then decide to buy a train ticket which conductor after consulting each passenger.

  1. Asynchronous: After consulting people buying tickets, need to think about one minute, immediately step aside, but do not line up again, when you can think clearly talk to the conductor immediately go buy a ticket. In thinking of the person standing next to the time, back then people rushed to buy up. This time the team is very fast moving, without obstruction, to maximize the efficiency of the conductor.

  2. Multithreading: n Station open windows (but still only one ticket), while the outside of row n teams, after the conductor answer Question question, horse stand up the next window, and then continue to rotate to the next window .... . people decide which window is good, the conductor flew past bought for him. This time the passengers is relatively simple, but if the team was thinking for a long time tangled behind the people on the tragedy.

  3. Parallel: Copies n train station, at the same time selling tickets, tickets greatly enhanced. We can also train station which few people that went to buy a ticket.

Visible: In just a train station, and only a conductor of cases, sold a then sell will lead to a waste of resources, inefficiency, the team stuck, it is difficult to move forward. 1,2-optimized way to solve all of the team does not move, the problem of low ticket sales. But increased the train station, increase the window, it is a good way to increase the conductor.

in conclusion:

  1. In fact, asynchronous and multi-threaded efficiency almost, but not many open windows, for example three, while there are a lot of people are going to spend 5 minutes instead of one minute to go when tangled, multi-threaded efficiency is actually lower than asynchronous, because the conductor or normally encountered three times while stuck in the tangle that can not queue for tickets.

  2. These two concepts used to contrast a bit inappropriate, because they are not a concept, or for the purpose of multi-threaded asynchronous, multi-threaded asynchronous should be a means to achieve. Asynchronous synchronization should talk to compare my son.

  3. Multithreading is relatively simple, but requires an additional window, increased costs, and the conductor was the worst case which is similar to apache php, javascript and relationships at node.js, is a multi-threaded, but is blocked, the other is a single-threaded asynchronous non-blocking of. php program more in line with conventional thinking, but more cost memory, node.js non-blocking, with fewer resources will be able to accomplish the same task, but the programming confusing.

  4. In parallel, while the similar use of each core of a multicore cpu to calculate. Concurrent be divided into pseudo complicated, really complicated. The former, for example, concurrent single-core processor, which refers to the concurrent development of multicore processors.

  5. The ultimate way to parallel computing, and asynchronous computing at each cpu, so you can take full advantage of each core. But the programming requirements are too high is too high, if not intensive computing, such as large finite element method (use more concurrent), or access the server simultaneously process thousands (or use more asynchronous multi-threading), or honestly the traditional way, after all, the amount of computation routine procedures of hardware right now, the problem is not large.


Four blocking non-blocking asynchronous synchronization difference? (Story Part)

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

Synchronous / asynchronous message notification mechanism is concerned, and blocking / non-blocking concern is the state of the program (thread) to wait for message notification.

Xiao Ming to download files analogy, these two focus groups to illustrate this concept again, hoping to promote a better understanding of everyone.

  1. Synchronous blocking: Xiao Ming has been staring download progress bar to 100% of the time it is completed.
    Synchronization is reflected in: Wait for the download is complete notification;
    blocking reflected in: Wait for the download to complete the notification process, can not do other processing tasks;

  2. Non-blocking synchronization: Xiao Ming after the submission of a download task to do otherwise, every once in a while go to glance at the progress bar, saw a 100% complete.
    Synchronization is reflected in: Wait for the download is complete notification, but you want;
    non-blocking reflected in: Wait for the download to complete the notification process, a task to do otherwise, but occasionally glance at the progress bar; [Xiaoming need to switch between tasks Follow the download progress]

  3. Asynchronous blocking: Xiao Ming has changed the download completion notification function of the software, the download is complete on the "ding" sound. But Xiao Ming still been waiting for "ding" sound (looks silly, does not it).
    Asynchronous reflected in: the download is complete "ding" sound notification;
    blocking reflected in: Wait for the download to complete the "ding" sound notification process, can not do other processing tasks;

  4. Asynchronous non-blocking: still download software that will "ding" sound, and Xiao Ming after the submission of download tasks to do otherwise, to hear the "ding" sound to know complete.
    Asynchronous reflected in: the download is complete "ding" sound notification;
    non-blocking reflected in: Wait for the download to complete the "ding" sound notification process, the task to do otherwise, and only needs to receive the "ding" sound to notify; [processing software download task, Xiao Ming to handle other tasks, without attention to progress, simply receiving software "ding" sound notifications to]

In other words, synchronous / asynchronous is the way (mechanism) "Download Complete Message" notification, and blocking / non-blocking is in a wait state "download completion message" notification process (can not do other tasks), in different scenario, synchronous / asynchronous, four combinations of blocking / non-blocking are applied.

So, to sum up, synchronous and asynchronous mechanisms are only concerned about how the message notification, and blocking and non-blocking concern is the state of waiting for message notification. In other words, in the case synchronized by the person processing the message themselves waiting to see if the message is triggered, and the case was triggered by an asynchronous notification mechanism to deal with those messages, so in an asynchronous mechanism, messages are processed and the trigger mechanism inter will need to bridge a connection:

In Bob's case, this bridge is the software "ding" sound.

Synchronous / asynchronous and blocking / non-blocking

  1. Synchronous blocking form of
    efficiency is the lowest, to take the example above, it is that you concentrate wait for the download is complete, do not do anything else. Actual program: read O_NONBLOCK is not set to the flag fd / write operation;

  2. Blocking asynchronous form of
    asynchronous operations can be blocked to live, but it is not blocked while processing a message, but the message is blocked while waiting for notification. For example, select a function, if passed in the last timeout parameter is NULL, then the event is not a concern if triggered, the program has been blocked in select call this place.

  3. Synchronous non-blocking form
    is actually inefficient, imagine that while you do other things while still do not need to look up the download is complete, if doing something else and observe the completion of the download location as two programs of operations, then this program needs to be between these two different behaviors of switching back and forth, imagine efficiency is low. Many people will write blocking read / write operations, but do not forget you can set O_NONBLOCK flag on fd, so that you can synchronize the operation becomes a non-blocking.

  4. Non-blocking asynchronous form of
    more efficient because you wait for the download is complete (waiters) thing, and you notice is the computer (the message trigger mechanism) things, the program does not switch back and forth in two different operations.

Guess you like

Origin www.cnblogs.com/leomessi10/p/11882303.html