An example of a thread scheduling policy

Source: Internet
Author: User

To create a thread:
int Pthread_create (pthread_t *thread, const pthread_attr_t *attr,
void * (*start_routine) (void*), void *arg);

The next thing to say is: Set the thread priority after creating the thread.

Gets/sets the scheduling policy used by the current thread:
function: int pthread_attr_getschedpolicy (const pthread_attr_t *attr, int *policy);
int Pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy); Main use sched_param.__sched_priority
Successfully returns 0, otherwise returns-1
The policy is generally: Sched_fifo, SCHED_RR and Sched_other. The definition of the source see Sched.h
/* Scheduling algorithms. */
#define Sched_other0
#define Sched_fifo1
#define SCHED_RR2
#ifdef __USE_GNU
# define Sched_batch3
# define Sched_idle5

When the system creates a thread, the default thread is Sched_other.
(1) Note: Sometimes you will find that the Get scheduling policy returns-1, this behavior is related to the compilation method, please add-lpthread on the compilation option

Gets the priority extremum under one of the debugging policies: the highest and lowest priority that the thread can set
int Sched_get_priority_max (int policy);
int sched_get_priority_min (int policy);
Description
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.
The extrema under three strategies are as follows:
Sched_other: (0-0)
Sched_fifo: (1-99)
SCHED_RR: (1-99)

Conclusion:
If the program controls the priority of the thread, it is generally used pthread_attr_getschedpolicy to get the scheduling policy used by the system,
If it is sched_other, it indicates that the current policy does not support the use of thread priority, otherwise it can. Of course, the priority range you set must be between the maximum and minimum values.
We can get them through Sched_get_priority_max and sched_get_priority_min.
In theory, you can set your own scheduling policy by int pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);

And then, how do you set the priority level when you allow thread prioritization?
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);
where __sched_priority in struct sched_param is used to prioritize threads

(2) int pthread_setschedparam (pthread_t thread, int policy, const struct Sched_param *param);
int Pthread_attr_setschedparam (pthread_attr_t *restrict attr, const struct sched_param *restrict param);
The former is to modify the threads that are currently executing, specifically, to be used after pthread_create.
The latter is the parameter that modifies the thread that will be created and is used in the pthread_create.


Concept: Policy priority:
Functions: int Pthread_getschedparam (pthread_t thread, int policy, struct Sched_param *param)
For example, the thread internal code is as follows:
int policy = 0;
struct Sched_param param;
Pthread_getschedparam (Pthread_self (), &policy, &param);

Example:

#include <stdio.h>#include<unistd.h>#include<stdlib.h>#include<pthread.h>void*thread1 (void*Arg) {Sleep (1); intpolicy; structSched_param param; Pthread_getschedparam (Pthread_self (),&policy, &param); printf ("Thread1:policy (%d), PRI (%d) \ n", Policy, param.__sched_priority);  for(intI=1;i<Ten; i++)        {                 for(intj=1;j<5000000; j + +) {} printf ("Thread 1\n"); } printf ("Pthread 1 exit\n");}void*thread2 (void*Arg) {Sleep (1); intpolicy; structSched_param param; Pthread_getschedparam (Pthread_self (),&policy, &param); printf ("Thread2:policy (%d), PRI (%d) \ n", Policy, param.__sched_priority);  for(intI=1;i<Ten; i++)        {                 for(intj=1;j<5000000; j + +) {} printf ("Thread 2\n"); } printf ("Pthread 2 exit\n");}void*thread3 (void*Arg) {Sleep (1); intpolicy; structSched_param param; Pthread_getschedparam (Pthread_self (),&policy,&param); printf ("Thread3:policy (%d), PRI (%d) \ n", Policy, param.__sched_priority);  for(intI=1;i<Ten; i++)        {                 for(intj=1;j<5000000; j + +) {} printf ("Thread 3\n"); } printf ("Pthread 3 exit\n");}intMain () {inti; I=Getuid (); if(i==0) printf ("The current user is root\n"); Elseprintf ("The current user isn't root\n");        pthread_t tid1,tid2,tid3; structSched_param param;        pthread_attr_t attr3,attr2,attr1; //Thread attr initPthread_attr_init (&attr1); Pthread_attr_init (&attr2); //Thread attr SettingParam.sched_priority =Wuyi; Pthread_attr_setschedpolicy (&ATTR1,SCHED_RR); Pthread_attr_setschedparam (&attr1,&param); Pthread_attr_setinheritsched (&attr1,pthread_explicit_sched);//to make the priority its role must have this sentenceparam.sched_priority= +; Pthread_attr_setschedpolicy (&ATTR2,SCHED_RR); Pthread_attr_setschedparam (&attr2,&param); Pthread_attr_setinheritsched (&attr2,pthread_explicit_sched); Pthread_attr_init (&ATTR3); //Thread CreatePthread_create (&tid3,&ATTR3, Thread3,null); Pthread_create (&tid2,&attr2,thread2,null); Pthread_create (&tid1,&attr1,thread1,null); //thread WaitPthread_join (tid3,null);        Pthread_join (Tid2,null);        Pthread_join (Tid1,null); //thread Desory attrPthread_attr_destroy (&attr1); Pthread_attr_destroy (&attr2); return 0;}

Running Result: g++ 2-sched.cpp-lpthread

[[email protected] thread]#./A. outThe current user isRootthread1:policy (2), PRI (Wuyi) Thread2:policy (2), PRI ( +) Thread3:policy (0), PRI (0Thread2Thread1Thread3Thread1Thread2Thread3Thread1Thread2Thread3Thread1Thread2Thread3Thread1Thread2Thread3Thread1Thread2Thread3Thread1Thread2Thread3Thread1Thread2Thread3Thread1Pthread1ExitThread2Pthread2ExitThread3Pthread3Exit

Conclusion:

As we can see here, because the scheduling policy of thread 3 is sched_other, and the scheduling policy of thread 2 is SCHED_RR, in Thread3, Thread 3 is preempted by thread 1 and thread 2. Because thread 1 has a priority greater than thread 2, threads 1 runs ahead of thread 2, but there is a portion of thread 2 running before thread 1.
I thought, as long as the priority of the thread is high, will be run first, in fact, this understanding is one-sided, especially in the SMP of the PC will increase its uncertainty.
In fact, the normal process of scheduling, is the CPU based on the process priority to calculate the time slice, this does not necessarily guarantee that the high-priority process must first run, but compared with the lower priority process, usually higher priority process to obtain more CPU time slice. In fact, if you want to ensure that one thread runs out of another thread, it is necessary to use multi-threaded synchronization techniques, semaphores, condition variables, and so on. Instead of absolutely relying on the priority level, to ensure.
However, from the results of the run, we can see that the scheduling policy is SCHED_RR thread 1, thread 2 does preempt the scheduling policy to Sched_other thread 3. This is understandable, since SCHER_RR is a real-time scheduling strategy.
A real-time process is replaced by another process only if one of the following events occurs.
(1) The process is preempted by another real-time process with a higher real-time priority.
(2) The process performs a blocking operation and goes to sleep
(3) The process stops (in task_stopped or task_traced state) or is killed.
(4) The process calls Sched_yield () by invoking the system and voluntarily abandons the CPU.
(5) The process is based on the time slice rotation of the real-time process (SCHED_RR), and uses up its time slice.

The realtime process based on time-slice rotation is not a real change in the priority of the process, but rather a change in the length of the basic time slice of the process. Therefore, the process scheduling based on time-slice rotation does not guarantee that the high-priority process will run first.

An example of a thread scheduling policy

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.