Article Title: Linux scheduling policy and thread priority settings. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Three Scheduling Policies for Linux kernel:
1. SCHED_OTHER time-based scheduling policy,
2. SCHED_FIFO real-time scheduling policy, first served. Once the cpu is used, it runs continuously. Run until a higher-priority task arrives or you give up.
3. SCHED_RR real-time scheduling policy, time slice rotation. When the process time slice is used up, the system will re-allocate the time slice and place it at the end of the ready queue. Putting it at the end of the queue ensures fair scheduling for all RR tasks with the same priority
Linux thread priority settings
First, you can use the following two functions to obtain the highest and lowest priority that can be set by the thread. 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 does not support priority, while SCHED_FIFO and SCHED_RR support priority. They are 1 and 99, respectively. A larger value indicates a higher priority.
The following functions are used to set and obtain the priority:
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 );
For example, the following code creates a thread with a priority of 10:
Struct sched_param
{
Int _ sched_priority; // specifies the thread priority.
};
For example, create a thread with a priority of 10.
Pthread_attr_t attr;
Struct sched_param param;
Pthread_attr_init (& attr );
Pthread_attr_setschedpolicy (& attr, SCHED_RR );
Param. sched_priority = 10;
Pthread_attr_setschedparam (& attr, & param );
Pthread_create (xxx, & attr, xxx, xxx );
Pthread_attr_destroy (& attr );
This article from the CSDN blog, reproduced please indicate the source: http://blog.chinaunix.net/u3/111966/showart_2187242.html