Linux nice() 是用来改变线程优先级还是进程优先级的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7684404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:32:13  来源:igfitidea点击:

is nice() used to change the thread priority or the process priority?

linuxpthreadsschedulernice

提问by pierrotlefou

The man page for nicesays "nice() adds inc to the nice value for the calling process. So, can we use it to change the nice value for a thread created by pthread_create?

的手册页nice说“nice() 将 inc 添加到调用进程的 nice 值。那么,我们可以使用它来更改由创建的线程的 nice 值pthread_create吗?

EDIT: It seems that we do can set the nice value per thread.

编辑:看来我们确实可以为每个线程设置好值。

I wrote a application ,setting different nice value for different thread, and observed that the "nicer" thread has been scheduled with lower priority. Checking the output, I found that the string "high priority ................" get outputted more frequently.

我写了一个应用程序,为不同的线程设置不同的 nice 值,并观察到“更好”的线程已被调度为较低的优先级。检查输出,我发现字符串“高优先级……”的输出频率更高。

void * thread_function1(void *arg)
{

 pid_t tid = syscall(SYS_gettid);

 int ret = setpriority(PRIO_PROCESS, tid, -10);
 printf("tid of high priority thread %d , %d\n",tid ,getpriority(PRIO_PROCESS,tid));
 while(1){
  printf("high priority ................\n");
    }

}


void * thread_function(void *arg)
{
 pid_t tid = syscall(SYS_gettid);
 int ret = setpriority(PRIO_PROCESS, tid, 10);
 printf("tid of low priority thread %d , %d \n",tid ,getpriority(PRIO_PROCESS,tid));
while(1) 
{

  printf("lower priority\n");
}
}


int main()
{

pthread_t id1;
pthread_t id2;

pid_t pid = getpid();
pid_t tid = syscall(SYS_gettid);

printf("main thread : pid = %d , tid = %d \n" , pid, tid);
pthread_create(&id1, NULL, thread_function1,  NULL);
pthread_create(&id2, NULL,thread_function,   NULL);

pthread_join(id1, NULL);
pthread_join(id2, NULL);


}

采纳答案by Frédéric Hamidi

The pthreads man pagesays:

并行线程手册页说:

POSIX.1 also requires that threads share a range of other attributes (i.e., these attributes are process-wide rather than per-thread):

[...]

  • nice value (setpriority(2))

POSIX.1 还要求线程共享一系列其他属性(即,这些属性是进程范围的而不是每个线程的):

[...]

  • 不错的价值 ( setpriority(2))

So, theoretically, the "niceness" value is global to the process and shared by all threads, and you should not be able to set a specific niceness for one or more individual threads.

因此,从理论上讲,“niceness”值对于进程来说是全局的,并且由所有线程共享,并且您不应该为一个或多个单独的线程设置特定的 niceness。

However, the very same man page also says:

然而,同样的手册页也说:

LinuxThreads

The notable features of this implementation are the following:

[...]

  • Threads do not share a common nice value.

NPTL

[...]

NPTL still has a few non-conformances with POSIX.1:

  • Threads do not share a common nice value.

Linux线程

此实现的显着特点如下:

[...]

  • 线程不共享一个共同的 nice 值。

国家知识产权局

[...]

NPTL 与 POSIX.1 仍有一些不符合项:

  • 线程不共享一个共同的 nice 值。

So it turns out that both threading implementations on Linux (LinuxThreads and NPTL) actually violate POSIX.1, and you can set a specific niceness for one or more individual threads by passing a tidto setpriority()on these systems.

因此,事实证明 Linux 上的两个线程实现(LinuxThreads 和 NPTL)实际上都违反了 POSIX.1,并且您可以通过在这些系统上将a 传递tidsetpriority()来为一个或多个单独的线程设置特定的好感