Priority of Linux thread scheduling

Source: Internet
Author: User

Three scheduling strategies for the Linux kernel:

1. Sched_other time-sharing scheduling strategy

2, Sched_fifo Real-time scheduling strategy, first-come-first service. Runs continuously once the CPU is occupied. Keep running until a higher priority task arrives or abandons itself

3, SCHED_RR Real-time scheduling strategy, time slice rotation. When the time slice of the process is exhausted, the system will redistribute the time slices and place them at the end of the ready queue. Placed at the end of the queue ensures that all RR tasks with the same priority are scheduled fairly

Linux Thread Priority setting:

First, the following two functions can be used to get the highest and lowest priority that a thread can set, and the policy in the function is the macro definition of the above three strategies:

  int sched_get_priority_max (int  policy);  int sched_get_priority_min (int policy);

Note: Sched_other is not supported for priority use, while SCHED_FIFO and SCHED_RR support priority use, they are 1 and 99 respectively, the higher the value the higher the priority.

Set and get priority through the following two functions:

int Const struct sched_param *param); int pthread_attr_getschedparam (conststruct sched_param *param);   Wuyi // setting the priority level

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

int int policy);

The above Param uses the following data structure:

struct sched_param{     int  to set the thread priority };

We can use the following test procedure to illustrate the priority of our own system support:

#include <stdio.h>#include<pthread.h>#include<sched.h>#include<assert.h>Static intGet_thread_policy (pthread_attr_t *attr) {  intpolicy; intrs = Pthread_attr_getschedpolicy (attr,&policy); ASSERT (Rs==0); Switch(policy) { Casesched_fifo:printf ("policy= sched_fifo\n");  Break;  Casesched_rr:printf ("policy= SCHED_RR");  Break;  Casesched_other:printf ("policy=sched_other\n");  Break; default: printf ("policy=unknown\n");  Break; }  returnpolicy;}Static voidShow_thread_priority (pthread_attr_t *attr,intpolicy) {  intPriority =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 intGet_thread_priority (pthread_attr_t *attr) {  structSched_param param; intrs = Pthread_attr_getschedparam (attr,&param); ASSERT (Rs==0); printf ("priority=%d", param.__sched_priority); returnparam.__sched_priority;}Static voidSet_thread_policy (pthread_attr_t *attr,intpolicy) {  intrs =Pthread_attr_setschedpolicy (Attr,policy); ASSERT (Rs==0); Get_thread_policy (attr);}intMainvoid) {pthread_attr_t attr; structSched_param sched; intrs; RS= Pthread_attr_init (&attr); ASSERT (Rs==0); intPolicy = Get_thread_policy (&attr); printf ("Show Current configuration of priority\n"); Show_thread_priority (&attr,policy); printf ("show Sched_fifo of priority\n"); Show_thread_priority (&Attr,sched_fifo); printf ("show Sched_rr of priority\n"); Show_thread_priority (&ATTR,SCHED_RR); printf ("Show priority of the current thread\n"); intPriority = Get_thread_priority (&attr); printf ("Set thread policy\n"); printf ("Set Sched_fifo policy\n"); Set_thread_policy (&Attr,sched_fifo); printf ("Set SCHED_RR policy\n"); Set_thread_policy (&ATTR,SCHED_RR); printf ("Restore Current policy\n"); Set_thread_policy (&attr,policy); RS= Pthread_attr_destroy (&attr); ASSERT (Rs==0); return 0;}

Here is the result of the test program running:

policy=sched_othershow Current configuration of prioritymax_priority=0min_priority=0show Sched_fifo of Prioritymax_priority= Aboutmin_priority=1show Sched_rr of Prioritymax_priority= Aboutmin_priority=1Show priority of the current threadpriority=0Set Thread PolicySetSched_fifo Policypolicy=Sched_fifoSetSCHED_RR Policypolicy=Sched_rrrestore Current Policypolicy=sched_other

Priority of Linux thread scheduling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.