简体   繁体   English

posix中的内核线程

[英]Kernel threads in posix

As far as I understand, the kernel has kernelthreads for each core in a computer and threads from the userspace are scheduled onto these kernel threads (The OS decides which thread from an application gets connected to which kernelthread). 据我所知,内核为计算机中的每个核心都有内核线程,来自用户空间的线程被调度到这些内核线程上(操作系统决定来自应用程序的哪个线程连接到哪个内核线程)。 Lets say I want to create an application that uses X number of cores on a computer with X cores. 假设我想创建一个在具有X核心的计算机上使用X个核心的应用程序。 If I use regular pthreads, I think it would be possible that the OS decides to have all the threads I created to be scheduled onto a single core. 如果我使用常规pthread,我认为操作系统可能决定将我创建的所有线程安排到一个核心上。 How can I ensure that each each thread is one-on-one with the kernelthreads? 如何确保每个线程与kernelthreads一对一?

You should basically trust the kernel you are using (in particular, because there could be another heavy process running; the kernel scheduler will choose tasks to be run during a quantum of time). 您基本上应该信任您正在使用的内核(特别是因为可能会有另一个繁重的进程在运行;内核调度程序将选择要在一段时间内运行的任务)。

Perhaps you are interested in CPU affinity, with non-portable functions like pthread_attr_setaffinity_np 也许您对CPU亲和性感兴趣,使用非便携式函数,如pthread_attr_setaffinity_np

You're understanding is a bit off. 你的理解有点偏。 'kernelthreads' on Linux are basically kernel tasks that are scheduled alongside other processes and threads. Linux上的“kernelthreads”基本上是与其他进程和线程一起安排的内核任务。 When the kernel's scheduler runs, the scheduling algorithm decides which process/thread, out of the pool of runnable threads, will be scheduled to run next on a given CPU core. 当内核的调度程序运行时,调度算法决定在可运行线程池中的哪个进程/线程将被调度为在给定的CPU内核上运行。 As @Basile Starynkevitch mentioned, you can tell the kernel to pin individual threads from your application to a particular core, which means the operating system's scheduler will only consider running it on that core, along with other threads that are not pinned to a particular core. 正如@Basile Starynkevitch所提到的,你可以告诉内核将你的应用程序中的各个线程固定到特定的核心,这意味着操作系统的调度程序只考虑在该核心上运行它,以及其他没有固定到特定核心的线程。

In general with multithreading, you don't want your number of threads to be equal to your number of cores, unless you're doing exclusively CPU-bound processing, you want number of threads > number of cores. 通常使用多线程,您不希望您的线程数等于您的内核数,除非您只进行CPU绑定处理,您需要线程数>内核数。 When waiting for network or disk IO (ie when you're waiting in an accept(2), recv(2), or read(2)) you're thread is not considered runnable. 在等待网络或磁盘IO时(即,当您在accept(2),recv(2)或read(2)中等待时),您的线程不被视为可运行。 If N threads > N cores, the operating system may be able to schedule a different thread of yours to do work while waiting for that IO. 如果N个线程> N个核心,则操作系统可能能够在等待该IO时调度您的不同线程以执行工作。

What you mention is one possible model to implement threading. 你提到的是一种实现线程的可能模型。 But such a hierarchical model may not be followed at all by a given POSIX thread implementation. 但是,给定的POSIX线程实现可能根本不遵循这样的分层模型。 Since somebody already mentioned linux, it dosn't have it, all threads are equal from the point of view of the scheduler, there. 既然有人已经提到了linux,它就没有它,从调度程序的角度来看,所有线程都是相同的。 They compete for the same resources if you don't specify something extra. 如果你没有指定额外的东西,他们会竞争相同的资源。

Last time I have seen such a hierarchical model was on a machine with an IRIX OS, long time ago. 很久以前,我上次看到这样的分层模型是在带有IRIX OS的机器上。

So in summary, there is no general rule under POSIX for that, you'd have to look up the documentation of your particular OS or ask a more specific question about it. 总而言之,POSIX下没有通用规则,您必须查找特定操作系统的文档或询问有关它的更具体的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM