Linux内核的三种调度策略
2013-04-03 13:03
337 查看
Linux内核的三种调度策略:
1,SCHED_OTHER分时调度策略2,SCHED_FIFO实时调度策略
先到先服务.一旦占用cpu则一直运行.一直运行直到有更高优先级任务到达或自己放弃
3,SCHED_RR实时调度策略,
时间片轮转.当进程的时间片用完,系统将重新分配时间片,并置于就绪队列尾.放在队列尾保证了所有具有相同优先级的RR任务的调度公平Linux线程优先级设置,
可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义:
intsched_get_priority_max(intpolicy);
intsched_get_priority_min(intpolicy);
SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高.
设置和获取优先级通过以下两个函数:
intpthread_attr_setschedparam(pthread_attr_t*attr,conststructsched_param*param);
intpthread_attr_getschedparam(constpthread_attr_t*attr,structsched_param*param);
多线程编程常用的几个函数:
pthread_create()函数
函数pthread_create()创建一个新的线程并把它的标识符放入参数thread指向的新线程中。API定义如下: #include <pthread.h>
int pthread_create(pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg)
第二个参数attr是用来设置线程的属性。线程的属性是由函数pthread_attr_init()来生成。第三个参数是新线程要执行的函数的地址。第四个参数是一个void指针,可以作为任意类型的参数传给start_routine()函数;同时,start_routine()可以返回一个void *类型的返回值,而这个返回值也可以是其他类型,并由pthread_join()获取。
pthread_join()函数
函数pthread_join()的作用是挂起当前线程直到参数th指定的线程被终止为止。API定义如下: #include <pthread.h>
int pthread_join (pthread_t th, void **thread_return);
int pthread_detach(pthread_t th);
第一个参数th为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。
pthread_exit()函数
该函数调用pthread_cleanup_push()为线程注册的清除处理函数,然后结束当前线程,返回retval,父线程或其它线程可以通过函数pthread_join()来检索它。API定义为: #include <pthread.h>
void pthread_exit(void * retval)
属性控制
我们用pthread_create()函数创建了一个线程,在这个线程中,我们使用了默认参数,即将该函数的第二个参数设为NULL。对大多数程序来说使用默认属性足够,但我们仍然需要了解一下线程的属性。 属性结构为pthread_attr_t,它同样在头文件/usr/include/pthread.h中定义,属性值不能直接设置,须使用相关函数进行操作。函数pthread_attr_init()的作用是初始化一个新的属性对象,函数pthread_attr_destroy()的作用是清除属性对象。用户在调用这些函数之前要为属性(attr)对象分配空间。
#include <pthread.h>
int pthread_attr_init(pthread_attr_t * attr);
int pthread_attr_destroy(pthread_attr_t * attr);
int pthread_attr_setdetachstate(pthread_attr_t * attr,int detachstate);
int pthread_attr_getdetachstate(const pthread_attr_t * attr,int * detachstate);
int pthread_attr_setschedpolicy(pthread_attr_t * attr,int policy);
int pthread_attr_getschedpolicy(const pthread_attr_t * attr,int * policy);
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);
int pthread_attr_setscope(pthread_attr_t * attr,int scope);
int pthread_attr_getscope(const pthread_attr_t * attr,int * scope);
void Set_ThreadAttr(pthread_attr_t *thread_attr, int sched_policy, int sched_priority)
{
int status;
struct sched_param thread_param;
char policy[6];
status = 0;
switch(sched_policy) {
case SCHED_RR:
strcpy(policy, "RR");
break;
case SCHED_FIFO:
strcpy(policy, "FIFO");
break;
case SCHED_OTHER:
strcpy(policy, "OTHER");
break;
default:
printf("Wrong sched policy\n");
break;
}
status = pthread_attr_init(thread_attr);
if (status != 0) {
printf("Init attr error: %d\n", status);
}
status = pthread_attr_setschedpolicy(thread_attr, sched_policy);
if (status != 0)
printf("Unable to set policy %s.\n", policy);
else {
thread_param.sched_priority = sched_priority;
status = pthread_attr_setschedparam(thread_attr, &thread_param);
if (status != 0) {
printf("Set param error: %d\n", status);
}
printf("Creating thread at %s/%d\n", policy, thread_param.sched_priority);
status = pthread_attr_setinheritsched(thread_attr, PTHREAD_EXPLICIT_SCHED);
if (status != 0) {
printf("Set inherit error: %d\n", status);
}
}
}
相关文章推荐
- linux内核的三种主要调度策略
- linux内核的三种主要调度策略
- Linux内核的三种调度策略
- linux内核调度策略SCHED_OTHER,SCHED_FIFO和SCHED_RR
- linux内核的三种调度方法
- linux内核CFS进程调度策略分析(二)
- linux内核的调度策略
- btrfs cfq, noop, deadline三种IO调度策略下的IO性能表现(gp针对grup.conf配置)
- linux内核的三种调度方法
- linux内核CFS进程调度策略
- Linux内核调度策略与算法分析 (1)
- linux内核CFS进程调度策略
- Linux内核调度策略与算法分析 (2)
- linux内核CFS进程调度策略
- (转)Linux内核调度策略与算法分析 (1)
- linux内核调度策略的修改对内核线程的影响
- (转)Linux内核调度策略与算法分析 (2)
- linux内核的三种调度方法
- linux内核的三种调度方法
- lvs十种调度策略+三种模式