1 The origin of the story
Digging gold friends, hello, I am a mu, today May Day, I wish you a happy May Day!
You should see the title to know, today through plain English + graphics to explain to my wife synchronous, asynchronous, blocking, non-blocking, parallel, concurrent understanding. My wife is a questioner who likes to ask questions about computers, even though she is a math teacher, even though she has studied computer programming. But I don’t think she can do anything. Sometimes when I am annoyed with me, I will lose my temper with her. Here, I sincerely apologize to my wife (in person).
Just talk to my wife about it today.
If you are interested, please search “I am MU” on wechat and follow me, a slightly nervous but optimistic programmer ~
Husband, what is blocking, non-blocking, synchronous and asynchronous?
“Husband before I see you every day to see the operating system of the directory of the book there are blocking and non blocking, is what meaning? Is it like we usually take the subway, sometimes the peak of the subway 🚇 was blocked to sit on, sometimes very few people and then not traffic?” . You say say say what mean, is not so ah, sprinkle a jiao.
There is the meaning of this point, but your meaning is only one-sided, let me draw a picture for you, so that you can understand at a glance. “I can never stand a girl acting spoiled.”
“What can you tell from the graph? Can you see what the whole process looks like?” “I asked my wife
“Husband, I saw, see come out”, wife excited excited of say.
“Tell me what you see. Is my drawing so clearly structured? I thought you didn’t understand English well?” I waited expectantly with a smile on my face and I saw a positive look from my wife.
“Husband you this picture of very good look of, use what software to draw of, good clear oh! “He said to me with an adoring look.
“Ho ho ho, that’s what you saw! “, a basin of cold water poured on my head, cold, wife you are insulting my painting; Let me just show you the picture.
Figure 1:
1) Read The user thread is blocked after the user requests a read call.
The kernel has no ready data.
The prepared data is stored in the kernel cache.
kernel copies data to user
The kernel cache copies data to user space.
The copy data is returned to the caller for use
So when a user makes a request, the user thread is blocked until the kernel has sorted out the data and copied it to the user cache. The whole process is: the kernel reads data from disk into the kernel cache; Then copy the data to the user cache; The entire process is blocked and must proceed consistently to get the result before a request is completed.
Summary blocking means that the current thread is suspended until the call returns with the result. The calling thread will wake up to perform subsequent operations only after the result has been obtained.
Figure 2:
The basic logic is the same as blocking, except that it has several more read requests (that is, polling requests). Yeah, non-blocking is basically what happens here.
“That it is how to embody it, the husband can give me a simple example to listen to, your professional terms I am not very understand”, a face confused confused looking at me.
Let me give you an example:
For example: when you take the subway, suddenly there is an aunt in the security gate with the security inspector quarrel, aunt threatened: “today if you don’t give Lao Tze a statement, I didn’t play with you, who also don’t want to go”. Aunt moved a small bench blocked the door, where to sit buckle mobile phone, from time to time to ask that security inspector, how to solve this matter? So this whole process is a non-blocking operation for the auntie. Ask the security inspector for a solution while you’re on your phone. In plain English, while you’re waiting for a solution, you can do something else in between. When the solution comes out, the process is complete.
The last read operation is a synchronous process that requires the thread to wait; The reason is that the kernel is ready to copy the data to the user cache.
Summary Non-blocking means that the call does not block the current thread until the result of the call is returned.
synchronous
Synchronization means that the user initiates a request and does not proceed with the next operation logic until the request result is returned. You have to do one thing at a time. Only when the first thing is done can you do the next thing.
For example, when we go to parks or shopping malls during the epidemic, we need to log in with “Suikang code” for name and ID card authentication. Only after the user receives the response result from the real-name authentication API, can we carry out the subsequent code scanning verification before continuing with the following things.
asynchronous
Asynchronous is the opposite of synchronous, with the main difference being whether to wait for the result set to return. After the user performs a read or write operation, the kernel immediately returns, passing in a callback function (callblack), and then reads and copies the data. The user can process the data immediately (at this point, the data has already been copied to the user’s cache, so there is no need to retrieve the data).
Describe in plain language:
- Blocking/non-blocking: The difference is the current thread/process state of the person making the request. One is that the request stays blocked and the user can’t do anything. One is that the user can do something else during the request and then wait for the result.
- Synchronous/asynchronous: indicates how the user request is completed, or synchronous if it is ultimately completed by the user; Notifying the user of receipt is asynchronous if it is not done by itself but by someone else. Home term difference: Whether copying data from the kernel to empty space is done by the user thread
Husband, what is parallelism and concurrency?
Wife’s problem is more and more interesting, listen to me slowly say, here parallel and concurrent often can cause everyone’s misunderstanding, many people can’t distinguish the difference between the two is where?
In fact, whenever I have done some large-scale projects, such as live broadcasting, e-commerce, online education and so on, the first problem is how to solve concurrency. It is estimated that there are three inevitable aspects of interview: high concurrency, high performance and high availability.
The difference between concurrency and parallelism is quite high: it depends mainly on whether it is simultaneous or not.
Concurrency is when multiple events occur at the same time interval, while parallelism is when multiple events occur at the same time. Implication: Concurrent description of a time period, parallel description of a point in time
Parallelism: When our cup is multi-core, one CPU can execute one thread while another CPU can execute another thread. They do not compete with each other for CPU resources and can execute multiple tasks at the same time.
Concurrency: When multiple tasks exist in the system, the CPU divides the running time into multiple periods and allocates each task to execute. Context is switched back and forth to execute the task according to the time slice.
For example:
Go to the company to eat breakfast in the morning: breakfast has millet congee, steamed stuffed bun, soybean milk, fried noodles, in the specified time, for a while to drink congee, for a while to eat steamed stuffed bun, and then for a while to eat fried noodles, so back and forth switch finished breakfast – concurrency
It is a parallel operation to find your neighbor eating breakfast at the same time as you.
“Oh oh oh, the original is so ah, the concept of this computer is really set on people ah, too difficult to understand, but also good you can help me a white words.” When I saw her, I wanted to say that there are all these concepts in the book. Only when we solve them in the actual project can we really understand them.
The final summary
Blocking/non-blocking: The application waits for message notifications. Blocking The current thread is suspended, and the thread can do nothing but wait for a result to return before it can proceed to the next thing. Non-blocking: If no result is obtained, you can continue to do the following; If it is synchronous and non-blocking, it checks to see if the result has been returned by polling, otherwise it waits for notification and then callback processing.
Synchronous/asynchronous: Synchronous means that the structure must wait for the current request to finish processing before being returned directly. The caller is actively waiting for the result set. Asynchronous: After the user initiates the request, the called directly returns to the structure, waits for the data to be completed, notifies the caller to accept the data processing in the form of callback function or state, so the caller is in a passive position to receive the result set. This is why, in many concurrent cases, people choose to process business logic asynchronously.
This article focuses on the concepts of synchronous, asynchronous, blocking, non-blocking, concurrency, and parallelism, and illustrates what they look like in real life. You may have learned “computer composition principles” and “Computer operating system” in college, so you can look back to understand, after all, no matter the front-end or back-end positions are essential interview questions.
The above is my personal understanding of synchronous/asynchronous/blocking/non-blocking/parallel/concurrency.
reference
Computer Operating System
Finally, please pay attention to my personal public account “I am AMu”. I will update back-end knowledge points and study notes irregularly. Also welcome to contact me directly on the public account, private message or email, we can learn together, progress together.
Ok, I am a mu, a worker who does not want to be eliminated at the age of 30 ⛽️ ⛽️ college.