POSIX Threading (i): Threading model, Pthread series functions, and simple multithreaded server-side programs
One, the thread has 3 kinds of models, namely N:1 user threading model, 1:1 core threading model and N:M hybrid threading model, POSIX thread belongs to 1:1 model.
(i), N:1 user threading Model
Thread implementation is based on the process control mechanism and is managed by the Library of user space. The OS kernel does not know the thread information at all. These threads are called user-space threads. These threads all work in the "into
(Process contention scope): Each thread competes for "scheduled CPU time" (but not directly competing with threads in other processes) in the same process.
In the N:1 threading model, the kernel does not interfere with any of the thread's life activities, nor does it interfere with thread-environment switching in the same process.
In the N:1 threading model, multiple threads in a process can only dispatch to a single CPU, which limits the amount of parallel available.
The second drawback is that if a thread executes a "blocking" operation (such as read), all threads in the process will block until the end of that operation. For this reason, some threads are implemented for
These blocking functions provide wrappers that replace these system calls with non-blocking versions to eliminate this limitation.
(ii), 1:1 core threading model Pthread line Libraries --NPTL (Native POSIX threading Library)
In the 1:1 core threading model, every thread (and also a book called LWP) created by an application is managed directly by a core thread. Each core thread is tuned to the system CPU by the OS kernel,
Therefore, all threads work in the system contention scope: the threads compete directly with other threads in system-wide.
The creation and dispatch of such threads is done by the kernel because the overhead of such threads is relatively large (but generally, less than the process overhead)
(iii), N:M hybrid threading Model ngpt (Next Generation POSIX Threads)
The N:M hybrid threading model provides level two control that maps user threads to the system's scheduler for parallelism, which is called a lightweight process (lwp:light weight processes), and the LWP one by one maps to the core thread. As shown in. Each core thread is tuned to the system CPU by the OS kernel, so all threads work in the "System race range".
It is said that some Unix-like systems (such as Solaris) have implemented a more mature m:n threading model, which has some advantages over Linux threads, but does not take advantage of the SMP architecture.
According to the notice on the official website of the NGPT on March 2003, NGPT, considering the growing acceptance of NPTL, will not be further developed in the future in order to avoid the confusion caused by different line libraries versions, and now carry out supportive maintenance work. That said, Ngpt has given up the libraries standard of competing with NPTL for the next generation of Linux POSIX lines.
Ii. Overview of POSIX threads
We know that processes run in their own separate address space, that sharing data between processes requires interprocess communication mechanisms, and that there are situations where multiple control processes need to be executed simultaneously in a single process.
Threads are useful, such as the implementation of a graphical interface to download software, on the one hand need to interact with users, waiting and handling the user's mouse and keyboard events, on the other hand, but also need to download multiple text
, waiting for and processing data from multiple network hosts, these tasks require a "wait-and-process" loop, which can be implemented in multi-threaded, a thread that is specifically responsible for interacting with the user, and several other lines
Each thread is responsible for communicating with a network host.
As we have said before, the main function and the signal processing function are multiple control flows in the same process address space, and multithreading is the same, but more flexible than the signal processing function, the signal processing function
The control process is only generated when the signal is delivered, and ends when the signal is processed, and the multithreaded control process can coexist over a long period of time, and the operating system will dispatch and switch between the threads as if in multiple
Scheduling and switching between processes. Because multiple threads of the same process share the same address space, the text Segment, Data Segment are shared, and if a function is defined, the thread
Can be called if a global variable is defined and accessible in each thread, in addition to each thread, the following process resources and environment are shared:
File Descriptor Descriptor
How each signal is processed (sig_ign, SIG_DFL, or a custom signal processing function)
Current working directory
User ID and Group ID
But some of the resources are each thread has one copy:
Thread ID
Context, including values for various registers, program counters, and stack pointers
Stack space
errno variable
Signal Shielding Word
Scheduling priority
The line libraries function we are going to learn is defined by the POSIX standard, known as POSIX thread or pthread. The Linux thread function is located in the Libpthread shared library, so the-lpthread option is added at compile time.
Note:Linux 2.6The next thread isBy the user-statePthreadLibrary implementation of.UsePthreadlater,In the view of users,Each onetask_struct< Span style= "font-family: Song body, Arial; line-height:26px; text-indent:28px; " > corresponds to a thread .getpid (Get process ID) system call returns the Tgid in Tast_struct, while the PID in Tast_struct is returned by the Gettid system.
when a thread stops/continues, or if it receives a fatal signal, the kernel applies the processing action to the entire thread group.
For example, when the program a.out run, a thread is created. Assume that the main thread's PID is 10001, the child threads are 10002 (their tgid are all 10001). At this point, if you kill 10002, you can kill 10001 and 10002 of these two threads together, even though the PS command does not see a 10002 process at all. If you don't know the story behind the Linux thread, you'll definitely feel like you're in a supernatural event.
Reference:
"Linux C Programming One-stop learning"
"UNP"
"Apue"
Http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/index.html
Http://hi.baidu.com/_kouu/item/282b80a933ccc3a829ce9dd9
http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/
POSIX threading (i): Threading model, Pthread series functions, and simple multithreaded server-side programs