linux降低线程优先级,Linux线程优先级,行为不正常
在下面的代码片段中,我创建了6个线程。各有不同的优先级。全局优先级数组中提到了优先级。我正在根据线索索引在每个线程内连续增加全局变量。如果线程优先级更高,我期待计数更高。但我的输出不遵循优先概念pl。请参阅下面显示的输出顺序。我在Ubuntu 16.04和Linux内核4.10上尝试了这一点。Linux线程优先级,行为不正常
O/P,
Thread=0
Thread=3
Thread=2
Thread=5
Thread=1
Thread=4
pid=32155 count=4522138740
pid=32155 count=4509082289
pid=32155 count=4535088439
pid=32155 count=4517943246
pid=32155 count=4522643905
pid=32155 count=4519640181
代码:
#include
#include
#define FAILURE -1
#define MAX_THREADS 15
long int global_count[MAX_THREADS];
/* priority of each thread */
long int priority[]={1,20,40,60,80,99};
void clearGlobalCounts()
{
int i=0;
for(i=0;i
global_count[i]=0;
}
/**
thread parameter is thread index
**/
void funcDoNothing(void *threadArgument)
{
int count=0;
int index = *((int *)threadArgument);
printf("Thread=%d\n",index);
clearGlobalCounts();
while(1)
{
count++;
if(count==100)
{
global_count[index]++;
count=0;
}
}
}
int main()
{
int i=0;
for(int i=0;i
create_thread(funcDoNothing, i,priority[i]);
sleep(3600);
for(i=0;i
{
printf("pid=%d count=%ld\n",getpid(),
global_count[i]);
}
}
create_thread(void *func,int thread_index,int priority)
{
pthread_attr_t attr;
struct sched_param schedParam;
void *pParm=NULL;
int id;
int * index = malloc(sizeof(int));
*index = thread_index;
void *res;
/* Initialize the thread attributes */
if (pthread_attr_init(&attr))
{
printf("Failed to initialize thread attrs\n");
return FAILURE;
}
if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO))
{
printf("Failed to pthread_attr_setschedpolicy\n");
return FAILURE;
}
if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO))
{
printf("Failed to setschedpolicy\n");
return FAILURE;
}
/* Set the capture thread priority */
pthread_attr_getschedparam(&attr, &schedParam);;
schedParam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
schedParam.sched_priority = priority;
if (pthread_attr_setschedparam(&attr, &schedParam))
{
printf("Failed to setschedparam\n");
return FAILURE;
}
pthread_create(&id, &attr, (void *)func, index);
}
linux降低线程优先级,Linux线程优先级,行为不正常相关推荐
- linux内核线程优先级设置,Linux c 线程属性,线程优先级的修改
Linux c 线程属性,线程优先级的修改 发布时间:2018-07-19 10:24, 浏览次数:1290 , 标签: Linux 线程属性的设置,网上找的文章总感觉不够全面,还是结合man手册查看 ...
- Linux 操作系统原理 — 进程与线程管理
目录 文章目录 目录 前言 进程与线程 内核线程,用户线程与轻量级进程 内核线程 轻量级进程 用户线程 轻量级进程与用户线程的区别 用户线程与轻量级进程的混合模式 用户线程和内核线程的区别 线程的实现 ...
- Linux下查看进程和线程
在linux中查看线程数的三种方法 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行显示一个进程. 2.ps xH 手册 ...
- Java线程之多线程与多进程(2)——线程优先级与线程安全
线程优先级 现在主流操作系统(如Windows.Linux.Mac OS X)的任务调度除了具有前面提到的时间片轮转的特点外,还有优先级调度(Priority Schedule)的特点.优先级调度决定 ...
- Linux系统编程----15(线程与进程函数之间的对比,线程属性及其函数,线程属性控制流程,线程使用注意事项,线程库)
对比 进程 线程 fork pthread_create exit (10) pthread_exit (void *) wait (int *) pthread_join (,void **)阻塞 ...
- linux进程和线程教程,Linux下查看进程和线程
在Linux中查看线程数的三种方法 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行显示一个进程. 2.ps xH 手册 ...
- linux下查看进程的线程数,linux查看进程的线程数
top -H -p $PID #查看对应进程的那个线程占用CPU过高 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行 ...
- Linux进阶之进程与线程
一.进程与线程 1.进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的 ...
- 【LWP,进程,线程】Linux
[前情提要]: 1.在Linux下,没有线程这个概念,而在Windows系统下有线程. 2.Linux下,用进程来模拟线程(这种模拟出来的线程叫做轻量级进程,简称LWP). 3.在Linux中(在CP ...
最新文章
- Meta 发布 Bean Machine 帮助衡量 AI 模型的不确定性
- Maven 的这 7 个问题你思考过没有?
- 目标检测模型从训练到部署!
- 鍵盤彈出,頁面佈局被推上去了.....
- CodeForces - 641ELittle Artem and Time Machine——map+树状数组
- python程序代码解析_Python源码分析3 – 词法分析器PyTokenizer
- LeetCode 802. 找到最终的安全状态(逆向图+拓扑排序)
- ES2020的新特性解读
- WINDOWS SERVER 2008/2008 R2/2012 最大内存支持
- 关于FPGA核心bug解决
- android怎么做版本检测,android 实现检测版本,下载apk更新(附源码)
- U盘不识别量产工具解决方案
- 标准库std::min/std::max与min/max宏冲突
- 新周刊:放弃北上广的10个理由
- eNSP 路由远程登录
- GPA计算(5.0分制)
- 系统调用的内核实现,一文讲透open函数内核真实实现。
- Latex 中文简历 过程(更新Miktex和 修改utf字体)
- MyTT工作(一)ListView使用
- 自然可视图是什么意思?