默认情况下,程序在 Linux 上使用分时(TS 策略)运行。如何从命令行在 Linux 上运行具有 SCHED_RR 策略的程序?
感谢您提供有关 chrt(1) 命令的信息。我已经使用该命令以 RR 策略运行 Firefox,但如下所示,只有 Firefox 的主线程使用 RR 策略运行。你能告诉我如何使用 RR 策略运行 Firefox 的所有其他线程吗?
$ ps -Lo pid,tid,class 2051
PID TID CLS
2051 2051 RR
2051 2055 TS
2051 2056 TS
2051 2057 TS
2051 2058 TS
2051 2059 TS
2051 2060 TS
2051 2061 TS
2051 2063 TS
2051 2067 TS
2051 2068 TS
2051 2069 TS
2051 2070 TS
2051 2072 TS
2051 2073 TS
2051 2074 TS
2051 2075 TS
2051 2077 TS
2051 2078 TS
2051 2080 TS
2051 2356 RR
2051 2386 TS
2051 2387 TS
编辑:我运行了以下简单的 pthreads 程序并像上面一样进行了测试。不幸的是 chrt 命令只改变主线程的类。请参阅下文。
$ ps -Lo pid,tid,class 3552
PID TID CLS
3552 3552 TS
3552 3553 TS
3552 3554 TS
3552 3555 TS
3552 3556 TS
3552 3557 TS
$ sudo chrt --rr -p 30 3552
...
$ ps -Lo pid,tid,class 3552
PID TID CLS
3552 3552 RR
3552 3553 TS
3552 3554 TS
3552 3555 TS
3552 3556 TS
3552 3557 TS
- - 程序 - -
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
long k = 1;
long a[10000];
int i = 1;
long b[10000];
for (k = 0; k < 400000000; k++) {
if (i == 9999) {
i = 1;
}
a[i] = ((k + i) * (k - i))/2;
a[i] = k/2;
b[i] = i * 20;
b[i] = a[i] - b[i];
i++;
int j = 0;
for (j = 0; j < i; j++) {
k = j - i;
}
}
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
使用chrt
命令chrt --rr <priority between 1-99> <command>
例子:
chrt --rr 99 ls
请注意,设置SCHED_RR
需要 root 权限,因此您必须是 root 或使用 sudo 运行它。
您还可以使用chrt
给正在运行的进程实时优先级:
chrt -p --rr <priority between 1-99> <pid>
相同的命令也适用于其他调度类,尽管使用不同的参数而不是 -rr:
Scheduling policies:
-b | --batch set policy to SCHED_BATCH
-f | --fifo set policy to SCHED_FIFO
-i | --idle set policy to SCHED_IDLE
-o | --other set policy to SCHED_OTHER
-r | --rr set policy to SCHED_RR (default)
编辑:
在 Firefox 的情况下,它必须特定于 Firefox。在我自己编写的多线程应用程序中,所有线程都保留 RR 类。如您的输出所示,两个线程都具有 RR 类,因此它也不仅仅是父线程。
编辑2:
尝试使用chrt
而不是重新安排现有的 pid 来启动进程。看来,如果您重新安排,只有第一个线程获得 RR 类。但是,如果您以 开头chrt
,则每个线程都会得到它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句