在 Linux 中,进程是一组线程。每个线程都有自己的优先级!但是进程也有优先级吗?如果是这样,它与线程优先级有何不同?当创建一个新进程时,这些值是如何传播的?
SCHED_FIFO 和 SCHED_RR 都用于实时使用。我知道 SCHED_RR 可以被时间切片抢占。但是,如果我有一个线程设置为 SCHED_FIFO,另一个线程设置为 SCHED_RR,如果两个线程都准备好运行,它们是否纯粹按优先级进行调度?如果它们具有相同的优先级怎么办?
我有一个具有 8 个物理核心(16 个具有超线程)的 Intel 处理器,我试图通过提高 C++ 应用程序进程的优先级以及我在此应用程序中启动的四个线程的优先级来充分利用其中四个核心(我正在使用 OpenMp 来午餐线程)。
根据微软的优先级表:
| 进程优先级 | 线程优先级 | 基本优先级 |
|---|---|---|
| HIGH_PRIORITY_CLASS | 线程优先级空闲 | 1 |
| THREAD_PRIORITY_LOWEST | 11 | |
| THREAD_PRIORITY_BELOW_NORMAL | 12 | |
| 线程优先级正常 | 13 | |
| THREAD_PRIORITY_ABOVE_NORMAL | 14 | |
| THREAD_PRIORITY_HIGHEST | 15 | |
| THREAD_PRIORITY_TIME_CRITICAL | 15 | |
| REALTIME_PRIORITY_CLASS | 线程优先级空闲 | 16 |
| THREAD_PRIORITY_LOWEST | 22 | |
| THREAD_PRIORITY_BELOW_NORMAL | 23 | |
| 线程优先级正常 | 24 | |
| THREAD_PRIORITY_ABOVE_NORMAL | 25 | |
| THREAD_PRIORITY_HIGHEST | 26 | |
| THREAD_PRIORITY_TIME_CRITICAL | 31 |
当我执行以下操作时,我只看到优先级设置为 15:
int priority;
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
priority = GetThreadPriority(GetCurrentThread());
Run Code Online (Sandbox Code Playgroud)
另外,从任务管理器检查优先级,它设置为高,而不是实时的。
如何将优先级提高到15以上?
调用pthread_create函数后,我收到下一条消息:
W/libc(26409):pthread_create sched_setscheduler调用失败:不允许操作
用于创建线程的代码是:
pthread_attr_t threadAttr;
int ret = pthread_attr_init(&threadAttr);
//code to check ret - it's 0
size_t guard_size = 0;
pthread_attr_getguardsize(&threadAttr, &guard_size);
ret = pthread_attr_setstacksize(&threadAttr, myStackSize + guard_size);
//code to check ret - it's 0
ret = pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);
//code to check ret - it's 0
ret = pthread_attr_setschedpolicy(&threadAttr, SCHED_FIFO);
//code to check ret - it's 0
sched_param schedParam;
schedParam.sched_priority = myPriority; //it's 16
ret = pthread_attr_setschedparam(&threadAttr, &schedParam);
//code to check ret - it's 0
// Create …Run Code Online (Sandbox Code Playgroud) 我知道线程池优先级应该/不能由正在运行的进程更改,但是在线程池上运行的特定任务的优先级是否在某种程度上与调用进程优先级有关?
换句话说,无论调用进程的优先级如何,线程池中的所有任务是否都以相同的优先级运行?
谢谢
更新 1:我应该更具体,我指的是 Parallel.ForEach 内的线程
我有以下来源喜欢 SCHED_RR 优先级 90 :
int main(int argc, char** argv)
{
const char *sched_policy[] = {
"SCHED_OTHER",
"SCHED_FIFO",
"SCHED_RR",
"SCHED_BATCH"
};
struct sched_param sp = {
.sched_priority = 90
};
pid_t pid = getpid();
printf("pid=(%d)\n",pid);
sched_setscheduler(pid, SCHED_RR, &sp);
printf("Scheduler Policy is %s.\n", sched_policy[sched_getscheduler(pid)]);
pthread_t tid ;
pthread_create(&tid , NULL, Thread1 , (void*)(long)3);
pthread_create(&tid , NULL, Thread2 , (void*)(long)3);
pthread_create(&tid , NULL, Thread3 , (void*)(long)3);
while(1)
sleep(100);
}
Run Code Online (Sandbox Code Playgroud)
而shell“top”,我可以看到该进程具有-91的PR,看起来它可以工作,据我所知,在Linux中,thread1和thread2和thread3是与主线程不同的任务,它们只是共享相同的虚拟内存,我想知道在这个测试中,我是否需要添加
pthread_setschedparam(pthread_self(), SCHED_RR, &sp);
Run Code Online (Sandbox Code Playgroud)
对于所有线程 1、线程 2 和线程 3,所有这 3 个都可以使用 SCHED_RR 进行调度?!或者我不需要这样做?!以及如何观察 …
我使用我的 Maven 项目的并行构建。
通常我会运行mvn install -T 4(我的 PC 上有 4 个内核)。
之后,我必须等待近 5-10 分钟才能完全构建项目。我无法在 Windows 工作站上执行任何操作:鼠标/键盘没有响应。CPU 负载 = 100%。
有没有办法降低 maven 生成的线程/进程(javac)的优先级?
据我了解,如果我将优先级设置为低于正常值 - 操作系统将为 maven 构建安排更少的时间,而为其他程序安排更多的时间。
我正在尝试更改线程的优先级,但无法使其工作。我制作了一个按钮,可以在低和高之间切换优先级,当我在作业列表中检查此按钮时,优先级会发生变化。但CPU使用率没有改变。我想知道这是否只是因为我没有使用全部 CPU 能力或者这是怎么回事。
我不是在问这是否是一个好主意。我是问怎么做。
这是我更改优先级的方法。这是类背后的代码:
private Thread tr;
public MainWindow()
{
InitializeComponent();
tr = new Thread(new ThreadStart(infiniteLoop));
tr.Start();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Process.GetCurrentProcess().PriorityClass == ProcessPriorityClass.High)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;
tr.Priority = ThreadPriority.Lowest;
description.Text = "Idle";
}
else
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
tr.Priority = ThreadPriority.Highest;
description.Text = "High";
}
}
private void infiniteLoop()
{
while (true)
{
}
}
Run Code Online (Sandbox Code Playgroud) 以下代码5个具有不同优先级的线程正在竞争访问具有8个内核的CPU(Mac OS X 10.8.5,Mono).每个线程增加其计数器.
using System;
using System.Threading;
class PriorityTesting
{
static long[] counts;
static bool finish;
static void ThreadFunc(object iThread)
{
while(true)
{
if(finish)
break;
counts[(int)iThread]++;
}
}
static void Main()
{
counts = new long[5];
Thread[] t = new Thread[5];
for(int i=0; i<t.Length; i++)
{
t[i] = new Thread(ThreadFunc);
t[i].Priority = (ThreadPriority)i;
}
// ????????? ??????
for(int i=0; i<t.Length; i++)
t[i].Start(i);
// ???? ??????? ??????????? ?????????? 10 c
Thread.Sleep(10000);
// ?????? ? ??????????
finish = true;
// ??????? …Run Code Online (Sandbox Code Playgroud) c# mono multithreading task-parallel-library thread-priority
以下是我的代码.
public class Test {
public static void main(String[] args) throws InterruptedException {
PrintingThread a = new PrintingThread("A");
a.setPriority(9);
PrintingThread b = new PrintingThread("B");
b.setPriority(1);
a.start();
b.start();
}
static class PrintingThread extends Thread {
private int i = 0;
private String name;
public PrintingThread(String name) {
super();
this.name = name;
}
@Override
public void run() {
while (true) {
i++;
if (i % 100000 == 0) {
System.out.println("Thread " + name + " count=" + i
+ ". Time :" …Run Code Online (Sandbox Code Playgroud) thread-priority ×10
c# ×3
linux ×3
c ×2
java ×2
pthreads ×2
android ×1
c++ ×1
concurrency ×1
maven ×1
mono ×1
real-time ×1
threadpool ×1
winapi ×1
wpf ×1