The Linux2.6 kernel implements the NPTL

Source: Internet
Author: User

NPTL is a 1x1 threading model, that is, a thread for an operating system scheduling process, the advantage is very simple. Some other operating systems, such as Solaris, are MXN, and m corresponds to the number of threads created, and n corresponds to the entities that the operating system can run. (N<M), the advantage is that thread switching is fast, but the implementation is slightly more complex

Turn

The Linux2.6 kernel implements the NPTL threading model, still using processes to simulate threads, but introduces the concept of thread groups (process groups) to make the implementation more efficient.

In the 2.4 kernel, there is no concept of thread group, when running a multithreaded program, using the PS command, you can see that there are many processes, in the PS command, the thread is basically the same as the process, in the signal processing, the same is true, only the thread that specifies the process number can receive the signal. The concept of thread groups is introduced in the 2.6 kernel, and in the 2.6 kernel, if you use the PS command to see a multithreaded process, only one process is displayed, and the process in the entire thread group is signaled when a signal is sent to any thread in the thread group.
The relevant fields in the kernel task_struct are as follows (in include/linux/sched.h):

Code :  Select All

937   struct task_struct  {
         ...
993      pid_t pid;
994      pid_t tgid;
         ...
1013     struct task_struct *group_leader;   /* threadgroup leader */
         ...
1017     struct list_head thread_group;
         ...
1198  };

The PID, which is literally the process ID, is actually the thread ID.
Tgid, literally, should be the thread group ID, which is the real process ID.
This can be seen from system calls Getpid and Gettid (located in kernel/timer.c).

Code :  Select All

954   asmlinkage long sys_getpid(void)
955   {
956      return current->tgid;
957   }
1100  asmlinkage long sys_gettid(void)
1101  {
1102     return current->pid;
1103  }

The Group_leader field, which points to the first thread in the thread group, when the first thread is created, Group_leader points to itself, creating a subsequent thread, pointing to the task_struct structure of the first thread;
Thread_group, the queue for the current process thread, for Group_leader, this is a queue header, for subsequent processes, through this field, in the queue, you can traverse all threads through this queue.
The relationship of each thread in the thread group is set in Do_fork, and the specific code is in Copy_process (at kernel/fork.c): code : Select All

959   copy_process()
960   {
         ...
1112     p->tgid = p->pid;
1113     if (clone_flags & CLONE_THREAD)
1114        p->tgid = current->tgid;
         ...
1181     p->group_leader = p;
1182     INIT_LIST_HEAD(&p->thread_group);
         ...
1234     if (clone_flags & CLONE_THREAD) {
1235        p->group_leader = current->group_leader;
1236        list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
      ...
1252     }

1254     if (likely(p->pid)) {
            ...
1259        if (thread_group_leader(p)) {
               ...
1266           list_add_tail_rcu(&p->tasks, &init_task.tasks);
               ...
1268        }
            ...
1271     }
         ...
1320  }

The 1113-1114-line description gets tgid from the parent process when the thread is created, indicates that they are in the same thread group, 1181-1182 initializes the Group_leader and Thread_group, and for the first thread, Group_leader is its own 1234-1236 lines, the Group_leader setting of the newly created thread becomes the parent process Group_leader, regardless of whether the parent process is the first thread in the thread group, its group_leader is the task_struct that points to the first thread, The Thread_group field is also attached to the Thread_group queue of the first thread, and 1266 rows indicate that only the first thread in the thread group is attached to the Init_task queue through the Tasks field.
After introducing the concept of thread group, the Exit section also introduces a new system call Exit_group (located at kernel/exit.c)

1055 Noret_type void
1056 do_group_exit (int exit_code)
1057 {
1058 bug_on (Exit_code & 0x80); /* Core dumps don ' t get here */
1059
1060 if (Current->signal->flags & Signal_group_exit)
1061 Exit_code = current->signal->group_exit_code;
1062 Else if (!thread_group_empty (current)) {
1063 struct signal_struct *const sig = current->signal;
1064 struct sighand_struct *const Sighand = current->sighand;
1065 Spin_lock_irq (&sighand->siglock);
1066 if (Sig->flags & Signal_group_exit)
1067/* Another thread got here before we took the lock. */
1068 Exit_code = sig->group_exit_code;
1069 else {
1070 Sig->group_exit_code = Exit_code;
1071 Zap_other_threads (current);
1072}
1073 Spin_unlock_irq (&sighand->siglock);
1074}
1075
1076 Do_exit (Exit_code);
1077/* notreached */
1078}

In 1060 rows, current->signal is actually shared by all threads in the thread group, and for the thread that calls Exit_group, if it is a multithreaded process, it will enter the 1062-1074 part of the code, and if it is a single thread, go directly to Do_ Exit exits the process. The main operation of this part of the code is in Zap_other_threads (located in kernel/signal.c)

void Zap_other_threads (struct task_struct *p)
982 {
983 struct task_struct *t;
984
985 p->signal->flags = Signal_group_exit;
986 p->signal->group_stop_count = 0;
987
988 if (Thread_group_empty (p))
989 return;
990
991 for (t = Next_thread (p); t! = p; t = Next_thread (t)) {
992/*
993 * Don ' t bother with already dead threads
994 */
995 if (t->exit_state)
996 continue;
997
998/* SIGKILL'll be handled before any pending SIGSTOP */
999 Sigaddset (&t->pending.signal, SIGKILL);
SIGNAL_WAKE_UP (t, 1);
1001}
1002}

Next_thread is defined in Include/linux/sched.h, as follows

651 static inline struct task_struct *next_thread (const struct TASK_STRUCT *p)
1652 {
1653 return List_entry (Rcu_dereference (P->thread_group.next),
1654 struct task_struct, thread_group);
1655}

It is actually traversing all the threads in the thread group through the Thread_group queue in task_struct.
In it, the Signal_group_exit is set in Signal->flags, and all processes in the thread group are searched, and a sigkill signal is hung in each thread, so that when those threads are dispatched to run, the Sigkill signal is processed. For the processing of the Sigkill signal, the do_group_exit is called, but when the call is made to Do_group_exit, it runs to 1061 rows and then to the do_exit of 1076 rows. This way, when each thread in a thread group runs through it, the entire thread group exits.

The POSIX thread Library (NPTL) enables the Linux kernel to run programs that are written using POSIX threading standards very efficiently. Here is a test data, under the 32-bit machine, NPTL successfully started 100,000 threads in only 2 seconds, instead of using NPTL will take about 15 minutes or so.

History


Before kernel 2.6, the scheduler entity is a process, and the kernel does not really support threads. It can be implemented by a system called Clone (), which creates a copy of the calling process, which, unlike fork (), completely shares the address space of the calling process. Linuxthread is used by this system to provide thread support at the kernel level (many of the previous thread implementations are entirely in the user state, and the kernel does not know the existence of the thread at all). Unfortunately, there are quite a few places in this approach that do not follow POSIX standards, especially in the areas of signal processing, scheduling, inter-process communication primitives, and so on.

Obviously, in order to improve the linuxthread, the kernel must be supported, and the line libraries needs to be rewritten. To achieve this, there are two competing projects: IBM-initiated NGTP (Next Generation POSIX Threads) project, and Redhat Company's NPTL. In 2003, IBM gave up the NGTP, which is about then, Redhat released the original NPTL.

NPTL was first released in Redhat Linux 9 and now supports NPTL from the RHEL3 kernel 2.6 and is fully part of the GNU C library.

Design

NPTL uses the same approach as Linuxthread, where the thread is still treated as a process and still uses the clone () system call (called in the NPTL Library). However, NPTL requires special support at the kernel level, such as the thread synchronization primitive Futex that needs to hang and then wake the thread.

NPTL is also a 1*1 line libraries, that is, when you create a thread with a pthread_create () call, a scheduler entity is created in the kernel, which is a new process in Linux, which is the most likely way to simplify the implementation of the thread.

There is also a m*n model in addition to the 1*1 model of NPTL, which typically has more user threads than the kernel's scheduler entity. In this implementation, the line libraries itself must deal with the possible scheduling, so that the context switch inside the online libraries is usually quite fast because it avoids the system call going to the kernel state. This model, however, increases the complexity of threading implementations and may have problems such as priority reversal, and how user-state scheduling can be reconciled with kernel-state scheduling is difficult to satisfy.

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/guosha/archive/2008/09/22/2960186.aspx

---------------------------------------------------------------------------

There is a problem has not been thinking clearly, consult the cattle, thank you.
Resource Description:
system Environment: 4 CPUs, linux2.6 kernel, nptl,smp.

Problem:
( a process is a resource unit; a thread is an execution unit.) )
A process has 4 threads, and these 4 threads run on 4 CPUs at the same time.
Q 1: Is the system's dispatch unit a process or a thread? (for linux2.6 cores);
Q 2: If the dispatch unit is a thread, each thread should be running in the context of the same process, and if so, lock the process context, and if the threads of another process are running on the same CPU, then the context switch of the process will be very frequent?
If the dispatch unit is a process, there is no way to explain that 4 threads in the same process are running on 4 CPUs at the same time?
Q 3: See an article saying "SMP load balancing is calculated by process count" is not known whether it is correct.
If correct, all threads of the same process should be assigned to the same CPU, puzzled?

For:

question 1 : The scheduler unit in the
    Linux kernel is always a process. However, there is support for threads in the kernel.
Question 2:
    Since the thread in Linux is a process. There's nothing there. All threads are in the same process context. And this view is wrong. What is a thread? A thread is just a subset of a process. This means a subset of the process context. And there is very little overlap in the context of each thread. Unless there is a need for communication between two threads. The thread nature of the process in Linux is the process. Only these child processes and the parent process see the same address space and share the resources. Recovering a thread's run requires only a recovery of the thread context. Since the thread is running, what is the process context? Threads are implemented in different ways. But the principle is essentially the same. Since the scheduling unit for Linux is a process, threads are also implemented as processes. So it is possible for four threads of a process to run concurrently on 4 CPUs. As long as there is no mutex.

Issue 3:
    This is a high-level issue ... I don't know... The landlord can goolge under. There should be a lot of articles and papers.

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.