Linux thread priority setting

Three scheduling strategies of the Linux kernel:

1. SCHED_OTHER time-sharing scheduling strategy

2. SCHED_FIFO real-time scheduling policy, first come first served. Once the cpu is occupied, it will run all the time. Run until a higher priority task arrives or give up

3. SCHED_RR real-time scheduling strategy, time slice rotation. When the time slice of the process is used up, the system will reallocate the time slice and place it at the end of the ready queue. Putting it at the end of the queue ensures fair scheduling of all RR tasks with the same priority.

Linux thread priority settings:

First of all, the highest and lowest priorities that a thread can set can be obtained through the following two functions. The strategies in the functions are the macro definitions of the above three strategies:

  int sched_get_priority_max(int policy);   

       int sched_get_priority_min(int policy);

Note: SCHED_OTHER does not support the use of priority, while SCHED_FIFO and SCHED_RR support the use of priority. They are 1 and 99 respectively. The larger the value, the higher the priority.

Set and get the priority through the following two functions:

int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);

int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); param.sched_priority = 51; //set priority

When the system creates a thread, the default thread is SCHED_OTHER. So if we want to change the thread scheduling strategy, we can do it through the following function.

int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);

    The above param uses the following data structure:

struct sched_param {    

    int __sched_priority; // The thread priority to be set

};

We can use the following test program to illustrate the priority of the system we use:

#include <stdio.h>

#include <pthread.h>

#include <sched.h>

#include <assert.h>

static int api_get_thread_policy (pthread_attr_t *attr)

{

    int policy;

    int rs = pthread_attr_getschedpolicy (attr, &policy);

    assert (rs == 0);

    switch (policy)

    {

        case SCHED_FIFO:

            printf ("policy = SCHED_FIFO\n");

            break;

        case SCHED_RR:

            printf ("policy = SCHED_RR");

            break;

        case SCHED_OTHER:

            printf ("policy = SCHED_OTHER\n");

            break;

        default:

            printf ("policy = UNKNOWN\n");

            break; 

    }

    return policy;

}

static void api_show_thread_priority (pthread_attr_t *attr,int policy)

{

    int priority = sched_get_priority_max (policy);

    assert (priority != -1);

    printf ("max_priority = %d\n", priority);

    priority = sched_get_priority_min (policy);

    assert (priority != -1);

    printf ("min_priority = %d\n", priority);

}

static int api_get_thread_priority (pthread_attr_t *attr)

{

    struct sched_param param;

    int rs = pthread_attr_getschedparam (attr, &param);

    assert (rs == 0);

    printf ("priority = %d\n", param.__sched_priority);

    return param.__sched_priority;

}

static void api_set_thread_policy (pthread_attr_t *attr,int policy)

{

    int rs = pthread_attr_setschedpolicy (attr, policy);

    assert (rs == 0);

    api_get_thread_policy (attr);

int main(void)

{

    pthread_attr_t attr; // thread attributes

    struct sched_param sched; // scheduling strategy

    int rs;

    /* 

     * Initialize thread properties

     * After the initialization is complete, the structure contained in the pthread_attr_t structure

     * is the default value of all thread attributes supported by the operating system implementation

     */

    rs = pthread_attr_init (&attr);

    assert (rs == 0); // If rs is not equal to 0, the program abort() exits

    /* Get the current scheduling policy*/

    int policy = api_get_thread_policy (&attr);

    /* Display the thread priority range of the current scheduling policy */

    printf ("Show current configuration of priority\n");

    api_show_thread_priority(&attr, policy);

    /* Get the thread priority range under the SCHED_FIFO policy*/

    printf ("show SCHED_FIFO of priority\n");

    api_show_thread_priority(&attr, SCHED_FIFO);

    /* Get the thread priority range under the SCHED_RR policy*/

    printf ("show SCHED_RR of priority\n");

    api_show_thread_priority(&attr, SCHED_RR);

    /* Display the priority of the current thread */

    printf ("show priority of current thread\n");

    int priority = api_get_thread_priority (&attr);

    /* Manually set the scheduling policy */

    printf ("Set thread policy\n");

    printf ("set SCHED_FIFO policy\n");

    api_set_thread_policy(&attr, SCHED_FIFO);

    printf ("set SCHED_RR policy\n");

    api_set_thread_policy(&attr, SCHED_RR);

    /* restore the previous strategy */

    printf ("Restore current policy\n");

    api_set_thread_policy (&attr, policy);

    /* 

     * Deinitialize the pthread_attr_t structure

     * If the implementation of pthread_attr_init dynamically allocates the memory space of the attribute object,

     * phread_attr_destory will release the memory space

     */

    rs = pthread_attr_destroy (&attr);

    assert (rs == 0);

    return 0;

}

Guess you like

Origin blog.csdn.net/rukawashan/article/details/125142379