Can you tell the difference between user

This article is a supplement to the previous blog, and aims to complete the unclear "user-level and kernel-level threads". I hope readers can have a better understanding of threads.

Xiaobai is currently learning multi-threaded programming.

There is a lot of information about multithreading on the Internet. Xiaobai quickly understood the basic concepts of threads, but she couldn't figure out the concept of "user-level threads and kernel-level threads." Xiaoming ask.

As for Xiaobai's question, Xiaoming always answers patiently: "These two concepts in threads are really difficult to understand. Let me tell you about user-level threads first."

User-level threads

"Now that you said you've seen the basic concepts of threads, I'll skip this part.

A long time ago, the concept of threads appeared, but operating system vendors cannot directly modify the kernel of the operating system, because for them, stability is the most important. What should I do if I rashly add unverified things to the kernel? So if you want to verify the availability of the thread, you have to think of another way. "

"I know, I know, those researchers have written a function library about threads, and use the function library to implement threads !" Xiaobai proudly said, "I just saw this on the Internet."

"Yes, they put functions such as creating threads and terminating threads in this thread library , and users can call these functions to achieve the required functions." Xiaoming found a piece of paper and wrote a few functions: pthread_creat , pthread_exit , pthread_join , pthread_yield , and then said, "This is a few important functions. You should probably guess the function of these functions, right?"

"Emmmm, let me think, pthread_creat is to create a new thread, pthread_exit is to end the thread, pthread_join , I guess it is ready to run a certain thread, and then add it to the ready queue . I don't know the last function."

"It doesn't matter if you don't know, you will know in a moment." Xiao Ming went on to say: "You know, the thread library we just said is located in user space, the operating system kernel knows nothing about this library , so the From a perspective, it is still managed in the normal way. "

Xiaobai asked: "That is to say, the operating system still only has processes ? Then a multi-threaded process that I write with the thread library can only run on one CPU core at a time?"

Xiao Ming nodded and said, "You're right, this is actually a disadvantage of user-level threads. These threads can only occupy one core, so parallel acceleration cannot be achieved . Moreover, due to the transparency of user threads, the operating system cannot Actively switch threads , in other words, if A and B are two threads of the same process, while A is running, thread B wants to run, and can only wait for A to actively give up the CPU, which is to actively call pthread_yield function."

tobe Note: To the operating system, user-level threads are invisible , also known as transparency .

"Stop, let me think about it," Xiaobai thought quickly about Xiaoming, "is that even if there is a thread library, user-level threads can't do round-robin scheduling like processes?"

"Very correct! It seems that you are very clear about the concept of processes. However, although round-robin scheduling is not possible, user-level threads also have their own benefits-you can customize the scheduling algorithm for your application . After all, when Exit the thread. You have the final say. Just now, because the operating system can only see the existence of the process, if a certain thread is blocked , what do you think will happen? "

"In the eyes of the operating system, a process is blocked, and the entire process will enter a blocking state . Until the blocking operation ends, this process cannot obtain CPU resources. That is equivalent to all threads being blocked ." Small Prideful answer.

"That's right, so if threads are allowed to perform blocking operations, the efficiency of the process will be greatly affected, so in the process, an alternative solution emerges- jacket . The so-called jacket is the conversion of a blocking system call Into a non-blocking system call. "

Xiaobai was surprised and asked, "How can this be done? The call that should be blocked can still become non-blocking?"

Xiaoming replied: "Let me give an example. Instead of calling a system I / O routine directly, I call an application-level I / O jacket routine. The code in this jacket routine will check and determine the I / O Is the O device busy? If it is busy, block the thread in user mode and then transfer control to another thread. Check the I / O device again after a period of time. Like you said, the last thing is Will perform blocking calls, but using a socket can shorten the blocking time. However, in some cases it can be unblocked, depending on the implementation. "

Xiaoming paused for a while and said, "There are probably so many concepts of user-level threads. Let's talk about kernel-level threads next."

Kernel-level threads

"With the pavement of user-level threads, kernel-level threads are much easier to talk about. Now we know that many operating systems already support kernel-level threads. In order to implement threads, the kernel needs to have threads that record all threads in the system Table. When you need to create a new thread, you need to make a system call , and then the operating system to update the thread table. Of course, the traditional process table is also available. If you think about it, if the operating system "see What are the benefits of "seeing" threads? "

Xiaobai replied confidently: "If the operating system kernel knows the existence of threads, it can be like scheduling multiple processes and placing these threads on several CPU cores to achieve actual parallelism ."

"One more thing you didn't say is that if the thread is visible, then if thread A is blocked, the threads belonging to the same process as him will not be blocked . This is the absolute advantage of kernel-level threads."

"Is there any disadvantage in kernel-level threads?"

"Of course, there are disadvantages. If you think about it, let the operating system perform thread scheduling. That means that every time you switch threads, you need to" sink " into the kernel state, and the operating system changes from user mode to kernel mode . Therefore, the cost of kernel-level thread switching is greater than that of user-level threads . There is also a very important point-the thread table is stored in the fixed table space or stack space of the operating system, so the number of kernel-level threads is limited and the scalability is not as good as user-level threads. "

"Kernel-level threads are just a little bit like this. I'll leave you a picture at the end. If you can understand it, it means that you understand today's concepts."

Two-threaded model

Xiaobai said proudly: "Of course I understand, thank you Xiaoming!"

I hope you have learned something after reading my article.

Thank you for reading, we will have a period later!

Disclaimer: Original article, unauthorized reproduction is prohibited