进程中线程的最高优先级是多少?



>使用pthread_setschedparam API 创建优先级为 '-15' 的线程。但是失败的值 EINVAL(根据 MAN 页面:策略不是公认的策略,或者参数对策略没有意义。N

据我所知,进程的优先级可以调整为值(-19 到 20),但我不知道进程中线程的优先级范围是多少。需要专家帮助才能理解这一点。

class CRAII_Priority_Thread : public std::thread
{
std::thread mDSIRecvThread;
const int   mPolicy;
sched_param mSchParams;
public:
CRAII_Priority_Thread(std::thread&& thr,const int policy, const int priority)
: mDSIRecvThread{std::move(thr)}
, mPolicy{policy}
, mSchParams{priority}
{
sched_param currSchParams;
int currPolicy = 0;
if (pthread_getschedparam(mDSIRecvThread.native_handle(), &currPolicy, &currSchParams))
{
std::cout << "Failed to pthread_getschedparam: ERROR "  << std::strerror(errno) << "n";
}
std::cout << "Current configuration DSIThread ["<< mDSIRecvThread.get_id()
<< "] currPolicy [" << currPolicy << "] and PRIORITY ["
<< currSchParams.sched_priority << "]n";
std::cout << "Trying to set configuration as DSIThread  ["<< mDSIRecvThread.get_id()
<< "] currPolicy [" << mPolicy << "] and PRIORITY ["
<< mSchParams.sched_priority << "]n";
int iRet = -1;
if (iRet = pthread_setschedparam(mDSIRecvThread.native_handle(), mPolicy, &mSchParams))
{
switch(iRet)
{
case ESRCH:
std::cout << "No thread with the ID thread could be foundn";
break;
case EINVAL:
std::cout << "policy is not a recognized policy, or param does not make sense for the policy.n";
break;
case EPERM:
std::cout << "The caller does not have appropriate privileges to set the specified scheduling policy and parameters.n";
break;
case ENOTSUP:
std::cout << "attempt was made to set the policy or scheduling parameters to an unsupported valuen";
break;
default:
break;
}
std::cout << "Return value [" << iRet << "] Failed to pthread_setschedparam: ERROR "  << std::strerror(errno) << "n";
}
if (pthread_getschedparam(mDSIRecvThread.native_handle(), &currPolicy, &currSchParams))
{
std::cout << "Failed to pthread_getschedparam: ERROR "  << std::strerror(errno) << "n";
}
std::cout << "setconfiguration successfull current configuration  DSIThread ["<< mDSIRecvThread.get_id()
<< "] currPolicy [" << currPolicy << "] and PRIORITY ["
<< currSchParams.sched_priority << "]n";
}
~CRAII_Priority_Thread()
{
if (mDSIRecvThread.joinable())
{
mDSIRecvThread.join();
}
else
{
std::cout << "ERROR : Failed to join DSI recv threadn";
}
}
private:
sched_param sch_params;
};
void thread_function()
{
std::cout << __FUNCTION__ << "n";
}
int main()
{
CRAII_Priority_Thread(std::thread(&thread_function), 0, -15);
return 0;
}

收到如下所示的错误:

当前配置 DSIThread [140333652039424] 当前策略 [0] 和 优先级 [0]

尝试将配置设置为 DSIThread [140333652039424] currPolicy [0] 和优先级 [-15]

策略不是公认的策略,或者参数对 政策。thread_function

返回值 [22] 无法pthread_setschedparam:错误无效 论点

无法pthread_getschedparam:错误 参数无效 设置配置成功当前配置 DSIThread [140333652039424] 当前策略 [0] 和优先级 [0]

man sched_get_priority_max

int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);

sched_get_priority_max()返回可与策略标识的计划算法一起使用的最大优先级值。sched_get_priority_min()返回可与策略标识的计划算法一起使用的最低优先级值。支持的策略值为SCHED_FIFOSCHED_RRSCHED_OTHERSCHED_BATCHSCHED_IDLESCHED_DEADLINE


请注意,有效策略列表中未指定 0。

并非eeorika的答案中的所有内容都是正确的。sched_setscheduler的文档 ,说(除其他外)是这样的:

对于在某个正常调度策略(SCHED_OTHER、SCHED_IDLE SCHED_BATCH)下调度的线程,调度决策中不使用sched_priority(必须指定为 0)。

调度策略 0 对应于SCHED_OTHER,这就是您的呼叫失败的原因。

我可能是错的,但实际上似乎没有办法在没有root权限的情况下提高单个线程的优先级。 另请参阅如何提高 pthreads 中的线程优先级?。

最新更新