Linux 如何处理线程和进程调度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8463741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:35:06  来源:igfitidea点击:

How Linux handles threads and process scheduling

linuxmultithreadingprocessscheduling

提问by whitefox

I'm trying to understand how Linux handles process scheduling and thread scheduling. I read that Linux can schedule both processes and threads.

我试图了解 Linux 如何处理进程调度和线程调度。我读到 Linux 可以同时调度进程和线程。

Does Linux have a thread scheduler AND a process scheduler? If yes, how do they cooperate?

Linux 有线程调度器和进程调度器吗?如果有,他们是如何合作的?

采纳答案by Basile Starynkevitch

The Linux kernel scheduleris actually scheduling tasks, and these are either threads or (single-threaded) processes.

Linux内核调度器被实际调度的任务,并且这些要么螺纹或(单线程)工艺。

So a task(a task_structinside the kernel), in the context of the scheduler, is the thing being scheduled, and can be some kernel threadlike kworkeror kswapd, some user threadof a multi-threaded process (like firefox), or the single-thread of a single-threaded process(like bash), identified with that single-threaded process.

因此,在调度程序的上下文中,任务task_struct内核内部)是被调度的事物,可以是某个内核线程,例如kworkerkswapd,多线程进程的某个用户线程(例如firefox),或单线程一个的单线程处理(像bash),确定与该单线程处理。

A processis a non-empty finite set (sometimes a singleton) of threadssharing the same virtual address space(and other things like file descriptors, working directory, etc etc...). See also credentials(7), capabilities(7)etc....

一个过程是一个非空有限集(有时是单身)的线程共享相同的虚拟地址空间(和其他东西一样文件描述符工作目录,等等...)。另请参阅凭据(7)功能(7)等....

Threads on Linux are kernel threads(in the sense of being managed by the kernel, which also creates its own threads), created by the Linux specific clonesyscall (which can also be used to create processes on Linux). The pthread_createfunction is probably built (on Linux) above cloneinside NPTLand Gnu Libc(which integrated NPTL on Linux) and musl-libc.

Linux 上的线程内核线程(在内核管理的意义上,它也创建自己的线程),由 Linux 特定的克隆系统调用(也可用于在 Linux 上创建进程)创建。将在pthread_create函数可能建于(在Linux上)上述clone内部NPTLGNU库(其中集成NPTL在Linux上)和MUSL-libc中

回答by nurio

Kernel threads under Linux are implemented as processes that share resources. The scheduler does not differentiate between a thread and a process

Linux 下的内核线程被实现为共享资源的进程。调度器不区分线程和进程

See here for more information: http://www.linuxquestions.org/linux/articles/Technical/Linux_Kernel_Thread

有关更多信息,请参见此处:http: //www.linuxquestions.org/linux/articles/Technical/Linux_Kernel_Thread

回答by kiran

Under LINUX there is no concept of threads,to make LINUX POSIX complaint thread is nothing but another process.when you try to get a process id it would display the leader process id under any thread. For more details try to refer this book "Understanding LINUX kernel".Hope

在LINUX下没有线程的概念,为了使LINUX POSIX抱怨线程只不过是另一个进程。当您尝试获取进程ID时,它会显示任何线程下的领导进程ID。更多细节可以参考这本书《理解LINUX内核》。希望