Categories
Uncategorized

Thread the ins and outs, you know?

Some recent troubles process, frown all day, visiting memory when a bit absent-minded.

Memory is a discerning eye, straight to the point and asked:? “Process ah, recently encountered a problem I see what you are feeling a bit low recently, so what’s the problem you say it directly Well, let me help you with Huoer to think of a way . “

Process sighed and said: “? Alas, not to say that the recent frequency single-core CPU bottleneck humans have yet to make up for lack of a multi-core processor performance of single-core, our CPU does not have to upgrade to a quad-core thing”

“Yes ah, this is a good thing ah, now up to four parallel processing process, the efficiency is higher than ever before, this is not right?” Asked the puzzled memory.

“Nice, but every time I run on CPU time, could not help but think about it, if the single-core frequency does not increase my total run time is not or no change? After the application is growing , eat more CPU resources, such as those the big game process, requires a lot of computing in a short time, barely holding on monocytes how to do. after the talk, to talk about myself, I would like to be able to run to completion as soon as possible, go to bed earlier ah. “

tobe Note: Run time is very obvious single process is smaller, but the main emphasis here is process CPU time.

Memory nodded, agreed and said:.. “The question I was not thinking, multi-core processors on a single process does not friendly and Nazan need to find ways so that you can simultaneously use several core a time but I can not think of any good way, or join in the discussions under the bar. “

At the seminar, a memory to tell us the problems encountered in the process now.

“How a parallel process?” Process Scheduler first asks: “I can not put a process on the four cores it, so not only pointless, but also hinder the execution of other processes.”

Operating system well-informed, said:. “The process once on a few core run is certainly not possible, I think, our goal is actually to make multiple cores do not conflict with helping to run a process that thing we have to put the process of “open” and then put on a few core. “

OS spoke, he drew a diagram:

“Look, if that fun1 and fun2 these two functions unrelated, we can make two nuclear execute them simultaneously, this is not to be done in parallel yet?”

“Do you mean to say that a process is split into several process?”

OS shook his head: “No split into multiple processes, the process of switching costs too much, say, ripped out these functions, they share a single address space, inherently capable of data sharing, if split into the process, we We have to consider the problem of communication between processes, that much trouble. However, in order to distinguish with the process, called them “thread (thread)” it. “

Process surprised, we should split itself into threads? That it is not gone? Quickly asked: “Then I would not be no room for the existence of?”

Process Scheduler also alarmed: “If not the process, I is not have to be retired?”

Operating system quickly explained: You misunderstood, I want to open, and the process flow is executed, the process is not included resource ownership and execution stream it, or ownership of resources to the control by the process, the flow of execution to give a few threads ,like this:

tobe Note: In the process model, the process has control over the memory, I / O channels, I / O devices and resources such as files, called “resource ownership.” “Execution flow” can be seen as the process of the implementation process on the CPU (that is intuitive high-level language statements).

Process suddenly realized: “That I’m still in control of those resources, those threads is equivalent to help me work a little brother?”

“Yes, and from this point of view, you are still a single-threaded process itself.”

After listening for so long, RAM to ask a question: “? The process of creating, I want to save the process of PCB, that in order to create a thread, I is not have to create a TCB (Thread Control Block)”

“Of course, the information needed to switch threads must exist TCB inside. But do not worry, TCB is much smaller than the PCB, so the thread switch will switch much faster than the process.”

After listening to everyone, we have felt “thread” of this model the perfect solution to the current problem, and said:. “Or we can now add threading model in the operating system in it, to solve the problem as soon as possible the process of”

But the operating system undaunted, said: “Our threading model is just a hypothetical, hastily add to the mix, it may be a problem, a system crash may bad, or to give priority to stabilize this model … but still have to try, or we create a thread library, by a user-level applications – thread scheduler to manage these threads of it. “

Process understand the question: “But this is the case, I was assigned on a single core, ah, even multi-threaded, and can only run on a single core Besides, if there is a thread is blocked in. your opinion, are blocking the whole process, and that other threads, even if the ready state, not get CPU resources. “

Operating system carefully to the next, he said: “No way, the user-level threads does have two drawbacks, since the kernel compared to implement threads, user-level threads have his benefits – I do not need to thread switching state transition (state from the user to kernel mode), small overhead, in addition, the threads library can have multiple scheduling algorithm scheduling algorithm can be tailored for the application. “

tobe Note: There is a solution to thread blocking scheme called jacketing, he could produce a blocking system call is converted into a non-blocking system calls, for example, do not directly call system-level I / O routines, but to let the thread the calling application-level I / O routines jacket, the jacket routine checks I / O device is busy, if busy, then do not perform I / O operations, instead dispatching other threads, to avoid waiting for I / O devices the process caused by the blockage.


User-level threads quickly put into use, Linux system pthread (POSIX thread) library can be said to be a big success, the operating system has made a major decision – support kernel-level threads.

Kernel-level threads solve the problem of parallel processes, in addition, due to the presence of visible kernel thread, a thread is blocked, and in the same process in the other thread is still able to run.

Parallel issue is resolved, the process said he was very happy.


I hope you gain something after reading my article.

Thank you for reading, we meet again!

Disclaimer: original article without permission is prohibited reprint

Leave a Reply