Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1482306
  • 博文数量: 181
  • 博客积分: 3308
  • 博客等级: 中校
  • 技术积分: 2227
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-03 12:03
个人简介

我是zoro

文章分类

全部博文(181)

文章存档

2015年(1)

2013年(35)

2012年(39)

2011年(50)

2010年(56)

分类: LINUX

2010-12-02 12:29:19

测试代码如下:

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

void testTime(struct timeval *priv, struct timeval *next)
{
    long long sec, usec, consume;
    sec = next->tv_sec - priv->tv_sec;
    usec = next->tv_usec - priv->tv_usec;
    if (sec) {
        consume = sec * 1000000 + usec;
    } else {                        
        consume =usec;    
    }                            
    
    printf("(%x)Consume time = %lld\n",
        (int)pthread_self(), consume);
}

void do_prime(long long value)
{
    long long i;
    int flag = 0;

    for (i = 2; i < value; i++) {
        if ((value % i) == 0) {
            flag = 1;
            break;
        }
    }

    if (flag == 0) {
        printf("%lld is a prime!\n",
             value);
    } else {
        printf("%lldis not a prime!\n", value);
    }
}

void * thread_handler(void * arg)
{
    printf("My tid is %x\n", (int)pthread_self());
    int ret, policy;
    struct sched_param param;

    ret = pthread_getschedparam(pthread_self(),
        &policy, &param);
    if (ret) {
        printf("get schedparam:%s\n",
            strerror(ret));
        return NULL;    
    }    
    
    if (policy == SCHED_OTHER)
        printf("policy:SCHED_OTHER");
    else if (policy == SCHED_FIFO)    
        printf("policy:SCHED_FIFO");
    else if (policy == SCHED_RR)    
        printf("policy:SCHED_RR");
    printf("\n");    

    //测试时间

    struct timeval priv, next;
    gettimeofday(&priv, NULL);
    do_prime(300000007);
    gettimeofday(&next, NULL);
    testTime(&priv, &next);
    
    return NULL;
}

int main(void)
{
    pthread_t tid;
    int ret;
    pthread_attr_t attr;
    int policy, inher;
    struct sched_param param;

    policy = SCHED_FIFO;
    param.sched_priority = 99;    //设置优先级参数



    pthread_attr_init(&attr);    //初始化属性

    ret = pthread_attr_setdetachstate (&attr,PTHREAD_CREATE_DETACHED);    //设置分离状态
    pthread_attr_getinheritsched(&attr, &inher);    //获取继承的调度策略

    if (inher == PTHREAD_INHERIT_SCHED)
        printf("Can't change sched policy!\n");
    else if (inher == PTHREAD_EXPLICIT_SCHED)    
        printf("Can change sched policy!\n");
#if 1
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);    //设置继承的调度策略

    
    pthread_attr_getinheritsched(&attr, &inher);    //获取继承的调度策略

    if (inher == PTHREAD_INHERIT_SCHED)
        printf("Can't change sched policy!\n");
    else if (inher == PTHREAD_EXPLICIT_SCHED)    
        printf("Can change sched policy!\n");
        
    ret = pthread_attr_setschedpolicy(&attr, policy);        //设置调度策略

    if (ret) {
        printf("set policy:%s\n",
            strerror(ret));
        exit(1);
    }
 
    ret = pthread_attr_setschedparam(&attr, &param);        //设置调度参数

    if (ret) {
        printf("set policy:%s\n",
            strerror(ret));
        exit(1);
    }
#endif

    
    ret = pthread_create(&tid, &attr,thread_handler, NULL);        //创建新线程

    if (ret) {
        printf("pthread_create:%s\n",
            strerror(ret));
        exit(1);    
    }
    
    //测试时间

    struct timeval priv, next;
    gettimeofday(&priv, NULL);
    do_prime(300000007);
    gettimeofday(&next, NULL);
    testTime(&priv, &next);
    
    pthread_exit(NULL);
}

运行结果:
1.如不设置线程属性(main函数中#if 1 改为 #if 0)
Can't change sched policy!
My tid is b770fb70
policy:SCHED_OTHER
300000007  is a prime!
(b77106c0)Consume time = 23942636
300000007  is a prime!
(b770fb70)Consume time = 23940930

2.设置新线程属性(main中#if 0 改为#if 1)
Can't change sched policy!
Can change sched policy!
My tid is b7764b70
policy:SCHED_FIFO
300000007  is a prime!
(b7764b70)Consume time = 12319551
300000007  is a prime!
(b77656c0)Consume time = 22628737

可以看出新线程处理任务所需时间减少了一半左右。

阅读(2478) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~