Linux kernel--Processes and threads

Source: Internet
Author: User

http://blog.csdn.net/yusiguyuan/article/details/12154823

In the third chapter of Linux kernel design and implementation, the process management is explained, and the difference between the concept of process and thread is blurred by the point. The book says:

Process is the procedure that is in the execution period. However, the process is not confined to a single executable program code. Typically the process also contains other resources, such as open files, pending signals, kernel internal data, a tool state, one or more memory-mapped address spaces, and one or more threads of execution, including, of course, data segments to hold global variables.

Each thread, however, has a separate program counter, a process stack, and a set of process registers. The following sentence: the kernel dispatches an object that is a thread, not a process. In fact, the premise of this sentence is that a process consists of only one thread. In the current operating system, a process is comprised of multiple threads.

A task is the most abstract and a generic term that refers to an activity done by the software. A task can be either a process or a thread. In short, it refers to a series of operations that collectively achieve a purpose. For example, read the data and put the data in memory. This task can be implemented as a process, or as a thread (or as an interrupt task).

Process is often defined as the execution of a program. You can think of a process as a separate program that has complete data space and code space in memory. The data and variables owned by a process belong to itself only.

Threads are programs that run alone in a process. That is, the thread exists in the process. A process consists of one or more threads, each of which shares the same code and global data, but each has its own stack. because the stack is one per thread, local variables are private to each thread. Because all threads share the same code and global data, they are more compact than processes, tend to interact more with each other than individual processes, and are easier to interact with each other because they themselves have some shared memory for communication: The global data for the process.

The most significant difference between a process and a thread is that the thread has its own global data. Threads exist in a process, so the global variables of a process are shared by all threads. Because threads share the same system area, the resources assigned to a process by the operating system are available to all threads of the process, just as the global data is available to all threads.

in short, a program has at least one process, and a process has at least one thread . The thread's dividing scale is smaller than the process, which makes the multi-thread procedure high concurrency. In addition, the process has a separate memory unit during execution, and multiple threads share memory, which greatly improves the efficiency of the program Operation . Threads are still different from the process during execution. Each separate thread has a program run entry, sequence of sequence execution, and exit of the program. However, threads cannot be executed independently, and must be dependent on the application, which provides multiple threads of execution control. From a logical point of view, the meaning of multithreading lies in an application where multiple execution parts can be executed concurrently. However, the operating system does not consider multiple threads as separate applications to implement scheduling and management of processes and resource allocations, which are important differences between processes and threads.

One thread can create and revoke another thread, and can execute concurrently between multiple threads in the same process.

The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process dead, so the multi-process program is more robust than multithreaded programs, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.


Process Concepts
Process is the basic unit of resource allocation and the basic unit of dispatching operation. For example, when a user runs his or her own program, the system creates a process and assigns IT resources, including tables, memory space, disk space, I/O devices, and so on. Then, put the process in the ready queue of the human process. The process Scheduler selects it, allocates CPU and other related resources to it, and the process actually runs. Therefore, the process is the unit of concurrent execution in the system.
In a microkernel-based operating system such as Mac, Windows NT, the functionality of the process has changed: it is only the unit of the resource allocation, not the unit of the dispatch run. In a microkernel system, the basic unit of a real dispatch operation is a thread. Therefore, the unit that implements the concurrency feature is the thread.


Threading Concepts
A thread is the smallest unit of execution in a process, that is, the basic unit of the execution processor dispatch. If the process is understood as a task that is done logically by the operating system, then the thread represents one of many possible subtasks that complete the task. For example, if a user launches a database application in a window, the operating system represents a call to the database as a process. Suppose a user is going to generate a payroll report from a database and upload it to a file, which is a subtask; in the process of generating payroll reports, the user can also lose the database query request, which is a sub-task. In this way, the operating system represents each request-payroll report and the new sender's data query as a separate thread in the database process.

Threads can be scheduled to execute independently on the processor, allowing several threads to be on separate processors in a multiprocessor environment. The operating system provides threads for the convenience and effectiveness of this concurrency.


Benefits of introducing threading
(1) Easy to dispatch.
(2) Improve concurrency. Concurrency can be easily and efficiently achieved through threading. A process can create multiple threads to perform different parts of the same program.
(3) less overhead. Creating a line turndown creates a process that is fast and requires little overhead.
(4) Facilitate the full use of multi-processor functions. By creating a multithreaded process (that is, a process can have two or more threads), each thread runs on a single processor, enabling the concurrency of the application so that each processor is fully operational.


relationship of processes and threads
(1) A thread can belong to only one process, while a process may have multiple threads, but at least one thread.
(2) A resource is allocated to a process, and all the threads of the same process share all the resources of that process.
(3) The processor is assigned to a thread, that is, a thread that is actually running on the processing machine.
(4) Threads need to be synchronized during execution. Synchronization is achieved between threads of different processes using the means of message communication.

but for the Linux operating system, the scheduling solution is a thread rather than a process, and a large part of the reason is that Linux considers threads and processes to be the same.

Another very appropriate explanation for processes and threads: http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html

Linux kernel--Processes and threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.