Three kinds of scheduling methods for Linux kernel:
1,sched_other time-sharing scheduling strategy,
2,sched_fifo Real-time scheduling strategy, first come first service
3,SCHED_RR Real-time scheduling strategy, time slice rotation
The process of SCHED_RR scheduling and Sched_fifo scheduling belongs to real-time process, and the process of time-sharing scheduling is not real-time.
When the real-time process is ready, if the current CPU is running a non-real-time process, the real-time process immediately grabs the non-real-time process.
The SCHED_RR thread and the SCHED_FIFO thread all adopt the real-time priority as the weight standard of the dispatch, RR is an extension of FIFO. FIFO, if two processes have the same priority, then these two priority processes are executed by their position in the queue, which leads to some unfairness (priority is the same, why do you keep running?) if the scheduling policy for the two priority tasks is set to RR, Guarantees that the two tasks can be implemented in a circular and fair.
Use Pthread_yield () to discard the execution rights of a thread
After the SCHED_RR and Sched_fifo real-time threads call Pthread_yield or are in the ready queue, they will continue to run without equal priority or higher priority
The scheduling unit of the Linux kernel is the thread (the concept of Linux threads and processes is rather vague, the execution thread is the kernel's scheduling object), and the Sched_fifo and SCHED_RR can be used to achieve 95% CPU utilization while testing, while (1) the time-sharing thread ( In the process of the impassability) get about 30%. Time-sharing threads can also get a small amount of execution time while running processes and time-sharing threads that have real-time scheduling threads (not blocking).
Sched_fifo and SCHED_RR are not strictly based on the Sched_fifo and SCHED_RR definitions, according to the Linux kernel scheduling strategy does not pass through other threads more or less CPU time (as above)
The creation of a live thread requires superuser privileges
Creation of real-time threads
Routine is the execution routine, dwpriority is the priority Sched_get_priority_max and sched_get_priority_min get generally 1-99 higher priority
int CREATE_FIFO_THD (void* (* routine) (void* param), int dwpriority)
{
int hea;
pthread_attr_t attr;
Pthread_attr_init (&ATTR);
/* Priority inheritance pthread_attr_setinheritsched after SCHED_RR Sched_fifo will play a role ********************/
if (pthread_attr_getinheritsched (&attr, &hea)!= 0)
printf ("\npthread_attr_getinheritsched err\n");
if (Hea = = pthread_inherit_sched)
printf ("\npthread_inherit_sched\n");
else if (Hea = = pthread_explicit_sched)
printf ("\npthread_inherit_sched\n");
Hea = pthread_explicit_sched;
if (pthread_attr_setinheritsched (&attr, Hea)!= 0)
printf ("\npthread_attr_getinheritsched err\n");
if (Hea = = pthread_inherit_sched)
printf ("\npthread_inherit_sched\n");
else if (Hea = = pthread_explicit_sched)
printf ("\npthread_explicit_sched\n");
Exit (1);
/***********************************************************************/
if (Pthread_attr_setschedpolicy (&ATTR,SCHED_FIFO)!= 0)
printf ("\npthread_attr_setschedpolicy\n");
Pthread_attr_setdetachstate (&attr,pthread_create_detached);
struct Sched_param sch_param;
sch_param.sched_priority = dwpriority;
if (Pthread_attr_setschedparam (&attr,&sch_param)!= 0)
printf ("\npthread_attr_setschedparam\n");
pthread_t pid = 0;
if (Pthread_create (&pid,&attr,routine,0)!= 0) {
printf ("\npthread_create err\n");
}
Pthread_attr_destroy (&ATTR);
return PID;
}
int CREATE_RR_THD (void* (* routine) (void* param), int dwpriority)
{
int hea;
int policy;
pthread_attr_t attr;
Pthread_attr_init (&ATTR);
/* Priority inheritance pthread_attr_setinheritsched after SCHED_RR Sched_fifo will play a role ********************/
if (pthread_attr_getinheritsched (&attr, &hea)!= 0)
printf ("\npthread_attr_getinheritsched err\n");
if (Hea = = pthread_inherit_sched)
printf ("\npthread_inherit_sched\n");
else if (Hea = = pthread_explicit_sched)
printf ("\npthread_inherit_sched\n");
Hea = pthread_explicit_sched;
if (pthread_attr_setinheritsched (&attr, Hea)!= 0)
printf ("\npthread_attr_getinheritsched err\n");
if (Hea = = pthread_inherit_sched)
printf ("\npthread_inherit_sched\n");
else if (Hea = = pthread_explicit_sched)
printf ("\npthread_explicit_sched\n");
/***********************************************************************/
if (Pthread_attr_setschedpolicy (&ATTR,SCHED_RR)!= 0)
printf ("\npthread_attr_setschedpolicy\n");
Pthread_attr_getschedpolicy (&attr,&policy);
if (policy = = SCHED_RR) {
printf ("\nsched_rr\n");
else if (policy = = Sched_fifo) {
printf ("\nsched_fifo\n");
else if (policy = = Sched_other) {
printf ("\nsched_other\n");
}
Exit (1);
Pthread_attr_setdetachstate (&attr,pthread_create_detached);
struct Sched_param sch_param;
sch_param.sched_priority = dwpriority;
if (Pthread_attr_setschedparam (&attr,&sch_param)!= 0)
printf ("\npthread_attr_setschedparam\n");
pthread_t pid = 0;
if (Pthread_create (&pid,&attr,routine,0)!= 0) {
printf ("\npthread_create err\n");
}
Pthread_attr_destroy (&ATTR);
return PID;
}