Linux-线程和进程调度优先级 [英] Linux - threads and process scheduling priorities

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

问题描述

如果我们在Linux上使用默认调度策略创建pthreads(pthread_create)或进程(fork),那么调度程序在调度进程和线程时会优先处理优先级相同的进程和线程吗?

if we create pthreads (pthread_create) or processes (fork) with default scheduling policies on linux, will the scheduler treats the processes and threads with same priority while scheduling them?

让我们说有一个线程有一个进程P1 并用2个线程T1 T2处理P2

let us say there is process P1 with one thread and process P2 with 2 threads T1 T2

让我们说只有一个核心. P1 T1 P1 T2 P1 T1 P1 T2

lets say there is only one core..will the scheduling be P1 T1 P1 T2 P1 T1 P1 T2

P1 T1 T2 P1 T1 T2

P1 T1 T2 P1 T1 T2

推荐答案

Linux不再计划任何进程.

Linux no longer schedules processes at all.

在内核中,安排了线程.现在,过程的概念是一种人工构造,主要由内核外部的事物看到.显然,内核必须知道如何将线程捆绑在一起,但是不是用于调度.

Within the kernel, threads are scheduled. The concept of a process is now an artificial construct seen mostly by things outside the kernel. Obviously, the kernel has to know how threads are tied together, but not for scheduling purposes.

基本上,内核维护着很多线程,每个线程都有一个线程组领导者,这在外部被视为进程.线程具有线程ID和线程组ID,这与PID和PPID(进程ID和父进程ID)之间的关系非常相似.

Basically, the kernel maintains a whole lot of threads and each thread has a thread group leader, which is what's seen on the outside as the process. A thread has a thread ID and a thread group ID - it's a lot like the relationship between a PID and a PPID (process ID and parent process ID).

创建常规线程时,内核会为其赋予一个全新的线程ID,但其线程组ID设置为与创建它的线程的组ID相同.这样,它看起来像是流程中通往外部世界的线程.

When you create a regular thread, the kernel gives it a brand new thread ID but its thread group ID is set identical to the group ID of the thread that created it. That way, it looks like a thread within a process to the outside world.

当您 fork 时,内核会为其赋予一个全新的线程ID,并将其线程组ID设置为其与线程ID相同的值.这样,对于外界来说,这就像是一个过程.

When you fork, the kernel gives it a brand new thread ID and sets its thread group ID to the same value as its thread ID. That way, it looks like a process to the outside world.

大多数报告进程的非内核实用程序实际上只是在线程ID与线程组ID相同的线程上报告.

Most non-kernel utilities that report on processes are really just reporting on threads where the thread ID is the same as the thread group ID.

其他方法有些微妙之处,可能太复杂了,无法在此处介绍.我上面写的是(希望)是一篇不错的中级论文.

There are subtleties with other methods which are probably too complicated to go into here. What I've written above is (hopefully) a good medium level treatise.

现在,对于您的特定问题,由于P1只有一个线程(没有P1T2),因此不会出现这种情况.

Now, for your specific question, it would be neither case since P1 only has one thread (there is no P1T2).

在内核中,线程是P1T1P2T1P2T2,并假设它们具有相同的调度属性并表现相同的(a),这就是它们的方式被安排.

Withing the kernel, the threads are P1T1, P2T1 and P2T2 and, assuming they have the same scheduling properties and behave the same (a), that's how they'll be scheduled.

另请参阅:

  • Linux - Threads and Process;
  • If I have a process, and I clone it, is the PID the same?; and
  • Will a CPU process have at least one thread?

了解更多信息.

(a):显然,如果线程开始在I/O上阻塞(内核在I/O可用之前不会调度它们)或提前释放它们的时间量(内核可能会阻塞),则会发生更改提高他们的优先级,作为表现出色的奖励),但随后他们的表现却不一样.

(a): Obviously that changes if the threads start blocking on I/O (kernel won't schedule them until I/O is available) or releasing their time quanta early (kernel will probably up their priority as a reward for playing nicely) but then they're not behaving the same.

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

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