尼斯级别的pthreads? [英] Nice-Level for pthreads?

查看:78
本文介绍了尼斯级别的pthreads?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嵌入式系统上(使用glibc 2.6.1的ARM处理器上的Linux内核2.6.28),我正在运行由多个线程组成的应用程序.我希望其中一个线程比其他线程获得更多的CPU时间.

On an embedded system (Linux kernel 2.6.28 on ARM processor using glibc 2.6.1) I am running an application consisting of multiple threads. I would like one of those threads to get more CPU time than the others.

设置优先级的一个选项似乎是将pthread \ _setschedparamSCHED \ _RR(或SCHED \ _FIFO)一起使用,但是这给线程提供了太多的CPU(除非它进入睡眠状态,将所有CPU都从仍在使用SCHED_OTHER的同一个线程中吞噬掉.

One option for setting priorities seems to be to use pthread \ _setschedparam with SCHED \ _RR (or SCHED \ _FIFO), however this gives the thread quite too much CPU (unless it sleeps, it eats away all CPU from its fellow threads which are still using SCHED_OTHER).

另一个选择是设置线程的级别.但是,尽管这正是我想要的(线程只是获得了更多的CPU,但无法使其他线程饿死),但我无法使其正常工作.根据手册页的内容,线程没有共享一个通用的nice值",我将其解释为可以在同一进程中为不同的线程设置单独的nice值.

Another option would be setting the nice level of the thread. However while this would be exactly what I was looking for (the thread just gets a bit more CPU but can't starve the other threads), I can't get it to work correctly. According to the man pages "threads do not share a common nice value", which I interpret as that I can set individual nice values to different threads in the same process.

这是我的代码:

pid_t tid;
tid = syscall(SYS_gettid);

int ret = setpriority(PRIO_PROCESS, tid, priority);

但是,在所有线程中,tid似乎都是相同的.同样,为线程设置单独的尼斯级别时,在调用getpriority时仍将在所有线程中返回最新的尼斯级别.从性能上来看,这是一致的(即以相同的任务运行线程A和线程B,然后将A设置为-19,将B设置为19,并且两者将大致同时完成).

However it seems that tid is the same among all threads. Also setting individual nice-levels for thread will still return the latest set nice level in all threads when calling getpriority. From the performance this is consistent (i.e. run thread A and thread B with the same task, then set A to -19 and B to 19 and both will finish roughly the same time).

但是,当运行线程A时,将其设置为-19,然后运行B,然后将其设置为+19,将使B以全功率运行(比A提前完成).

Yet, when running thread A, then setting it to -19 and then running B and setting it to +19 will have B run at full power (finishing way ahead of A).

因此,似乎线程将在运行时使用漂亮的级别集,而此后无法对其进行更改.

So it seems like a thread will use the nice level set at the time it is run with no way of changing it afterwards.

我疯狂地在Google上搜索了更多信息或进行了任何形式的确认,但到目前为止,我发现的所有内容都相当模糊.这里是否有人对Linux(2.6.28,glibc 2.6.1)如何处理线程的精确级别有任何描述?以及如何更改已经运行的线程的良好级别?

I've googled like crazy for more information or any kind of confirmation, but all I found so far is rather vague. Does anybody here have any description available on how nice levels for threads are handled on Linux (2.6.28, glibc 2.6.1)? And how I possibly could change the nice level of an already running thread?

推荐答案

Errm,一旦我发布了这个问题,我突然意识到了我的错误-我通过从主线程调用函数来设置好级别,所以当然它们都有相同的线程ID(与在主线程的上下文中执行的线程ID相同). DOH !!!!

Errm, once I posted this question I suddenly realized my mistake - I set the nice level by calling the function from the main thread, so of course they all have the same thread ID (as it is executed in the context of the main thread). DOH!!!!

使用正确的提示,一切正常.

Using the correct tid everything works fine.

现在如何删除此问题?

这篇关于尼斯级别的pthreads?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆