linux 是调度进程还是线程? [英] Does linux schedule a process or a thread?

查看:62
本文介绍了linux 是调度进程还是线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读这个 SO 问题后,我有一些疑问.请帮助理解.

After reading this SO question I got a few doubts. Please help in understanding.

调度涉及决定何时运行进程以及运行多长时间.

Scheduling involves deciding when to run a process and for what quantum of time.

  1. linux 内核是调度线程还是进程?由于进程和线程在内核内部没有区别,调度程序如何处理它们?

  1. Does linux kernel schedule a thread or a process? As process and thread are not differentiated inside kernel how a scheduler treats them?

每个线程的量是如何决定的?一个.如果为一个进程确定了一个时间量(比如 100us),是否在进程的所有线程之间共享?或者湾每个线程的量程是由调度程序决定的吗?

How quantum for each thread is decided? a. If a quantum of time (say 100us) is decided for a process is that getting shared between all the threads of the process? or b. A quantum for each thread is decided by the scheduler?

注意:问题 1 和问题 2 是相关的,可能看起来相同,但只是想弄清楚事情是如何运作的,将它们都张贴在这里.

Note: Questions 1 and 2 are related and may look the same but just wanted to be clear on how things are working posted them both here.

推荐答案

Linux 调度程序(在最近的 Linux 内核上,例如至少 3.0)正在调度可调度任务或简单地任务em>.

The Linux scheduler (on recent Linux kernels, e.g. 3.0 at least) is scheduling schedulable tasks or simply tasks.

一个任务可能是:

  • 单线程进程(例如由fork创建,没有任何线程库)
  • 多线程进程内的任何线程(包括其主线程),尤其是 Posix 线程(pthread)
  • 内核任务,在内核内部启动并留在内核空间(例如 kworkernfsiodkjournaldkauditd, kswapd 等等...)
  • a single-threaded process (e.g. created by fork without any thread library)
  • any thread inside a multi-threaded process (including its main thread), in particular Posix threads (pthreads)
  • kernel tasks, which are started internally in the kernel and stay in kernel land (e.g. kworker, nfsiod, kjournald , kauditd, kswapd etc etc...)

换句话说,多线程进程中的线程被调度为非线程的——即单线程进程.

In other words, threads inside multi-threaded processes are scheduled like non-threaded -i.e. single threaded- processes.

低级 clone(2) 系统调用创建用户级可调度任务(可用于创建 fork 处理的进程或用于实现线程库,如 pthread).除非你是一个低级线程库实现者,否则你不想直接使用clone.

The low-level clone(2) syscall creates user-land schedulable tasks (and can be used both for creating fork-ed process or for implementation of thread libraries, like pthread). Unless you are a low-level thread library implementor, you don't want to use clone directly.

AFAIK,对于多线程进程,内核(几乎)不是在调度进程,而是在内部的每个单独线程(包括主线程).

AFAIK, for multi-threaded processes, the kernel is (almost) not scheduling the process, but each individual thread inside (including the main thread).

实际上,调度中有一些线程组和affinity的概念,但我不太了解他们

Actually, there is some notion of thread groups and affinity in the scheduling, but I don't know them well

如今,处理器通常有多个内核,每个内核都在运行一项任务(在某个给定时刻),因此您确实有多个任务并行运行.

These days, processors have generally more than one core, and each core is running a task (at some given instant) so you do have several tasks running in parallel.

CPU 时间分配给任务,而不是给进程

CPU quantum times are given to tasks, not to processes

这篇关于linux 是调度进程还是线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆