Linux pthread 多线程库学习 之线程调度策略

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/* Return in *POLICY the scheduling policy of *ATTR. */ extern int pthread_attr_getschedpolicy (__const pthread_attr_t *__restrict
__attr, int *__restrict __policy)
/* Set scheduling policy in *ATTR according to POLICY. */ extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)
/* Return in *INHERIT the scheduling inheritance mode of *ATTR. */ extern int pthread_attr_getinheritsched (__const pthread_attr_t *__restrict
int __inherit)
线程调度程序示例!
/*
* pthread_policy_exp.c
*
* Created on: 2010-12-23
*
Author: banxi1988
*/
#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<unistd.h>
max_priority = sched_get_priority_max(SCHED_OTHER); min_priority = sched_get_priority_min(SCHED_OTHER);
printf("The max priority of SCHED_OTHER is %d,the min is %d \n",max_priority,min_priority);
char *message = "Hello,wBaidu Nhomakorabearld!"; int thread_finished = 0;
void *hello(char *hello){ printf("thread hello is running ,Argument was: %s\n",hello); sleep(4); printf("Second thread setting finished flag ,and exiting now\n"); thread_finished = 1; pthread_exit(0);
ret = pthread_attr_getschedparam(&attr,&scheduling_value); if(ret){
perror("Get schedpolicy failed!\n"); exit(EXIT_FAILURE); }else{ printf("the priority of current thread is %d !\n",scheduling_value.__sched_priority); }
部分 API
1. 设置指定的线程调度属性 API 如下: /* Functions for scheduling control. */ 线程调度函数. /* Set the scheduling parameters for TARGET_THREAD according to POLICY
and *PARAM. */ extern int pthread_setschedparam (pthread_t __target_thread, int __policy, __const struct sched_param *__param) ;
ret = pthread_attr_init(&attr); if(ret){
perror("pthread_attr_init failed !\n"); exit(EXIT_FAILURE); } printf("the father id is %u\n",pthread_self());
ret = pthread_attr_setschedpolicy(&attr,SCHED_OTHER); if(ret){
}
int main(int argc,char **argv){ pthread_t t1; int ret ; int state; int max_priority,min_priority; pthread_attr_t attr; struct sched_param scheduling_value;
2. 各函数的参数说明如下: 首先是 pthread_t pthread_t 定义在/usr/include/bits/pthreadtypes.h 中
/* Thread identifiers. The structure of the attribute type is not exposed on purpose. */ typedef unsigned long int pthread_t;
/** * 设置优先级 */
scheduling_value.__sched_priority = -10; ret = pthread_attr_setschedparam(&attr,&scheduling_value);/* 设置调度参数*/ if(ret){
perror("Setting schedpolicy failed!\n"); exit(EXIT_FAILURE); }else{ printf("Set the schedparam sucess! %d !\n"); } ret = pthread_attr_getschedparam(&attr,&scheduling_value); if(ret){ perror("Get schedpolicy failed!\n"); exit(EXIT_FAILURE); }else{
__attr, int *__restrict __inherit)
/* Set scheduling inheritance mode in *ATTR according to INHERIT. */ extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,
将上面的注释翻译如下:根据 POLICY(调度策略) 和*PARAM(参数) 为 TARGETTHREAD(目 标线程)设置调度参数.
/* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */ extern int pthread_getschedparam (pthread_t __target_thread, int *__restrict __policy, struct sched_param *__restrict __param) ;
}; 用来作为调度策略的补充参数. 在创建线程的时候可以设置线程的属性.但是要先获得属性对象. 在 /usr/include/pthread.h 头文件的 300 页左右开始,定义了一系统的设置和获得线程属性的 API 函数.其中也有关于调度策略相关参数的 API. 如下: 好比面向对象中的 Setter 和 Getter.即属性设置器和访问器. 现在来分析下,上面的 API 和下面的 API 的区别.上面的 API 的操作对象是目标线程. 而下面的操作对象是线程属性对象.也就是说通过下面的 API 设置线程的属性的话,对共用同 一个线程属性对象创建出来的线程都有用. /* Return in *PARAM the scheduling parameters of *ATTR. */
第三个参数. ,struct sched_param *param 结构 struct sched_param 也定义在 /usr/include/bits/sched.h 中 /* The official definition. */ struct sched_param
{ int __sched_priority;
ret = pthread_create(&t1,&attr,(void *)*hello,"hello,world!"); if(ret){
perror("pthread_create failed \n"); exit(EXIT_FAILURE); }else{ printf("the new thread one id is %u\n",t1); }
ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); if(ret){
perror("pthread_attr_setdetachstate failed!\n"); exit(EXIT_FAILURE); }else{ printf("Set the detachestate to PTHREAD_CREATE_DETACHED\n"); }
POSIX 为线程调度策略指定了如下三个原则: 1. SCHED_OTHER: 系统默认策略.如时间轮片策略或基于优先级的调度策略. 2. SCHED_FIFO: 先进先出原则.具有最高优先级的,等待时间最长的线程将成为下一个 要执行的线程. 3. SCHED_RR: 轮转调度.类似于 FIFO,但加上了时间轮片策略.
Linux pthread 多线程库学习
之 线程调度策略
简介:
操作系统中常见的调度策略有如下几种: (1) FIFO 先入先出原则: 首先请求服务的对象首先得到 CPU 处理. (2) 最短作业优先原则: 需要最小系统时间的服务首先得到处理. (3) 时间轮片原则: 每个任务分配一个系统时间,轮流执行.
perror("pthread_attr_setschedpolicy failed !\n"); exit(EXIT_FAILURE); }
ret = pthread_attr_getdetachstate(&attr,&state); if(ret == -1){
perror("pthread_attr_getdetachstate failed!\n"); exit(EXIT_FAILURE); }else{ printf("The default detachestate is %d\n",state); }
extern int pthread_attr_getschedparam (__const pthread_attr_t *__restrict __attr, struct sched_param *__restrict __param)
/* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */ extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr, __const struct sched_param *__restrict __param)
上面注释的意思是: 线程标识,属性类型的结构并不是故意显露的. 其实 可以看出 pthread_t 就是是无符号的长整型. 第二个参数.policy ,(调度)策略,是一个整型可以取的值为如下定义的宏参数. 定义在 /usr/include/bits/sched.h 中.
/* Scheduling algorithms. */ #define SCHED_OTHER 0 #define SCHED_FIFO 1 #define SCHED_RR 2 #ifdef __USE_GNU # define SCHED_BATCH 3 #endif
返回目标线程的调度策略和相关参数. /* Set the scheduling priority for TARGET_THREAD. */ extern int pthread_setschedprio (pthread_t __target_thread, int __prio) ; 为目标线程设置调度优先级值. priority(优先.n)
相关文档
最新文档