sched_setscheduler 是针对所有线程还是主线程?
sched_setscheduler is for all threads or main thread?
我有以下来源,喜欢 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);
}
而 shell "top" ,我可以看到该进程具有 -91 的 PR,看起来它有效,
据我所知,在 Linux 中,thread1 和 thread2 以及 thread3 是不同的任务
从主线程,他们只是共享相同的虚拟内存,我想知道
在此测试中,我是否需要添加
pthread_setschedparam(pthread_self(), SCHED_RR, &sp);
对于所有线程 1、线程 2 和线程 3,以便所有这 3 个都可以被调度
用 SCHED_RR ?!或者我不需要那样做?!我怎样才能观察到
线程 1、线程 2 和线程 3 线程是 SCHED_RR 或 SCHED_OTHER ?!
编辑:
sudo chrt -v -r 90 ./xxx.exe
将会看到:
pid 7187's new scheduling policy: SCHED_RR
pid 7187's new scheduling priority: 90
我如何确定这仅适用于主线程?!或 pid 7187 中的所有线程
是 SCHED_RR 政策吗?!再一次,如何观察它?!
您应该在创建任何新线程之前检查(并在需要时设置)调度程序继承属性。
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched);
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
pthread_attr_getinheritsched()
将存储在 inheritsched
指向的变量中两个可能值之一:
PTHREAD_INHERIT_SCHED - Threads that are created using attr
inherit scheduling attributes from the creating thread; the
scheduling attributes in attr are ignored.
PTHREAD_EXPLICIT_SCHED - Threads that are created using attr take
their scheduling attributes from the values specified by the
attributes object.
如果您希望每个新创建的线程都继承调用任务的调度程序属性,您应该设置PTHREAD_INHERIT_SCHED(如果尚未设置)。
另请注意:
The default setting of the inherit-scheduler attribute in a newly
initialized thread attributes object is PTHREAD_INHERIT_SCHED
参考文献
$ man pthread_setschedparam
$ man pthread_attr_setinheritsched
- (Blockquoted material 是从 Linux 手册页项目的 3.74 版部分复制的。)
我有以下来源,喜欢 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);
}
而 shell "top" ,我可以看到该进程具有 -91 的 PR,看起来它有效, 据我所知,在 Linux 中,thread1 和 thread2 以及 thread3 是不同的任务 从主线程,他们只是共享相同的虚拟内存,我想知道 在此测试中,我是否需要添加
pthread_setschedparam(pthread_self(), SCHED_RR, &sp);
对于所有线程 1、线程 2 和线程 3,以便所有这 3 个都可以被调度 用 SCHED_RR ?!或者我不需要那样做?!我怎样才能观察到 线程 1、线程 2 和线程 3 线程是 SCHED_RR 或 SCHED_OTHER ?!
编辑:
sudo chrt -v -r 90 ./xxx.exe
将会看到:
pid 7187's new scheduling policy: SCHED_RR
pid 7187's new scheduling priority: 90
我如何确定这仅适用于主线程?!或 pid 7187 中的所有线程 是 SCHED_RR 政策吗?!再一次,如何观察它?!
您应该在创建任何新线程之前检查(并在需要时设置)调度程序继承属性。
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched);
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
pthread_attr_getinheritsched()
将存储在 inheritsched
指向的变量中两个可能值之一:
PTHREAD_INHERIT_SCHED - Threads that are created using attr
inherit scheduling attributes from the creating thread; the scheduling attributes in attr are ignored.PTHREAD_EXPLICIT_SCHED - Threads that are created using attr take their scheduling attributes from the values specified by the attributes object.
如果您希望每个新创建的线程都继承调用任务的调度程序属性,您应该设置PTHREAD_INHERIT_SCHED(如果尚未设置)。
另请注意:
The default setting of the inherit-scheduler attribute in a newly initialized thread attributes object is PTHREAD_INHERIT_SCHED
参考文献
$ man pthread_setschedparam
$ man pthread_attr_setinheritsched
- (Blockquoted material 是从 Linux 手册页项目的 3.74 版部分复制的。)