知识回忆
基础知识
Linux内核当中有3种调度策略: .
SCHED_ OTHER分时调度策略;(普通进程)
SCHED_ FIFO 实时调度策略,先到先服务;
SCHED RR实时调度策略,时间片轮转。
备注:如果有相同优先级的实时进程(根据优先级计算的调度权值是一样的)已经准备好,FIFO时必须等待该进程主
动放弃之后才可以运行这个优先级相同的任务。而RR可以每个任务都执行一段时间。
线程创建的时候默认是othre 如果所有任务都是用分时调度策略的时候必须要指定优先级nice
2、获取线程设置的最高和最低优先级函数如下:
int sched get .priorit_ max(int policy); 获取实时优先级的最大值
int sched. .get priority min(int policy); I获取实时优先级的最小值
SCHED OTHER它不支持优先级使用,而SCHED RR/SCHED FIFO支持优先级使用,它们分析为1-99, 数值越大优
先级越高。
实时调度策略(SCHED_ FIFO/SCHED_ RR) 优先级最大值为99;
普通调度策略(SCHED_ NORMAL/SCHED_ BATCH/SCHED_ _IDLE) ,始终返回0,即普通任务调度的函数。
设置调度策略获取优先级的实战
文章来源:https://www.toymoban.com/news/detail-515386.html
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
//获取线程的调度策略
static int get_thread_policy(pthread_attr_t *attr)
{
int plicy;
int rs=pthread_attr_getschedpolicy(attr,&plicy);
assert(rs==0);
switch(plicy)
{
case SCHED_FIFO:
printf("policy=SCHED_FIFO.\n");
break;
case SCHED_RR:
printf("policy=SCHED_RR.\n");
break;
case SCHED_OTHER:
printf("policy=SCHED_OTHER.\n");
break;
default:
printf("policy=UNKNOWN.\n");
break;
}
return plicy;
}
//显示线程的实时优先级最大最小,如果不是实时进程其实没有优先级
static void 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 get_thread_priority(pthread_attr_t *attr)
{
struct sched_param param;
int rs=pthread_attr_getschedparam(attr,¶m);
assert(rs==0);
printf("priority=%d",param.__sched_priority);
return param.__sched_priority;
}
//设置进程调度策略
static void set_thread_policy(pthread_attr_t *attr,int policy)
{
int rs=pthread_attr_setschedpolicy(attr,policy);
assert(0==rs);
get_thread_policy(attr);
}
int main()
{
pthread_attr_t attr;
struct sched_param sched;
int rs=pthread_attr_init(&attr);
assert(0==rs);
int plicy=get_thread_policy(&attr);
printf("output current configuration of priority.\n");
show_thread_priority(&attr,plicy);
printf("output SCHED_FIFO of priority.\n");
show_thread_priority(&attr,SCHED_FIFO);
printf("output SCHED_RR of priority.\n");
show_thread_priority(&attr,SCHED_RR);
printf("output priority of current thread.\n");
int priority=get_thread_priority(&attr);
printf("set thrad policy.\n");
printf("set SCHED_FIFO polity.\n");
set_thread_policy(&attr,SCHED_FIFO);
printf("set SCHED_RR policy.\n");
set_thread_policy(&attr,SCHED_RR);
printf("restore current policy.\n");
set_thread_policy(&attr,plicy);
rs=pthread_attr_destroy(&attr);
assert(0==rs);
return 0;
}
创建线程设置线程实时调度属性,更改实战
文章来源地址https://www.toymoban.com/news/detail-515386.html
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void threadfunc1()
{
sleep(1);
int policy;
struct sched_param praram;
pthread_getschedparam(pthread_self(),&policy,&praram);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER.\n");
if(policy==SCHED_RR)
;
printf("SCHED_RR 1.\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO.\n");
for(int i=1;i<=10;i++)
{
for(int j=1;j<4000000;j++){
}
printf("Threadfunc1.\n");
}
printf("pthreadfunc1 EXIT.\n");
}
void threadfunc2()
{
sleep(1);
int policy;
struct sched_param praram;
pthread_getschedparam(pthread_self(),&policy,&praram);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER.\n");
if(policy==SCHED_RR)
;
printf("SCHED_RR 1.\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO.\n");
for(int i=1;i<=10;i++)
{
for(int j=1;j<4000000;j++){
}
printf("Threadfunc2.\n");
}
printf("pthreadfunc2 EXIT.\n");
}
void threadfunc3()
{
sleep(1);
int policy;
struct sched_param praram;
pthread_getschedparam(pthread_self(),&policy,&praram);
if(policy==SCHED_OTHER)
printf("SCHED_OTHER.\n");
if(policy==SCHED_RR)
;
printf("SCHED_RR 1.\n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO.\n");
for(int i=1;i<=10;i++)
{
for(int j=1;j<4000000;j++){
}
printf("Threadfunc3.\n");
}
printf("pthreadfunc3 EXIT.\n");
}
int main()
{
int i;
i=getuid();
if(i==0)
printf("the current user is root.\n");
else
printf("the current user is not root.\n");
pthread_t ppid1,ppid2,ppid3;
struct sched_param param;
pthread_attr_t attr1,attr2,attr3;
pthread_attr_init(&attr2);
pthread_attr_init(&attr1);
pthread_attr_init(&attr3);
param.sched_priority=51;
//线程3使用了实时属性,设置优先级51
pthread_attr_setschedpolicy(&attr3,SCHED_RR);
pthread_attr_setschedparam(&attr3,¶m);
pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);
//线程2使用了实时属性,设置优先级22
param.sched_priority=22;
pthread_attr_setschedpolicy(&attr2,SCHED_RR);
pthread_attr_setschedparam(&attr2,¶m);
pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);
pthread_create(&ppid3,&attr1,(void*)threadfunc3,NULL);
pthread_create(&ppid2,&attr2,(void*)threadfunc2,NULL);
pthread_create(&ppid1,&attr3,(void*)threadfunc1,NULL);
pthread_join(ppid3,NULL);
pthread_join(ppid2,NULL);
pthread_join(ppid1,NULL);
pthread_attr_destroy(&attr3);
pthread_attr_destroy(&attr2);
pthread_attr_destroy(&attr1);
return 0;
}
到了这里,关于02_04_02实时调度类_线程优先级代码实战的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!