- 论坛徽章:
- 0
|
linux实时调度策略有三种
(1)SCHED_OTHER。SCHED_OTHER是面向普通进程的时间片轮转策略。
(2)SCHED_FIFO。SCHED_FIFO策略适用于对响应时间要求比较高,运行所需时间比较短的实时进程。采用该策略时,各实时进程按其进入可运行队列的顺序依次获得CPU。除了因等待某个事件主动放弃CPU,或者出现优先级更高的进程而剥夺其CPU之外,该进程将一直占用CPU运行。
(3)SCHED_RR。SCHED_RR策略适用于对响应时间要求比较高,运行所需时间比较长的实时进程。采用该策略时,各实时进程按时间片轮流使用CPU。当一个运行进程的时间片用完后,进程调度程序停止其运行并将其置于可运行队列的末尾。
下面这个程序是测试SCHED_FIFO的调度策略
//============================================================================
// Name : TestPthread.cpp
// Author : Uranus
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C, Ansi-style
//============================================================================
#ifndef MSGDEQUEUE_H
#define MSGDEQUEUE_H
#include <pthread.h>
#include <iostream>
#include <errno.h>
#include <time.h>
#include <semaphore.h>
#include <deque>
#include <unistd.h>
#include <stdio.h>
#include <malloc.h>
#include <sched.h>
#include <string.h>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int my_policy;
pthread_attr_t thread_attr1;
pthread_attr_t thread_attr2;
int var = 0;
void* thread1(void* arg)
{
printf("\n thread1\n");
struct sched_param myparam;
pthread_getschedparam(pthread_self(), &my_policy, &myparam);
printf("\n111111111111111111111111111111111111result = %d, %d", my_policy, myparam.sched_priority);
while(1)
{
//printf("\n while1 \n");
}
}
void* thread2(void* arg)
{
printf("\n thread1\n");
struct sched_param myparam;
pthread_getschedparam(pthread_self(), &my_policy, &myparam);
printf("\n2222222222222222222222222222222222222result = %d, %d", my_policy, myparam.sched_priority);
while(1)
{
printf("\n while2 \n");
}
}
int main(void) {
pthread_t pid1;
pthread_t pid2;
struct sched_param my_param1;
memset(&my_param1, 0, sizeof(sched_param));
struct sched_param my_param2;
memset(&my_param2, 0, sizeof(sched_param));
int status;
status = pthread_attr_init(&thread_attr1);
status = pthread_attr_init(&thread_attr2);
if(status != 0)
{
printf("error in pthread_attr_init");
}
my_param1.__sched_priority = (sched_get_priority_min(SCHED_RR) + sched_get_priority_max(SCHED_RR)) / 2;
my_param2.__sched_priority = 1;
status = pthread_attr_setinheritsched(&thread_attr1, PTHREAD_EXPLICIT_SCHED);
status = pthread_attr_setinheritsched(&thread_attr2, PTHREAD_EXPLICIT_SCHED);
if(status != 0)
{
printf("error in pthread_attr_setinheritsched");
}
status = pthread_attr_setschedpolicy(&thread_attr1, SCHED_FIFO);
status = pthread_attr_setschedpolicy(&thread_attr2, SCHED_FIFO);
if(status != 0)
{
printf("error in pthread_attr_setschedpolicy");
}
status = pthread_attr_setschedparam(&thread_attr1, &my_param1);
status = pthread_attr_setschedparam(&thread_attr2, &my_param2);
if(status != 0)
{
printf("error in pthread_attr_setschedparam");
}
printf("\n Create Threand");
status = pthread_create(&pid1, &thread_attr1, thread1, NULL);
if(status != 0)
{
printf("error in pthread1 create, status = %d", status);
}
status = pthread_create(&pid2, &thread_attr2, thread2, NULL);
if(status != 0)
{
printf("error in pthread2 create, status = %d", status);
}
pthread_join( pid1,NULL );
pthread_join( pid2,NULL );
return 1;
}
程序运行结果:
thread1 = 1 //代表是FIFO策略
while1
while1
........
........
thread2 = 0 //Other
while2
while2
........
........
while1
........
while2
看这个结果得出,这两个线程是交替运行的。
那我向各位提出疑问:两个线程等级相同,采用FIFO策略,按理说因该由一个运行完后,另一个才会运行,现在怎么交替了。
谢谢你们的回复!
内核版本是2.6.16, 2.6.25
[ 本帖最后由 PassionUranus 于 2008-10-13 11:24 编辑 ] |
|