在下面的代码片段中,我创建了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线程优先级,行为不正常相关推荐

  1. linux内核线程优先级设置,Linux c 线程属性,线程优先级的修改

    Linux c 线程属性,线程优先级的修改 发布时间:2018-07-19 10:24, 浏览次数:1290 , 标签: Linux 线程属性的设置,网上找的文章总感觉不够全面,还是结合man手册查看 ...

  2. Linux 操作系统原理 — 进程与线程管理

    目录 文章目录 目录 前言 进程与线程 内核线程,用户线程与轻量级进程 内核线程 轻量级进程 用户线程 轻量级进程与用户线程的区别 用户线程与轻量级进程的混合模式 用户线程和内核线程的区别 线程的实现 ...

  3. Linux下查看进程和线程

    在linux中查看线程数的三种方法 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行显示一个进程. 2.ps xH 手册 ...

  4. Java线程之多线程与多进程(2)——线程优先级与线程安全

    线程优先级 现在主流操作系统(如Windows.Linux.Mac OS X)的任务调度除了具有前面提到的时间片轮转的特点外,还有优先级调度(Priority Schedule)的特点.优先级调度决定 ...

  5. Linux系统编程----15(线程与进程函数之间的对比,线程属性及其函数,线程属性控制流程,线程使用注意事项,线程库)

    对比 进程 线程 fork pthread_create exit (10) pthread_exit (void *) wait (int *) pthread_join (,void **)阻塞 ...

  6. linux进程和线程教程,Linux下查看进程和线程

    在Linux中查看线程数的三种方法 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行显示一个进程. 2.ps xH 手册 ...

  7. linux下查看进程的线程数,linux查看进程的线程数

    top -H -p $PID  #查看对应进程的那个线程占用CPU过高 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行 ...

  8. Linux进阶之进程与线程

    一.进程与线程 1.进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的 ...

  9. 【LWP,进程,线程】Linux

    [前情提要]: 1.在Linux下,没有线程这个概念,而在Windows系统下有线程. 2.Linux下,用进程来模拟线程(这种模拟出来的线程叫做轻量级进程,简称LWP). 3.在Linux中(在CP ...

最新文章

  1. Meta 发布 Bean Machine 帮助衡量 AI 模型的不确定性
  2. Maven 的这 7 个问题你思考过没有?
  3. 目标检测模型从训练到部署!
  4. 鍵盤彈出,頁面佈局被推上去了.....
  5. CodeForces - 641ELittle Artem and Time Machine——map+树状数组
  6. python程序代码解析_Python源码分析3 – 词法分析器PyTokenizer
  7. LeetCode 802. 找到最终的安全状态(逆向图+拓扑排序)
  8. ES2020的新特性解读
  9. WINDOWS SERVER 2008/2008 R2/2012 最大内存支持
  10. 关于FPGA核心bug解决
  11. android怎么做版本检测,android 实现检测版本,下载apk更新(附源码)
  12. U盘不识别量产工具解决方案
  13. 标准库std::min/std::max与min/max宏冲突
  14. 新周刊:放弃北上广的10个理由
  15. eNSP 路由远程登录
  16. GPA计算(5.0分制)
  17. 系统调用的内核实现,一文讲透open函数内核真实实现。
  18. Latex 中文简历 过程(更新Miktex和 修改utf字体)
  19. MyTT工作(一)ListView使用
  20. 自然可视图是什么意思?

热门文章

  1. 自然语言生成(NLG)的好处是什么,它如何影响BI?
  2. 如何合理的规划jvm性能调优
  3. 互联网下半场的角逐,玩转轻资产的大数据服务 | 阿里云栖开发者沙龙大数据专场(北京站)干货集锦
  4. 阿里开发者们的第5个感悟:听话,出活 1
  5. 五年,时间告诉我只有自己强大才是真的强大!
  6. NLP产品级系统设计模式
  7. ECS 按量付费VPC实例停机不收费FAQ
  8. Fortinet 荣膺谷歌云年度安全技术合作伙伴奖
  9. 如何用Chrome读懂网站监测Cookie
  10. 征战云时代,为什么安全是关键命题?