简体   繁体   English

Linux中的线程

[英]Threads in Linux

I have gone through mostly all the questions on here regarding the topic of Pthreads in Linux but there is a basic doubt which remains unresolved for me: 我在这里主要讨论了关于Linux中Pthreads主题的所有问题,但是有一个基本的疑问对我来说仍然没有解决:

It is mentioned in various responses that when we create a POSIX thread on Linux, there is a 1:1 mapping between user thread and kernel thread. 在各种响应中提到,当我们在Linux上创建POSIX线程时,用户线程和内核线程之间存在1:1的映射。

My doubt is when we use pthread_create() in Linux, is there 1 user thread and a corresponding unique kernel thread created implicitly (ie a total of 2 threads, one of which is invisible to the user) ? 我怀疑的是,当我们在Linux中使用pthread_create()时,是否存在1个用户线程和隐式创建的相应唯一内核线程(即总共2个线程,其中一个对用户是不可见的)

OR 要么

There only one kernel thread created and there is nothing anymore in newer Linux kernels called a user thread? 只创建了一个内核线程,在称为用户线程的新Linux内核中什么也没有了?

The NPTL (Native POSIX Thread Library) and the older LinuxThreads both use a 1:1 model, where each threads (or process) created by the user corresponds with one schedulable entity in the kernel. NPTL(Native POSIX线程库)和较旧的LinuxThreads都使用1:1模型,其中用户创建的每个线程(或进程)对应于内核中的一个可调度实体。

However, you maybe confused by user-level threads, or fibers , those are threads of execution created via calls like makecontext() and swapcontext() that have N:1 model, the kernel doesn't know about user-level threads and their scheduling is done in user-space. 但是,您可能会对用户级线程或光纤感到困惑,这些线程是通过具有N:1模型的makecontext()swapcontext()等调用创建的执行线程 ,内核不了解用户级线程及其调度在用户空间中完成。

pthread_create() internally calls fork(), and vfork()/fork() internally calls clone(). pthread_create()在内部调用fork(),而vfork()/ fork()在内部调用clone()。 So in most of the case it is 1:1 mapping. 所以在大多数情况下它是1:1映射。

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

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