Three scheduling strategies for the Linux kernel:
1,sched_other time-sharing scheduling strategy,
2,SCHED_FIFO Real-time scheduling strategy, first to first service. Run as soon as the CPU is occupied. Run 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 time slice is reassigned and placed at the end of the ready queue. Placed at the end of the queue guarantees scheduling fairness for all RR tasks with the same priority
Linux Thread priority setting
First, you can use the following two functions 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 policies:
int Sched_get_priority_max (int policy);
int sched_get_priority_min (int policy);
Sched_other is not supported for priority use, and SCHED_FIFO and SCHED_RR support priority use, they are 1 and 99, respectively, the higher the number of priority.
Set and get precedence 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're going to change the thread's scheduling strategy, we can do that with the following function.
int Pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy); |
The param above uses the following data structure:
struct Sched_param { int __sched_priority; The thread priority that you want to set }; |
We can use the following test program to illustrate the priority of the support of our own system:
#include <stdio.h> #include <pthread.h> #include <sched.h> #include <assert.h>
static int 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 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 get_thread_priority (pthread_attr_t *attr) { struct Sched_param param; int rs = Pthread_attr_getschedparam (Attr,¶m); ASSERT (rs==0); printf ("priority=%d", param.__sched_priority); return param.__sched_priority; }
static void Set_thread_policy (pthread_attr_t *attr,int policy) { int rs = Pthread_attr_setschedpolicy (Attr,policy); ASSERT (rs==0); Get_thread_policy (attr); }
int main (void) { pthread_attr_t attr; struct Sched_param sched; int RS; rs = Pthread_attr_init (&attr); ASSERT (rs==0);
int policy = 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 |