posix的线程优先级

时间:2017-04-11 10:16:21

标签: c multithreading posix

我正在尝试使用POSIX优先级。我的目标是拥有以下计划。两个线程正在运行优先级为10的Thread0和优先级为50的Thread1。在有限循环(如3秒)和此时间间隔内的Thread1块尝试执行自己。结果应该是Thread0将被阻止,因为正在执行优先级较高的线程。

我的结果是优先级不会改变线程的行为...我使用以下命令编译gcc -Wall -o scheduling scheduling3.c -pthread 和ubuntu上的sudo su命令。 结果:

Prio min = 1, Prio max = 99 
SCHED_FIFO
Priority of the thread 0 : 10 
SCHED_FIFO
Priority of the thread 1 : 50 
Value of test_thread_0 1 
Value of test_thread_1 1

我想要的结果:

Prio min = 1, Prio max = 99 
SCHED_FIFO
Priority of the thread 0 : 10 
SCHED_FIFO
Priority of the thread 1 : 50 
Value of test_thread_0 1 
Value of test_thread_1 1

计划:

// The thread with high priority wil block the thread with low priority
// Run with super user privilege (sudo su with ubuntu)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>

#define NUM_THREADS     2

int test_thread_0 = 0;
int test_thread_1 = 0;



void *BlockThread0(void *threadid) {
    test_thread_0 = 1;      
}

void *BlockThread1(void *threadid) {
    struct timeval start, end;
    long secs_used;
    gettimeofday(&start, NULL);
    test_thread_1 = 1;  

    while(1) {
        gettimeofday(&end, NULL);
        secs_used=(end.tv_sec - start.tv_sec); //avoid overflow by subtracting first
        if(secs_used > 3)
            break;
    }

}


int main(int argc, char *argv[]) {
    int i, policy;
    pthread_t tid[NUM_THREADS];
    pthread_attr_t attr[NUM_THREADS];
    struct sched_param param[NUM_THREADS], test;
    int prio_max, prio_min;

    // Get the range of the policy
    prio_max = sched_get_priority_max(SCHED_FIFO);
    prio_min = sched_get_priority_min(SCHED_FIFO);
    printf("Prio min = %d, Prio max = %d \n", prio_min, prio_max);

    // Set the different priority
    param[0].sched_priority = 10;
    param[1].sched_priority = 50;

    // Set all the attribute (policy + priority)
    for(i = 0; i < NUM_THREADS; i++) {

        pthread_attr_init(&attr[i]);

        if(pthread_attr_setinheritsched(&attr[i], PTHREAD_EXPLICIT_SCHED) != 0)
            fprintf(stderr, "Unable to set EXPLICIT SCHEDULER.\n");

        pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_JOINABLE);

        /* The attribute get the new policy */
        if(pthread_attr_setschedpolicy(&attr[i], SCHED_FIFO) != 0)
            fprintf(stderr, "Unable to set policy.\n");

        /* Test to change the priority of each task */
        if(pthread_attr_setschedparam(&attr[i], &param[i]) != 0)
            fprintf(stderr, "Unable to set priority.\n");
    }

    // Get all the attribute (policy + priority)
    for(i = 0; i < NUM_THREADS; i++) {
        // Get the policy
        if(pthread_attr_getschedpolicy(&attr[i], &policy) != 0)
            fprintf(stderr, "Unable to get policy.\n");
        else{
            if(policy == SCHED_OTHER)
                printf("SCHED_OTHER\n");
            else if(policy == SCHED_RR)
                printf("SCHED_RR\n");
            else if(policy == SCHED_FIFO)
                printf("SCHED_FIFO\n");
        }
        /* Get the priority */
        pthread_attr_getschedparam(&attr[i], &test);
        printf("Priority of the thread %d : %d \n",i,test.sched_priority);
    }

    // Thread1 with the most important priority is executing
    pthread_create(&tid[1], &attr[1], BlockThread1, (void *)1); 

    // To be sure that the thread1 is running
    sleep(1);


    //Thread2 with lower priority attempt to execute himself but he is blocked because thread1 is executing
    pthread_create(&tid[0], &attr[0], BlockThread0, (void *)0);

    /* now join on each thread */
    for(i = 0; i < NUM_THREADS; i++) 
        pthread_join(tid[i], NULL);

    printf("Value of test_thread_0 %d \n",test_thread_0);
    printf("Value of test_thread_1 %d \n",test_thread_1);

}

2 个答案:

答案 0 :(得分:1)

该循环被称为&#34;繁忙的循环&#34;,它不会给操作系统&#39; scheduler一种调度其他线程的方法。您应该使用sleep()的某些变体。

答案 1 :(得分:0)

&#39;结果应该是Thread0将被阻止,因为具有更高优先级的线程正在执行&#39;

没有。目前几乎所有硬件都没有。

线程只会保持就绪,等待执行,如果有更多的就绪/正在运行的线程而不是运行它们的内核。

使用两个线程,您应该注意到只有一个核心的处理器所期望的东西。

现在非常罕见。