pthread_policy_example.c Fedora上运行修改_xueyueguangshen-ChinaUnix博客

因为树上的例子在Fedora15,Fedora上都没能跑起来,估计是Fedora和RedHat的调度策略有一些不同吧,所以自己改了一下,把调度策略和优先级值改了,然后正确显示:

点击(此处)折叠或打开

    #include
    #include
    #include
    #include
    void *thread_function(void *arg);
    char message[]="Hello World";
    int thread_finished=0;
    int main(int argc,char argv[])
    {
    int res;
    int max_priority,min_priority;
    pthread_t a_thread;
    void *thread_result;
    pthread_attr_t thread_attr;
    struct sched_param scheduling_value;
    res=pthread_attr_init(thread_attr);//初始化线程属性
    if(res!=0)
    {
    perror("Attribute creation failed");
    exit(EXIT_FAILURE);
    }
    res=pthread_attr_setschedpolicy(thread_attr,SCHED_FIFO);//设置线程调度策略
    if(res!=0)
    {
    perror("Setting schedpolicy failed");
    exit(EXIT_FAILURE);
    }
    res=pthread_attr_setdetachstate(thread_attr,PTHREAD_CREATE_DETACHED);//设置状态
    if(res!=0)
    {
    perror("Setting detachstate failed");
    exit(EXIT_FAILURE);
    }
    else
    printf("set the datachstate to PTHREAD_CTEATE_DETACHED\n");
    res=pthread_create(a_thread,thread_attr,thread_function,(void *)message);//创建线程
    if(res!=0)
    {
    perror("Thread creation failed");
    exit(EXIT_FAILURE);
    }
    else
    printf("Create new thread success");
    max_priority=sched_get_priority_max(SCHED_FIFO);//获取最大优先级
    min_priority=sched_get_priority_min(SCHED_FIFO);//最小优先级
    printf("The max priority of SCHED_OTHER is %d; the min is %d\n",max_priority,min_priority);


    /*
    res=pthread_attr_setinheritsched(thread_attr,PTHREAD_EXPLICIT_SCHED);
    printf("\nres_before-2:%d\n",res);
    res=pthread_attr_setschedpolicy(thread_attr,SCHED_FIFO);
    printf("\nres_before-1:%d\n",res);
    if(res!=0)
    {
    perror("set schedpolicy failed");
    exit(EXIT_FAILURE);
    }
    max_priority=sched_get_priority_max(SCHED_FIFO);
    min_priority=sched_get_priority_min(SCHED_FIFO);
    printf("The max priority of SCHED_FIFO is %d; the min is %d\n",max_priority,min_priority);
    */



    res=pthread_attr_getschedparam(thread_attr,scheduling_value);
    if(res!=0)
    {
    perror("get schedpolicy failed");
    exit(EXIT_FAILURE);
    }
    else
    printf("The priority of current thread is %d\n",scheduling_value.sched_priority);
    scheduling_value.sched_priority=10;
    //printf("The priority of current thread is %d\n",scheduling_value.sched_priority);
    res=pthread_attr_setschedparam(thread_attr,scheduling_value);//设置优先级
    //printf("\nres_before:%d\n",res);
    if(res!=0)
    {
    perror("Setting schedpolicy failed\n");
    exit(EXIT_FAILURE);
    }
    else
    printf("Set the schedulparam success\n");
    //printf("The priority of current thread is %d\n",scheduling_value.sched_priority);//读调度参数
    res=pthread_attr_getschedparam(thread_attr,scheduling_value);
    //printf("\nres_after:%d\n",res);
    if(res!=0)
    {
    perror("Get schedpolicy failed");
    exit(EXIT_FAILURE);
    }
    else
    printf("The priority of current thread is %d\n",scheduling_value.sched_priority);
    (void)pthread_attr_destroy(thread_attr);//删除属性
    sleep(2);
    while(!thread_finished)
    {
    printf("Waiting for thread finished ...\n");
    sleep(1);
    }
    printf("Ohter thread finished ,bye!\n");
    exit(1);
    }
    void *thread_function(void *arg)
    {
    printf("Thread_function if running . Arguement was %d\n",(char *)arg);
    sleep(4);
    printf("Second thread setting finished flag,and exitting now\n");
    thread_finished=1;
    pthread_exit(NULL);
    }
结果:set the datachstate to PTHREAD_CTEATE_DETACHEDCreate new thread successThe max priority of SCHED_OTHER is 99; the min is 1The priority of current thread is 0Set the schedulparam successThe priority of current thread is 10Thread_function if running . Arguement was 134520660Waiting for thread finished ...Waiting for thread finished ...Second thread setting finished flag,and exitting nowOhter thread finished ,bye!

你可能感兴趣的:(博客,修改,运行)