写了样品C++,与10个线程运行多线程程序,每个线程设置为高优先级和亲和力。在具有16个内核的dell机器上编译并运行此代码,运行centos 7(Linux内核-3.10.0-229),禁用超线程。之后我跑这个代码,在几秒钟内,我们的Linux机器变得反应迟钝,因为,如果我打开Eclipse编辑器,并保存文件或保存在vi编辑器文件中的应用程序挂起感。有趣的是,一旦我停止了这个程序/进程,那么所有其他应用程序就会从他们离开的地方恢复。此外,如果我从这10个线程中删除优先级,我不会看到此问题。Linux内核响应当在多个内核的多个高优先级线程运行
问题:
1)停止16个内核,6芯仍留在机器上(上述CPU使用率所示,CPU执行62.9%的用户空间,并且是空闲37.1%有趣的是0%的CPU。在内核空间中的使用),所以理想情况下内核应该使用这6个内核来处理其他应用程序,可能是其他应用程序无法运行的原因?如何在不引入睡眠/更改优先级的情况下解决此问题?
2)想知道比线程引入睡眠/等待一个事件(其引入最小的延迟由于内核上下文开关)来实现并行其他更好的方法?
然顶部命令(top -H):
%Cpu(s): 62.9 us, 0.0 sy, 0.0 ni, 37.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
1107 arun rt 0 96748 1112 932 R 99.9 0.0 0:25.78 PthreadTest
1115 arun rt 0 96748 1112 932 R 99.9 0.0 0:24.79 PthreadTest
1118 arun rt 0 96748 1112 932 R 99.9 0.0 0:22.79 PthreadTest
1120 arun rt 0 96748 1112 932 R 99.9 0.0 0:20.79 PthreadTest
1123 arun rt 0 96748 1112 932 R 99.9 0.0 0:18.79 PthreadTest
1117 arun rt 0 96748 1112 932 R 94.1 0.0 0:23.78 PthreadTest
1119 arun rt 0 96748 1112 932 R 94.1 0.0 0:21.78 PthreadTest
1122 arun rt 0 96748 1112 932 R 94.1 0.0 0:19.78 PthreadTest
1124 arun rt 0 96748 1112 932 R 94.1 0.0 0:17.78 PthreadTest
1125 arun rt 0 96748 1112 932 R 94.1 0.0 0:16.76 PthreadTest
代码下面:
#include
#include
#include
#include
using namespace std;
#define NUM_THREADS 10
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
cout << "Hello World! Thread ID, " << tid << endl;
while(true)
{
continue;
}
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
pthread_attr_t threads_attr[NUM_THREADS];
struct sched_param params;
params.sched_priority = sched_get_priority_max(SCHED_FIFO);
int rc;
int i;
int cpu_num = 0;
for(i=0; i < NUM_THREADS; i++){
cpu_set_t cpu;
CPU_ZERO(&cpu);
CPU_SET(cpu_num, &cpu);
cout << "main() : creating thread, " << i << "cpu_num : "<
pthread_attr_init(&threads_attr[i]);
pthread_attr_setscope(&threads_attr[i], PTHREAD_SCOPE_SYSTEM);
rc = pthread_create(&threads[i], NULL,
PrintHello, (void *)i);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
sleep(1);
int ret = pthread_setaffinity_np(threads[i], sizeof(cpu_set_t), &cpu);
if(ret == 0 && CPU_ISSET(cpu_num, &cpu))
{
cout << "Thread " << i << " affinity set " <
}
ret = pthread_setschedparam(threads[i], SCHED_FIFO, ¶ms);
if(ret == 0)
{
cout << "Thread " << i << " priority set " <
}
cpu_num++;
}
// free attribute and wait for the other threads
void *status;
for(i=0; i < NUM_THREADS; i++)
{
rc = pthread_join(threads[i], &status);
if (rc){
cout << "Error:unable to join," << rc << endl;
exit(-1);
}
cout << "Main: completed thread id :" << i ;
cout << " exiting with status :" << status << endl;
}
pthread_exit(NULL);
}
编译:
g++ -std=c++14 -g -o PthreadTest busywait.cpp -lpthread
2015-11-04
arun
+1
这个问题很有趣,但出于好奇,为什么你在高优先级运行这个东西?具有较高优先级的CPU进食线程是完全挂起低优先级计划的配方... –
+2
线程总是希望CPU应该有降低的优先级,如果任何事情。 –
+0
这台机器有哪些CPU?你为什么要用GCC/linux指定'-lpthread'? (正确的标志是'-pthread'。) –