免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4230 | 回复: 10
打印 上一主题 下一主题

关于linux实时调度策略 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-09 18:29 |只看该作者 |倒序浏览
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 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-10-10 09:44 |只看该作者
我发在编程问题上,版主说让我放到内核来,
内核源码的高手帮我分析下

论坛徽章:
0
3 [报告]
发表于 2008-10-10 11:29 |只看该作者
http://www.linuxquestions.org/qu ... ad-in-linux-518274/

你看看目前的调度策略是什么,有没有被改变?

论坛徽章:
0
4 [报告]
发表于 2008-10-10 11:30 |只看该作者
http://www.linuxquestions.org/qu ... ad-in-linux-518274/

你看看目前的调度策略是什么,有没有被改变?

论坛徽章:
0
5 [报告]
发表于 2008-10-10 13:47 |只看该作者
printf打印到终端,是否会阻塞?仅用优先级低的打印试试

论坛徽章:
0
6 [报告]
发表于 2008-10-13 11:09 |只看该作者

回复 #3 yangliu817 的帖子

我打印出来调度策略,已经修改

论坛徽章:
0
7 [报告]
发表于 2008-10-13 11:21 |只看该作者
原帖由 flw2 于 2008-10-10 13:47 发表
printf打印到终端,是否会阻塞?仅用优先级低的打印试试

我把优先级底的打印了,优先级高的没打,但是低优先级还是打印了

论坛徽章:
0
8 [报告]
发表于 2008-10-13 11:21 |只看该作者
原帖由 flw2 于 2008-10-10 13:47 发表
printf打印到终端,是否会阻塞?仅用优先级低的打印试试


我觉的这个即使阻塞,应该不会影响到打印的顺序, 因为写入buffer的顺序是不变的.

另外,这种情况下是否会阻塞呢?我感觉不会,但是没有验证过.

论坛徽章:
0
9 [报告]
发表于 2008-10-13 11:22 |只看该作者
原帖由 yangliu817 于 2008-10-10 11:30 发表
http://www.linuxquestions.org/qu ... ad-in-linux-518274/

你看看目前的调度策略是什么,有没有被改变?


我打印出来调度策略,已经修改

论坛徽章:
0
10 [报告]
发表于 2008-10-14 09:37 |只看该作者
一定不要沉下去!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP