Introduction to Linux Threads

Source: Internet
Author: User
Tags data structures resource

A process is an instance of the execution of a program, that is, the collection of data structures to which the program has been executed. From the kernel point of view, the purpose of the process is to serve as the basic unit of allocating system resources (CPU time, memory, etc.).

A thread is a stream of execution of a process, the basic unit of CPU scheduling and dispatch, which is a smaller unit that can operate independently than a process. A process consists of several threads (the user program that has many relatively independent execution streams shares most of the data structures of the application), and the thread shares all the resources owned by the process with other threads of the same process.

"Process-the smallest unit of resource allocation, thread-the smallest unit of program execution"

Processes have separate address spaces, and when a process crashes, it does not affect other processes in protected mode, and threads are just different execution paths in a process. Thread has its own stack and local variables, but the thread does not have a separate address space, a thread dead is equal to the entire process dead, so the process of multiple processes than multithreaded program is robust, but in the process of switching, the resource consumption is greater, the efficiency is some worse. However, for concurrent operations that require simultaneous and share certain variables, only threads can be used, and processes cannot be used.
2, the use of thread reasons

From the above we know the difference between process and thread, in fact, these differences are the reasons why we use threads. In general, the process has a separate address space, and the thread does not have a separate address space (the thread in the same process is sharing the address space of the process). (The following is excerpted from multithreaded programming under Linux)

One reason to use multithreading is that it is a very "frugal" multitasking operation compared to a process. We know that under a Linux system, starting a new process must be assigned to its own address space, a large number of data tables to maintain its code snippets, stack segments and data segments, which is an "expensive" multitasking way of working. While running on multiple threads in a process, they use the same address space, share most of the data, and start a thread much less space than it takes to start a process, and the time it takes to switch between threads is much less than the time it takes to switch between processes. According to statistics, overall, the cost of a process is about 30 times times the cost of a thread, of course, on the specific system, this data may be a big difference.

The second reason to use multithreading is the convenient communication mechanism between threads. For different processes, they have a separate data space, the transmission of data can only be carried out by means of communication, which is not only time-consuming, but also inconvenient. Threads Otherwise, because the data space is shared between threads under the same process, the data for one thread can be used directly for other threads, which is quick and convenient. Of course, the sharing of data also brings some other problems, some variables can not be modified by two threads at the same time, some subroutines declared as static data is more likely to bring a catastrophic attack on multithreaded programs, which is the most need to note when writing multithreaded program.

In addition to the above mentioned advantages, not compared with the process, multithreaded procedure as a multitasking, concurrent work, of course, has the following advantages:

Increase application responsiveness. This is especially useful for graphical interface programs, when an operation takes a long time, the whole system will wait for this operation, when the program does not respond to keyboard, mouse, menu operation, and using multithreading technology, the time-consuming operation (time consuming) on a new thread, you can avoid this embarrassing situation.

Make multiple CPU systems more efficient. The operating system guarantees that when the number of threads is not greater than the number of CPUs, different threads run on different CPUs.

Improve the structure of the program. A long and complex process can be considered into multiple threads to become several independent or half independent parts of the operation, such programs will facilitate understanding and modification.

=============================

From a function call, the process creates using the fork () action, and the thread creates a clone () action. This is what Richard Stevens said:

Fork is expensive. Memory is copied from the parent to the "child", all descriptors are duplicated in the "Child", and "." Current implementations use a technique called copy-on-write, which avoids a copy of the parent ' s data spaces to the child Until the child needs its own copy. But, regardless of this optimization, fork is expensive.

The IPC is required to pass information between, the parent and child, after the fork. Passing information from the ' parent to the ' child before the fork are easy, since the child starts with a copy of the parent ' s data spaces and with a copy of the ' descriptors '. But, returning information from the "child" to the parent takes more work.

Threads help with both problems. Threads are sometimes called lightweight processes since a thread is "lighter weight" than a process. The thread creation can be 10–100 The times faster than process creation.

All threads within a process share the same global memory. This makes the sharing of information easy between the threads, but along with this simplicity comes the problem of SYNCHR Onization.

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.