免费注册 查看新帖 |

Chinaunix

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

linux线程调度和优先级设置有关问题(求解释) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-11 16:30 |只看该作者 |倒序浏览
linux可以设置线程的调度和优先级。支持三种调度方法。
1.SCHED_OTHER
分时调度策略,线程优先级为0;
2.SCHED_FIFO
实时调度策略,先到先服务。一当占用CPU,除非自己阻塞或结束或有更高优先级线程,否则会一直运行,线程优先级为1-99;
3.SCHED_RR
实时调度策略,时间片轮转 。其不会一直占用CPU,运行一个时间片后会让出CPU给自己同优先级的线程;

我编了一点程序,创建两个线程,都是SCHED_FIFO,线程1优先级为1,线程2为99。按理说,线程2会抢占线程1.线程2运行结束后线程1才运行。但是运行的结果却是两个线程交替执行。求大牛解释!
代码如下


/*
* main.c
*
*  Created on: 2011-7-4
*      Author: lis
*/

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sched.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/resource.h>
#include<string.h>

void FunThread1()
{
        printf("thread 1 start\n");

        int i,j;
        int policy;
        int priority;
        struct sched_param param;

        pthread_getschedparam(pthread_self(),&policy,&param);
        if(policy==SCHED_OTHER)
        {
         printf("thread 1 SCHED_OTHER\n");
         priority = param.sched_priority;
                printf("thread 1 SCHED_OTHER=%d \n",priority);
        }
        if(policy==SCHED_RR)
        {
         printf("thread 1 SCHED_RR\n");
         priority = param.sched_priority;
        printf("thread 1 SCHED_RR=%d \n",priority);
        }
        if(policy==SCHED_FIFO)
        {
                printf("thread 1 SCHED_FIFO\n");
                priority = param.sched_priority;
                printf("thread 1 SCHED_FIFO=%d \n",priority);
        }

        for(i=1;i<20;i++)
        {
        for(j=1;j<10000000;j++)
        {
        }
        printf("this is thread 1\n");
        }
        printf("Thread 1 exit\n");
}



void FunThread2()
{

        printf("thread 2 start\n");

        int i,j;
        int policy;
        int priority;
        struct sched_param param;

        pthread_getschedparam(pthread_self(),&policy,&param);
        if(policy==SCHED_OTHER)
        {
         printf("thread 2 SCHED_OTHER\n");
         priority = param.sched_priority;
                printf("thread 2 SCHED_OTHER=%d \n",priority);
        }
        if(policy==SCHED_RR)
        {
         printf("thread 2 SCHED_RR\n");
         priority = param.sched_priority;
        printf("thread 2 SCHED_RR=%d \n",priority);
        }
        if(policy==SCHED_FIFO)
        {
                printf("thread 2 SCHED_FIFO\n");
                priority = param.sched_priority;
                printf("thread 2 SCHED_FIFO=%d \n",priority);
        }

        for(i=1;i<20;i++)
        {
        for(j=1;j<10000000;j++)
        {
        }
        printf("this is thread 2\n");
        }
        printf("Thread 2 exit\n");
}

int main(int argc, char *argv[])
{

        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 tid1,tid2;
        pthread_attr_t attr1,attr2;
        struct sched_param param1;
        param1.sched_priority=1;
        struct sched_param param2;
        param2.sched_priority=99;

        pthread_attr_init(&attr1);
        pthread_attr_init(&attr2);

        pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
        pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);

        pthread_attr_setschedpolicy(&attr1,SCHED_FIFO);
        pthread_attr_setschedparam(&attr1,&param1);
        pthread_attr_setschedpolicy(&attr2,SCHED_FIFO);
        pthread_attr_setschedparam(&attr2,&param2);


        sleep(1);
        pthread_create(&tid1,&attr1,(void *)FunThread1,NULL);

        pthread_create(&tid2,&attr2,(void *)FunThread2,NULL);

        pthread_join(tid1,NULL);
        pthread_join(tid2,NULL);

        pthread_attr_destroy(&attr1);
        pthread_attr_destroy(&attr2);

        return 0;
}

运行结果:
The current user is root
thread 1 start
thread 2 start
thread 1 SCHED_FIFO
thread 2 SCHED_FIFO
thread 1 SCHED_FIFO=1
thread 2 SCHED_FIFO=99
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
this is thread 2
this is thread 1
Thread 1 exit
this is thread 2
Thread 2 exit

论坛徽章:
1
射手座
日期:2014-08-04 16:49:43
2 [报告]
发表于 2011-07-12 17:13 |只看该作者
因为有时间片。。。。  所以会切换,而且必须切换。。。。

假如真如你想的,我写个WHILE 1 或者 死锁 那么你电脑肯定理所应当的死机了, 你觉得合理吗?

论坛徽章:
0
3 [报告]
发表于 2011-07-12 22:34 |只看该作者
优先级是会动态改变的,不是你设多高,他就不会变

论坛徽章:
0
4 [报告]
发表于 2011-07-13 10:17 |只看该作者
回复 2# hanzhenlll


    那如何体现线程2的优先级比线程1高?

论坛徽章:
0
5 [报告]
发表于 2011-07-13 10:22 |只看该作者
回复 3# qprevf


    如果会改变,改变的原则是什么?

论坛徽章:
0
6 [报告]
发表于 2012-08-17 15:11 |只看该作者
本帖最后由 chen0610 于 2012-08-17 15:13 编辑

体现线程2的优先级比线程1高最好是用条件变量,线程2和线程1同时等待一个条件变量,另外再创建一个线程3释放条件变量。
哪一个线程先获得条件变量就说明哪一个的线程的优先级高。
在你的代码上修改的:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sched.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/resource.h>
#include<string.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *FunThread1(void *arg)
{
    int i;
    int policy;
    int priority;
    struct sched_param param;

        printf("Thread 1 start\n");

    pthread_getschedparam(pthread_self(),&policy,&param);
    if(policy==SCHED_OTHER)
    {
                printf("thread 1 SCHED_OTHER\n");
                priority = param.sched_priority;
                printf("thread 1 SCHED_OTHER=%d \n",priority);
    }
    if(policy==SCHED_RR)
    {
                printf("thread 1 SCHED_RR\n");
                priority = param.sched_priority;
                printf("thread 1 SCHED_RR=%d \n",priority);
    }
    if(policy==SCHED_FIFO)
    {
                printf("thread 1 SCHED_FIFO\n");
                priority = param.sched_priority;
                printf("thread 1 SCHED_FIFO=%d \n",priority);
    }

    for(i=11;i<=20;i++)
    {
                pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        printf("thread 1: i = %d.\n", i);
        pthread_mutex_unlock(&mutex);
    }
    printf("Thread 1 exit\n");
}



void *FunThread2(void *arg)
{
        int j;
        int policy;
        int priority;
        struct sched_param param;

        printf("Thread 2 start\n");

        pthread_getschedparam(pthread_self(),&policy,&param);
        if(policy==SCHED_OTHER)
        {
                printf("thread 2 SCHED_OTHER\n");
                priority = param.sched_priority;
                printf("thread 2 SCHED_OTHER=%d \n",priority);
        }
        if(policy==SCHED_RR)
        {
                printf("thread 2 SCHED_RR\n");
                priority = param.sched_priority;
                printf("thread 2 SCHED_RR=%d \n",priority);
        }
        if(policy==SCHED_FIFO)
        {
                printf("thread 2 SCHED_FIFO\n");
                priority = param.sched_priority;
                printf("thread 2 SCHED_FIFO=%d \n",priority);
        }

        for(j=1;j<=10;j++)
        {
                pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        printf("thread 2: j = %d.\n", j);
        pthread_mutex_unlock(&mutex);
        }
        printf("Thread 2 exit\n");
}

void *FunThread3(void *arg)
{
        int j;
        int policy;
        int priority;
        struct sched_param param;

        printf("Thread 3 start\n");

        pthread_getschedparam(pthread_self(),&policy,&param);
        if(policy==SCHED_OTHER)
        {
                printf("thread 3 SCHED_OTHER\n");
                priority = param.sched_priority;
                printf("thread 3 SCHED_OTHER=%d \n",priority);
        }
        if(policy==SCHED_RR)
        {
                printf("thread 3 SCHED_RR\n");
                priority = param.sched_priority;
                printf("thread 3 SCHED_RR=%d \n",priority);
        }
        if(policy==SCHED_FIFO)
        {
                printf("thread 3 SCHED_FIFO\n");
                priority = param.sched_priority;
                printf("thread 3 SCHED_FIFO=%d \n",priority);
        }

        for(;;)
        {
                pthread_mutex_lock(&mutex);
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        sleep(3);
        }
        printf("Thread 3 exit\n");
}


int main(int argc, char *argv[])
{
        int i;
        pthread_t tid1,tid2,tid3;
        pthread_attr_t attr1,attr2,attr3;
        struct sched_param param1, param2, param3;
       
        i=getuid();
        if(i==0)
        {
                printf("The current user is root\n");
        }
        else
        {
                printf("The current user is not root\n");
        }

        param1.sched_priority=20;
        param2.sched_priority=30;
        param3.sched_priority=10;

        pthread_attr_init(&attr1);
        pthread_attr_init(&attr2);
        pthread_attr_init(&attr3);

        pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
        pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);
        pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);

        pthread_attr_setschedpolicy(&attr1,SCHED_FIFO);
        pthread_attr_setschedparam(&attr1,&param1);
       
        pthread_attr_setschedpolicy(&attr2,SCHED_FIFO);
        pthread_attr_setschedparam(&attr2,&param2);
       
        pthread_attr_setschedpolicy(&attr3,SCHED_FIFO);
        pthread_attr_setschedparam(&attr3,&param3);

        sleep(1);

        pthread_create(&tid1,&attr1,FunThread1,NULL);

        sleep(1);

        pthread_create(&tid2,&attr2,FunThread2,NULL);

        sleep(1);

        pthread_create(&tid3,&attr3,FunThread3,NULL);

        pthread_join(tid3,NULL);

        pthread_attr_destroy(&attr1);
        pthread_attr_destroy(&attr2);

    return 0;
}

结果如下:
[root@localhost test]# gcc -o test test.c -lpthread
[root@localhost test]# ./test
The current user is root
Thread 1 start
thread 1 SCHED_FIFO
thread 1 SCHED_FIFO=20
Thread 2 start
thread 2 SCHED_FIFO
thread 2 SCHED_FIFO=30
Thread 3 start
thread 3 SCHED_FIFO
thread 3 SCHED_FIFO=10
thread 2: j = 1.
thread 2: j = 2.
thread 2: j = 3.
thread 2: j = 4.
thread 2: j = 5.
thread 2: j = 6.
thread 2: j = 7.
thread 2: j = 8.
thread 2: j = 9.
thread 2: j = 10.
Thread 2 exit
thread 1: i = 11.
thread 1: i = 12.
thread 1: i = 13.
thread 1: i = 14.
thread 1: i = 15.
thread 1: i = 16.
thread 1: i = 17.
thread 1: i = 18.
thread 1: i = 19.
thread 1: i = 20.
Thread 1 exit
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP