【问题标题】:Linux Thread priority , behaviour is abnormalLinux 线程优先级,行为异常
【发布时间】:2017-10-19 16:01:50
【问题描述】:

在下面的代码 sn-p 中,我创建了 6 个线程。每个都有不同的优先级。优先级在全局优先级数组中提到。我正在根据线程索引在每个线程内连续增加全局变量。如果线程优先级更高,我预计计数会更高。但我的输出没有遵循优先概念 pl。请参考下图的输出顺序。我正在 Ubuntu 16.04 和 Linux 内核 4.10 上尝试这个。

O/P,

Thread=0

Thread=3

Thread=2

Thread=5

Thread=1

Thread=4

pid=32155 count=4522138740

pid=32155 count=4509082289

pid=32155 count=4535088439

pid=32155 count=4517943246

pid=32155 count=4522643905

pid=32155 count=4519640181   

代码:

#include <stdio.h>
#include <pthread.h>
#define FAILURE -1
#define MAX_THREADS 15
long int global_count[MAX_THREADS];
/* priority of each thread */
long int priority[]={1,20,40,60,80,99};

void clearGlobalCounts()
{
        int i=0;
        for(i=0;i<MAX_THREADS;i++)
                global_count[i]=0;
}

/**
     thread parameter is thread index
**/
void funcDoNothing(void *threadArgument)
{
        int count=0;
        int index = *((int *)threadArgument);
        printf("Thread=%d\n",index);
        clearGlobalCounts();
        while(1)
        {
                count++;
                if(count==100)
                {       
                        global_count[index]++;
                        count=0;
                }
        }
}

int main()
{
        int i=0;
        for(int i=0;i<sizeof(priority)/sizeof(long int);i++)
            create_thread(funcDoNothing, i,priority[i]);
        sleep(3600);
        for(i=0;i<sizeof(priority)/sizeof(long int);i++)
        {
                printf("pid=%d count=%ld\n",getpid(),
                                global_count[i]);
        }
}

create_thread(void *func,int thread_index,int priority)
{
    pthread_attr_t attr;

    struct sched_param schedParam;
    void *pParm=NULL;
    int id;
    int * index = malloc(sizeof(int));
    *index = thread_index;
    void *res;
    /* Initialize the thread attributes */
    if (pthread_attr_init(&attr))
    {
            printf("Failed to initialize thread attrs\n");
            return FAILURE;
    }

    if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO))
    {
            printf("Failed to pthread_attr_setschedpolicy\n");
            return FAILURE;
    }

    if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO))
    {
            printf("Failed to setschedpolicy\n");
            return FAILURE;
    }

    /* Set the capture thread priority */
    pthread_attr_getschedparam(&attr, &schedParam);;
    schedParam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1; 
    schedParam.sched_priority = priority;
    if (pthread_attr_setschedparam(&attr, &schedParam))
    {
            printf("Failed to setschedparam\n");
            return FAILURE;
    }    
    pthread_create(&id, &attr, (void *)func, index);
}

【问题讨论】:

    标签: linux multithreading thread-priority


    【解决方案1】:

    pthread_attr_setschedparam 的文档说:

    为了进行参数设置 pthread_attr_setschedparam() 在调用时生效 pthread_create(3),调用者必须使用 pthread_attr_setinheritsched(3) 设置 属性对象 attr 的继承调度器属性 attr 到 PTHREAD_EXPLICIT_SCHED。

    所以你必须打电话给pthread_attr_setinheritsched(&amp;attr, PTHREAD_EXPLICIT_SCHED),例如:

       if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0) {
                perror("pthread_attr_setinheritsched");
        }
        pthread_create(&id, &attr, (void *)func, index);
    

    注意:您的代码会产生很多编译器警告,您需要修复这些警告。您不想尝试测试具有很多未定义行为的代码 - 正如一些警告所示。您可能应该将 sleep(3600) 降低到几秒钟,因为当您的线程在 SCHED_FIFO 下运行时,它们会占用您的 CPU 并且机器在运行时似乎冻结。

    【讨论】:

    • 感谢您的输入,行为似乎好多了。我更正了所有警告。 3600 是实验性的,只是为了确保初始调度延迟不会影响计数。 o/p 现在是,pid=31068 count=0 pid=31068 count=15 pid=31068 count=56961864 pid=31068 count=56964895 pid=31068 count=57002126 pid=31068 count=56688136 仍然是最高优先级的任务行为不是这样持续的。我尝试了 30sec sleep ,我会尝试更高的延迟。现在我以 root 身份运行程序,为什么会有这个限制?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2011-06-25
    • 2010-09-22
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    相关资源
    最近更新 更多