-
2021-09-08 11:45:26
原文地址:https://blog.csdn.net/m0_37329910/article/details/100765833
相关文章
1、linux修改线程优先级----https://blog.csdn.net/null_plus/article/details/78444649
2、linux中pthread_join()与pthread_detach()详解----https://blog.csdn.net/weibo1230123/article/details/81410241
3、setpriority 用法----https://blog.csdn.net/nullzxy/article/details/9278495
更多相关内容 -
Linux线程优先级设置
2022-06-06 10:35:321.SCHED_OTHER 分时调度策略2.SCHED_FIFO 实时调度策略,先到...首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义: int sched_get_priority_max(int policy);Linux内核的三种调度策略:
1.SCHED_OTHER 分时调度策略
2.SCHED_FIFO 实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃
3.SCHED_RR实 时调度策略,时间片轮转。当进程的时间片用完,系统将重新分配时间片,并置于就绪队列尾。放在队列尾保证了所有具有相同优先级的RR任务的调度公平。
Linux线程优先级设置:
首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义:
int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);
注意:SCHED_OTHER 是不支持优先级使用的,而 SCHED_FIFO 和 SCHED_RR 支持优先级的使用,他们分别为1和99,数值越大优先级越高。
设置和获取优先级通过以下两个函数:
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); param.sched_priority = 51; //设置优先级
系统创建线程时,默认的线程是 SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现。
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
上面的param使用了下面的这个数据结构:
struct sched_param {
int __sched_priority; // 所要设定的线程优先级
};
我们可以通过下面的测试程序来说明,我们自己使用的系统的支持的优先级:
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
static int api_get_thread_policy (pthread_attr_t *attr)
{
int policy;
int rs = pthread_attr_getschedpolicy (attr, &policy);
assert (rs == 0);
switch (policy)
{
case SCHED_FIFO:
printf ("policy = SCHED_FIFO\n");
break;
case SCHED_RR:
printf ("policy = SCHED_RR");
break;
case SCHED_OTHER:
printf ("policy = SCHED_OTHER\n");
break;
default:
printf ("policy = UNKNOWN\n");
break;
}
return policy;
}
static void api_show_thread_priority (pthread_attr_t *attr,int policy)
{
int priority = sched_get_priority_max (policy);
assert (priority != -1);
printf ("max_priority = %d\n", priority);
priority = sched_get_priority_min (policy);
assert (priority != -1);
printf ("min_priority = %d\n", priority);
}
static int api_get_thread_priority (pthread_attr_t *attr)
{
struct sched_param param;
int rs = pthread_attr_getschedparam (attr, ¶m);
assert (rs == 0);
printf ("priority = %d\n", param.__sched_priority);
return param.__sched_priority;
}
static void api_set_thread_policy (pthread_attr_t *attr,int policy)
{
int rs = pthread_attr_setschedpolicy (attr, policy);
assert (rs == 0);
api_get_thread_policy (attr);
}
int main(void)
{
pthread_attr_t attr; // 线程属性
struct sched_param sched; // 调度策略
int rs;
/*
* 对线程属性初始化
* 初始化完成以后,pthread_attr_t 结构所包含的结构体
* 就是操作系统实现支持的所有线程属性的默认值
*/
rs = pthread_attr_init (&attr);
assert (rs == 0); // 如果 rs 不等于 0,程序 abort() 退出
/* 获得当前调度策略 */
int policy = api_get_thread_policy (&attr);
/* 显示当前调度策略的线程优先级范围 */
printf ("Show current configuration of priority\n");
api_show_thread_priority(&attr, policy);
/* 获取 SCHED_FIFO 策略下的线程优先级范围 */
printf ("show SCHED_FIFO of priority\n");
api_show_thread_priority(&attr, SCHED_FIFO);
/* 获取 SCHED_RR 策略下的线程优先级范围 */
printf ("show SCHED_RR of priority\n");
api_show_thread_priority(&attr, SCHED_RR);
/* 显示当前线程的优先级 */
printf ("show priority of current thread\n");
int priority = api_get_thread_priority (&attr);
/* 手动设置调度策略 */
printf ("Set thread policy\n");
printf ("set SCHED_FIFO policy\n");
api_set_thread_policy(&attr, SCHED_FIFO);
printf ("set SCHED_RR policy\n");
api_set_thread_policy(&attr, SCHED_RR);
/* 还原之前的策略 */
printf ("Restore current policy\n");
api_set_thread_policy (&attr, policy);
/*
* 反初始化 pthread_attr_t 结构
* 如果 pthread_attr_init 的实现对属性对象的内存空间是动态分配的,
* phread_attr_destory 就会释放该内存空间
*/
rs = pthread_attr_destroy (&attr);
assert (rs == 0);
return 0;
}
-
Linux 线程优先级设置(内含C语言版线程创建、绑定CPU和优先级设置代码)
2021-07-02 11:50:21参考链接: https://blog.csdn.net/wushuomin/article/details/80051295 //详细...//线程优先级设定 1 线程创建 线程创建使用的函数接口为:pthread_create() 函数原型如下: #include <pthread.h> int pthrea参考链接:
https://blog.csdn.net/wushuomin/article/details/80051295 //详细讲解pthread_create 函数
https://blog.csdn.net/heybeaman/article/details/90896663 //讲解pthread_join的使用
https://blog.csdn.net/tiger_ibm/article/details/7932386 //线程优先级设定1 线程创建
线程创建使用的函数接口为:pthread_create()
函数原型如下:#include <pthread.h> int pthread_create( pthread_t *restrict tidp, //新创建的线程ID指向的内存单元。 const pthread_attr_t *restrict attr, //线程属性,默认为NULL void *(*start_rtn)(void *), //新创建的线程运行的函数 void *restrict arg //默认为NULL。如果线程运行的函数需要参数,将参数放入结构中并将地址作为arg传入。 );
2 线程优先级设定
线程优先级可以由线程属性控制,linux 内核提供了很多函数操作 pthread_attr_t 结构体,修改后将该结构体 通过 函数pthread_create 的第二个参数传递给线程即可。
//线程调度策略设置和获取函数,pthread_attr_t 为线程属性, policy 就是 线程调度策略 int pthread_attr_setschedpolicy(pthread_attr_*, int policy) int pthread_attr_getschedpolicy(const pthread_attr_t *, int * policy)
SCHED_OTHER(0):分时调度策略,线程默认调度策略,不区分优先级,该调度方式通过分时来完成的。当线程处于这种调度策略的时候,对线程进行优先级设置会失败。但高优先级的线程可以抢占处于该调度策略的线程。
SCHED_FIFO(1):实时调度策略,先进先出原则,这种调度方式有优先级之分,并且无时间片概念,处于该调度策略时,高优先级的进程将会一直占用CPU 直到有更高优先级的线程出现,将线程设置为该调度策略的时候需要超级用户模式。
SCHED_RR(2):实时调度策略,在SCHED_FIFO的基础上加入了时间片。于FIFO 不同,FIFO 即使有相同优先级的线程,也必须等到当前线程运行完毕后才能执行,RR可以使 相同优先级的线程都运行一段时间。//线程优先级设置和获取函数 int pthread_attr_setschedparam(pthread_attr_t *,const struct sched_param *); int pthread_attr_getschedparam(const pthread_attr_t *,struct sched_param *);
结构体 sched_param 中 只有一项 __sched_priority 代表优先级,值越大优先级越高。
//获取系统调度策略的最大最小优先级 int sched_get_priority_max( int policy ); int sched_get_priority_min( int policy );
//获取和设置线程的继承性 默认是 PTHREAD_INHERIT_SCHED int pthread_attr_getinheritsched(const pthread_attr_t * attr,int *inheritsched); int pthread_attr_setinheritsched(pthread_attr_t * attr,int inheritsched);
3 线程绑定CPU
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,const cpu_set_t *cpuset); int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset); 从函数名以及参数名都很明了,唯一需要点解释下的可能就是cpu_set_t这个结构体了。这个结构体的理解类似于select中的fd_set,可以理解为cpu集,也是通过约定好的宏来进行清除、设置以及判断: //初始化,设为空 void CPU_ZERO (cpu_set_t *set); //将某个cpu加入cpu集中 void CPU_SET (int cpu, cpu_set_t *set); //将某个cpu从cpu集中移出 void CPU_CLR (int cpu, cpu_set_t *set); //判断某个cpu是否已在cpu集中设置了 int CPU_ISSET (int cpu, const cpu_set_t *set);
代码如下:
#define _GNU_SOURCE #include <stdio.h> #include <math.h> #include <pthread.h> #include <math.h> #include <sys/time.h> #include <unistd.h> #include <sys/wait.h> #include <sched.h> #include <string.h> #include <assert.h> #include <stdlib.h> void *myThread1(void){ while(1) { printf("This is the 1th\n"); sleep(1); } } void *myThread2(void){ while(1) { printf("This is the 2th pthread\n"); sleep(1); } } void *myThread3(void) { struct timeval tv; while(1){ gettimeofday(&tv,NULL); printf("change priority befor millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); sleep(1); } } void myThread4(void) { struct timeval tv; while(1){ gettimeofday(&tv,NULL); printf("change priority after millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); sleep(1); } } void *thread_func(void *arg){ cpu_set_t cpuset; int policy, ret; struct sched_param param; //获取线程调度参数 ret = pthread_getschedparam(pthread_self(), &policy, ¶m); if(ret!=0){ printf("pthread_getschedparam %s\n", strerror(ret) ); exit(1); } if (policy == SCHED_FIFO){ printf("policy:SCHED_FIFO\n"); } else if (policy == SCHED_OTHER){ printf("policy:SCHED_OTHER\n"); } else if (policy == SCHED_RR){ printf("policy:SCHED_RR\n"); } printf("thread_func priority is %d\n", param.sched_priority); CPU_ZERO(&cpuset); CPU_SET(1, &cpuset); /* cpu 1 is in cpuset now */ /* bind process to processor 1 */ if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) !=0) { perror("pthread_setaffinity_np"); } printf("Core 1 is running! this pthread PID is %d\n",gettid()); myThread4(); } int main(int argc, char *argv[]){ pthread_t my_thread; time_t startwtime, endwtime; pthread_t id1,id2,id3; int ret = 0; pthread_attr_t attr; struct sched_param sp; int policy ,inher; int rs; bzero((void*)&sp,sizeof(sp)); pid_t pid = gettid(); pthread_t tid = pthread_self(); printf("main: pid=%d, tid=%lu\n", pid, tid); /* 在默认情况下通过pthread_create函数创建的线程是非分离属性的, * 由pthread_create函数的第二个参数决定, * 在非分离的情况下,当一个线程结束的时候, * 它所占用的系统资源并没有完全真正的释放,也没有真正终止。 * 只有在pthread_join函数返回时,该线程才会释放自己的资源。 * 或者是设置在分离属性的情况下,一个线程结束会立即释放它所占用的资源。 * pthread_join()函数会一直阻塞调用线程,直到指定的线程终止。 * 当pthread_join()返回之后,应用程序可回收与已终止线程关联的任何数据存储空间。 * 但是,同时需要注意,一定要和上面创建的某一线程配套使用,这样还可以起到互斥的作用。 * 否则多线程可能抢占CPU资源,导致运行结果不确定。 */ rs = pthread_attr_init(&attr); assert(rs == 0); //获取继承的调度策略 ret = pthread_attr_getinheritsched(&attr, &inher); if (ret!=0) { printf("pthread_attr_getinheritsched\n%s\n", strerror(ret)); exit(1); } // if (inher == PTHREAD_EXPLICIT_SCHED) { printf("PTHREAD_EXPLICIT_SCHED\n"); } else if (inher == PTHREAD_INHERIT_SCHED) { printf("PTHREAD_INHERIT_SCHED\n"); inher = PTHREAD_EXPLICIT_SCHED; } //设置继承的调度策略 //必需设置inher的属性为 PTHREAD_EXPLICIT_SCHED,否则设置线程的优先级会被忽略 ret = pthread_attr_setinheritsched(&attr, inher); if (ret!=0) { printf("pthread_attr_setinheritsched\n%s\n", strerror(ret)); exit(1); } policy = SCHED_RR; //需要超级用户权限 pthread_attr_setschedpolicy( &attr, policy );//设置 调度策略为FIFO assert( rs == 0 ); const int priority = 51; //设置优先级 为51 sp.__sched_priority = priority; //if(0 == pthread_setschedparam(pthread_self(),policy,&sp)){ if(pthread_attr_setschedparam(&attr,&sp) != 0){//设置优先级 printf("pthread set sched priority failed\n"); } if (pthread_create(&my_thread, &attr, thread_func,NULL) != 0) { perror("pthread_create failed\n"); } ret = pthread_create(&id1,NULL,(void*)myThread1,NULL); if(ret){ printf("cread pthread1 failed\n "); return -1; } ret = pthread_create(&id2,NULL,(void*)myThread2,NULL); if(ret){ printf("cread pthread2 failed\n "); return -1; } ret = pthread_create(&id3,NULL,(void*)myThread3,NULL); if(ret){ printf("cread pthread3 failed\n "); return -1; } pthread_join(my_thread,NULL); pthread_join(id1,NULL); pthread_join(id2,NULL); pthread_join(id3,NULL); return 0; }
编译过程中需要注意,编译的时候需要超级用户模式,否则优先级设定会出错。并且需要链接库pthread
即添加 -lpthread.编译结果:
绑定内核结果:
可以看到线程 3912 运行于CPU1 绑定运行在CPU1 核上。
优先级:调度策略为policy 2 就是 SCHED_RR,优先级为prio 这里显示为 48 上图显示为-52 不是很明白为什么会这么显示,但上面 top 命令下的PR 就是priority 动态调度优先级,取值范围是[-100,39],值越小优先级越高。
-
【Linux 内核】进程优先级与调度策略 ③ ( 设置、获取线程优先级的核心函数 | 修改线程调度策略函数 )
2022-04-04 10:35:40一、设置、获取线程优先级的核心函数、 二、修改线程调度策略函数
一、设置、获取线程优先级的核心函数
设置、获取 线程 优先级的 核心 函数 :
① 设置 " 创建线程 " 的优先级 :
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
② 获取 " 创建线程 " 的优先级 :
int pthread_attr_getschedparam(pthread_attr_t *attr, const struct sched_param *param)
设置
struct sched_param *param
结构体的sched_priority
字段 , 即可设置 " 优先级 " 属性 ;
上述 2 2 2 个函数 , 如果执行成功 , 返回 0 0 0 ;
如果执行失败 , 则返回错误代码 :
- EINVAL : 属性设置无效 ;
- ENOTSUP : 设置的属性值不合法 ;
二、修改线程调度策略函数
创建
pthread
线程时 , 默认的线程时 SCHED_OTHHER 调度策略 , 可以通过下面的函数 , 修改调度策略 ;修改线程 " 调度策略 " 函数 :
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
如果函数执行成功 , 返回 0 0 0 ;
如果函数执行失败 , 返回错误代码 :
- EINVALEINVAL :
pthread_attr_t *attr
线程未初始化 , 或者 ,int policy
不是有效调度策略 ; - EFAULTEFAULT :
pthread_attr_t *attr
指针无效 ; - ENOTSUPENOTSUP : 尝试将调度策略修改为 SCHED_FIFO 或 SCHED_RR 实时调度策略 ;
-
Linux c 线程属性,线程优先级的修改
2021-05-10 22:34:16Linux c 线程属性,线程优先级的修改发布时间:2018-07-19 10:24,浏览次数:1290, 标签:Linux线程属性的设置,网上找的文章总感觉不够全面,还是结合man手册查看。线程属性设置,分两个方式,一种是在创建之前,... -
【Linux 内核】线程调度示例一 ③ ( 获取线程优先级 | 设置线程调度策略 | 代码示例 )
2022-04-05 20:08:12一、获取线程优先级、 1、pthread_attr_setschedparam 和 pthread_attr_getschedparam 函数、 2、获取线程优先级代码示例、 二、设置线程调度策略、 1、pthread_attr_setschedpolicy 函数、 2、设置线程调度策略代码... -
Linux编程-线程优先级的设定
2021-05-11 14:59:34对于线程的优先级设定,在网上也看了不少的文章,大多数都只介绍了一个线程,关键是介绍的例程,设置的线程优先级都不起作用。由于之前接触的Linux编程知识比较少,这个问题困扰了我一晚上。于是接着在网上看资料,... -
Linux线程优先级
2021-03-04 10:16:07Linux线程优先级范围 Linux定义线程优先级范围在头文件<linux/sched.h>/* *Priorityofaprocessgoesfrom0..M... -
C++线程优先级SetThreadPriority的使用实例
2020-09-04 03:18:42主要介绍了C++线程优先级SetThreadPriority的使用实例,较为详细的讲述了C++线程及其优先级的用法,需要的朋友可以参考下 -
linux下设置线程优先级
2009-09-11 21:21:00在linux下我们可以通过int pthread_create(pthread_t *thread, const pthread_...来创建线程,但是如何设置线程的优先级呢?在讨论这个问题的时候,我们先要确定当前线程使用的调度策略,posix提供了int pthread_attr_ -
Linux:设置“线程”优先级
2021-05-15 04:14:46原文: blog.csdn.net/typhoonzb/article/details/4544278在linux下我们可以通过SCHED_FIFO, SCHED_RR 和 SCHED_OTHER。我们可以使用是不支持优先级使用的,而优先级越高。 从上面的结果我们可以看出,如果程序控制... -
Linux线程优先级范围
2022-06-06 16:54:14Linux定义线程优先级范围在头文件 Linux线程调度策略 Linux定义线程调度策略在头文件 SCHED_NORMAL即为SCHED_OTHER:采用分时调度策略。SCHED_FIFO:采用实时调度策略,且先到先服务,一直运行直到有更高... -
Linux线程属性及优先级设置
2021-05-23 08:20:50POSIX.1线程属性及优先级设置By zieckeyAll Right Reserved线程的属性由pthread_attr_t结构类型表示。在使用pthread_attr_t之前,需要调用pthread_attr_init对其初始化。pthread_attr_init为pthread_attr_t结构里面... -
关于linux下进程和线程优先级的一些总结
2020-12-25 16:42:30Linux的进程分普通进程(非实时进程)和实时进程, 进程 调度策略 优先级 说明 普通进程 SCHED_OTHER或SCHED_NORMAL 100-139 这个区间的优先级又称为静态优先级,不会随着时间而改变,内核不会修改它,只能... -
如何在linux中设置线程的优先级
2018-10-18 11:15:54在linux下我们可以通过 int pthread_create(pthread_t *thread, ...来创建线程,但是如何设置线程的优先级呢? 在讨论这个问题的时候,我们先要确定当前线程使用的调度策略,posix提供了 int pt... -
Linux-pthread设置线程优先级
2021-01-19 15:29:19设置线程优先级的函数: int pthread_setschedparam(pthread_t target_thread, int policy, const struct sched_param *param) 它主要用于设置线程的调用策略和优先级。 参数说明: 1. target_thread是使用... -
linux下设置线程优先级 http://blog.csdn.net/typhoonzb/article/details/4544278
2015-11-15 22:39:13在linux下我们可以通过 int pthread_create(pthread_t *thread, const pthread_...来创建线程,但是如何设置线程的优先级呢? 在讨论这个问题的时候,我们先要确定当前线程使用的调度策略,posix提供了 int pthread_a -
Android 中设置线程优先级的正确方式(2种方法)
2020-12-03 16:52:44Android 中设置线程优先级的正确方式(2种方法) 在 Android 中,有两种常见的设置线程优先级的方式: 第一种,使用 Thread 类实例的 setPriority 方法,来设置线程优先级。 第二种,使用 Process 类的 ... -
linux线程优先级设置
2017-03-24 09:09:26Linux线程优先级 Linux内核的三种调度策略: 1.SCHED_OTHER 分时调度策略 2.SCHED_FIFO 实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃 3.SCHED_... -
Linux-pthread如何设置线程的优先级
2020-11-17 08:49:41设置线程优先级的函数: int pthread_setschedparam( pthread_t target_thread, int policy, const struct sched_param *param ) 它主要用于设置线程的调用策略和优先级。 参数说明: target_... -
linux中设置线程优先级_如何在Linux中设置交换优先级
2020-07-27 05:43:08linux中设置线程优先级How to set swap priority in Linux? Can I use 2 swap partition at the same time? 如何在Linux中设置交换优先级? 我可以同时使用2个交换分区吗? You can set the priority of swap in ... -
线程优先级,设置,setPriority()方法
2021-05-24 01:00:48/*** @author xingsir* 线程优先级* 线程启动后纳入到线程调度,线程时刻处于被动获取CPU时间片而无法主动获取。我们可以通过调整线程的优先级来最大程度的干涉线程调度分配时间片的几率。* 理论上优先级越高的线程... -
Linux内核线程优先级设置的方法介绍
2021-05-10 22:34:04调度策略有三种:1.SCHED_NORMAL 非实时调度策略,默认情况下是100~139,由nice值决定;2.SCHED_FIFO实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃3.SCHED_RR实时... -
linux修改线程优先级
2017-11-04 17:10:54【转】 http://blog.chinaunix.net/uid-20788636-id-1841334.html ...Linux内核的三种调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高 -
查看线程优先级
2022-02-11 16:34:47linux下查看系统进程优先级命令,Linux 进程优先级查看及调整_恒大名宿王上源的博客-CSDN博客 linux线程优先级和nice值,如何使用nice和renice命令设置Linux进程优先级_机器人之路的博客-CSDN博客 htop -
Linux 线程调度策略与线程优先级
2021-05-14 11:37:00简单地说,如果系统使用这种调度策略,程序将无法设置线程的优先级。请注意,这种调度策略也是抢占式的,当高优先级的线程准备运行的时候,当前线程将被抢占并进入等待队列。这种调度策略仅仅决定线程在可运行线程... -
Linux 线程调度与优先级
2022-06-01 09:26:26【转】Linux下线程学习笔记(一)-frankzfz-ChinaUnix博客Linux下线程的调度策略与优先级(一)-frankzfz-ChinaUnix博客Linux内核的三种调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略,...