我正在尝试使用SCHED_RR设置pthread的优先级,以便当较高优先级的任务准备运行时,较低优先级的任务将不会获得cpu时间。我的系统是Ubuntu 18.04.1 LTS配置(没有特殊的内核选项)。
在测试用例中,两个任务都实时共享,而高优先级任务已准备好运行。
该测试用例创建了两个线程,每个线程短暂地睡眠然后继续。低优先级线程尝试每秒创建文本输出。我的期望是,低优先级线程不产生任何输出,而高优先级线程准备运行几秒钟。输出显示实时正在共享。
即使我以root身份运行代码(即sudo ./ThreadTest),也会发生这种情况
#include <iostream>
#include <error.h>
#include <pthread.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
using namespace std;
// Thread function designed to use all realtime it can get intended to be started
// at high priority. Prints once per second if it can.
void * BackGroundThread(void *)
{
struct timespec now;
cout << "Low priority task start" << endl;
sleep(1);
clock_gettime(CLOCK_MONOTONIC, &now);
int32_t intStartSec = now.tv_sec;
for (int i = 0; i < 10; i++)
{
do
{
clock_gettime(CLOCK_MONOTONIC, &now);
} while (intStartSec == now.tv_sec); // Wait for second boundary.
cout << "Low priority task i( " << i << " ) " << " Time( " << now.tv_sec
<< ", " << now.tv_nsec << " )" << endl;
intStartSec = now.tv_sec;
}
cout << "Low priority task end." << endl;
return NULL;
}
// Thread function designed to use all realtime it can get intended to be started
// at high priority. Prints once per second.
void * HighPriorityThread(void *)
{
sleep(3);
cout << "High priority task start." << endl;
volatile int y = 0;
do
{
y++;
} while (y != 0); // Wait for second boundary.
cout << "High priority task end." << endl;
return NULL;
}
pthread_t * CreateRRThreadWithPriority(pthread_t *myThread, int priority,
void *(*start_routine)(void *))
{
int retVal = -1;
pthread_attr_t workingAttrib;
struct sched_param prioParam;
prioParam.sched_priority = priority;
retVal = pthread_attr_init(&workingAttrib);
if (retVal)
{
cout << "CreateRRThreadWithPriority: Failure from pthread_attr_init: "
<< strerror(retVal) << endl;
}
retVal = pthread_attr_setschedpolicy(&workingAttrib, SCHED_RR);
if (retVal)
{
cout
<< "CreateRRThreadWithPriority: Failure from thread_attr_setschedpolic: "
<< strerror(retVal) << endl;
}
retVal = pthread_attr_setschedparam(&workingAttrib, &prioParam);
if (retVal)
{
cout
<< "CreateRRThreadWithPriority: Failure from pthread_attr_setschedparam: "
<< strerror(retVal) << endl;
}
retVal = pthread_create(myThread, &workingAttrib, start_routine, NULL);
if (retVal)
{
cout << "CreateRRThreadWithPriority: Failure from pthread_create: "
<< strerror(retVal) << endl;
}
return myThread;
}
int main()
{
cout << "Thread Test" << endl; // prints Thread Test
cout << sched_get_priority_min(SCHED_RR) << endl;
cout << sched_get_priority_max(SCHED_RR) << endl;
pthread_t LP, HP;
CreateRRThreadWithPriority(&LP, sched_get_priority_min(SCHED_RR),
BackGroundThread);
CreateRRThreadWithPriority(&HP, sched_get_priority_max(SCHED_RR),
HighPriorityThread);
pthread_join(LP, NULL);
pthread_join(HP, NULL);
cout << "Complete" << endl;
return 0;
}
输出:
Thread Test 1 99 Low priority task start Low priority task i( 0 ) Time( 11093, 13 ) Low priority task i( 1 ) Time( 11094, 17 ) High priority task start. Low priority task i( 2 ) Time( 11095, 3 ) Low priority task i( 3 ) Time( 11096, 4 ) Low priority task i( 4 ) Time( 11097, 16 ) Low priority task i( 5 ) Time( 11098, 8 ) Low priority task i( 6 ) Time( 11099, 14 ) Low priority task i( 7 ) Time( 11100, 1 ) Low priority task i( 8 ) Time( 11101, 5 ) High priority task end. Low priority task i( 9 ) Time( 11102, 2 ) Low priority task end. Complete
其他信息: 考虑到低优先级任务能够运行,因为有多个内核,因此我将高优先级任务的数量增加到12个。低优先级任务继续运行,而同时运行了12个高优先级任务。我缺少什么?有内核设置吗?