The process has been discussed before and the thread is now discussed. I'm trying to figure out how to compare the relationship between process and thread with the concrete things in reality.
Give me an example that I think is more appropriate. Comparing the process to a workshop in a factory, there are several production lines in the workshop, but each production line requires different parts, materials and staff. Parts, raw materials and employees are resources that all production lines can use together. Take the line as a thread here. In this way, each production line is only used to control its own production process.
Just like the discussion process, here's a list of the entities that the thread contains.
Program counter
Register
Statck
State
It is not difficult to find that the thread cares only the next moment CPU execution necessary entity, only need to know the PC and other registers information, the current stack pointer, do not care I have no child process ah, my address space where, I opened what file and so on information. Because this information is already known to the CPU.
Why to use Threads
The advantages of using threading have been explained in the modern operating system and can be easily understood. In fact, according to the above, can also guess the answer. There's no need to explain it here.
How threads are implemented
Threads can be implemented in the kernel or in user space. My own understanding is this: the implementation in the kernel is the operating system provides threading support, in the user space implementation is the user's own implementation. It was assumed that the JVM's thread was implemented in user space, but it looked like the Linux platform was called Pthred Library implementation. Here or in-depth study of the implementation of these two threads, for the future work and learning to lay the foundation, perhaps when the use of. Read a few times in the "Modern operating system" in the relevant chapters, but they are still foggy, not very clear, this time on a good understanding (not necessarily understand, there is no contact with the user space implementation of the thread, perhaps I ignorant).
User space Implementation Thread
In this case, the kernel does not know the existence of the thread. When a thread in a process finishes executing, it does not require a system call (Trap) and does not require a context exchange, so the thread switches very quickly.
But in the event of an I/O outage, the kernel must be processed. So the problem is that the kernel locks the process that the current thread is in because the kernel does not know the thread exists and only knows the process. It's a bit of a hassle to solve this. A solution is presented in modern operating system, in which some UNIX versions have a select system call that can determine if I/O operations will block. I/O operations are not performed if blocking. In this way run-time system (the English version of the textbook, many of the terms will not be translated or translated, embarrassing) will know whether to perform I/O, or other processes to execute the process.
Kernel implementation Thread
Kernel implementation threads and processes differ little. However, when the thread is destroyed, the kernel does not destroy the data structure of the threads, only marking that the thread is not executable. Thus, when a new thread is created, overwriting the data of the non-executable thread greatly improves the efficiency.
There is also the question of how the child process needs to replicate all the threads of the parent process or individual threads when a multithreaded process executes the fork. This, because there is little contact with C programming under Linux, so put it here for the time being.
The difference between a process and a thread
During the interview, this question may be the most frequently asked, and can be examined for inductive ability and understanding of processes and threads. Here's your understanding.
First, the process is the basic unit of operating system resource allocation and scheduling, and it contains the necessary resources for the program to execute. Threads, also known as lightweight processes, have only the necessary resources for CPU execution. Without a process, the thread cannot execute independently.
Second, the execution of the process is usually threaded, and a path goes to black. Threads are just the different paths that the process executes, concurrently executing.
That's all you've written.
Welcome everybody to exchange and study together.
-end-
Operating system-Threads