Nice() 是用来改变线程优先级还是进程优先级?
nice
的手册页说“nice() 将 inc 添加到调用进程的nice值中。那么,我们可以使用它来更改创建的线程的nice值吗?”通过 pthread_create
编辑: 看来我们可以为每个线程设置nice值。
我编写了一个应用程序,为不同的线程设置不同的好值,并观察到“更好”的线程已被安排为较低的优先级。检查输出,我发现字符串“highpriority......”的输出频率更高。
void * thread_function1(void *arg)
{
const 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)
{
const 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);
}
The man page for nice
says "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
?
EDIT:
It seems that we can set the nice value per thread.
I wrote an application, setting different nice values for different threads, and observed that the "nicer" thread has been scheduled with lower priority. Checking the output, I found that the string "high priority ................" gets outputted more frequently.
void * thread_function1(void *arg)
{
const 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)
{
const 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
pthreads 手册页 说:
因此,理论上,“niceness”值对于进程来说是全局的并由所有线程共享,并且您不应该能够为一个或多个单独的线程设置特定的良好度。
然而,同一个手册页还说:
因此,Linux 上的两种线程实现(LinuxThreads 和 NPTL)实际上都违反了 POSIX.1,您可以通过将
tid
传递给 setpriority()。The pthreads man page says:
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.
However, the very same man page also says:
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
tid
to setpriority() on these systems.根据 setpriority 的手册页,较低的好值( Nice 值在
-20
到20
范围内)表示调度优先级较高。看起来您的程序按预期工作(nice = -10
赋予该线程更高的优先级)。According to the man page for setpriority, a lower nice value (nice values are in the range of
-20
to20
) means higher priority in scheduling. It looks like your program works as expected (nice = -10
gives this thread higher priority).我想测试更改这些值如何真正影响线程的优先级,因此我将您的代码片段修改为以下基准:
SCHED_OTHER
调度策略上运行cat /proc/cpuinfo
)thread_function()
以执行一些“数字运算工作”当设置为边缘优先级时,您肯定可以使用
top -H 看到
高优先级线程运行频率更高,但其他线程不会出现饥饿现象。相关字段为NI
和TIME+
来自 顶部手册页:
使用
g++.cpp -lpthread
编译。运行
top -H -p $(pidof)
以启用线程模式并获取特定进程的信息I wanted to test how changing these values really affects the thread's priority, so I modified your snippet to this benchmark:
SCHED_OTHER
scheduling policycat /proc/cpuinfo
)thread_function()
to do some "number crunching work"When setting to edge priorities you can definitely see with
top -H
that the high priority thread runs more often, but no starvation occurs to other threads. relevant fields areNI
andTIME+
From top man page:
Compiled with
g++ <FILE_NAME>.cpp -lpthread
.Run
top -H -p $(pidof <PROCESS_NAME>)
to enable Threads-mode and get information for specific process