当前位置:网站首页>[multi thread programming] thread scheduling strategy and priority
[multi thread programming] thread scheduling strategy and priority
2022-06-22 07:27:00 【Neilo_ chen】
Through the previous study, I know the current Linux There are three main task scheduling strategies under the system :
1、SCHED_OTHER: General task scheduling strategy .
2、SCHED_FIFO: Real time task scheduling strategy , First come, first serve . Once occupied cpu It runs all the time , Until a higher priority task arrives or abandons itself .
3、SCHED_RR: Real time task scheduling strategy , Time slice rotation . When the time slice of the task runs out , The system will reallocate the timeslice , And put it at the end of the ready queue . Put it at the end of the queue
Ensures that all... With the same priority RR Fair scheduling of tasks .
Linux Provides interface functions related to task scheduling policy and priority :
1、
-
// First understand sched_param Structure
-
// In the current implementation, the structure contains only one field, sched_priority.
-
// The interpretation of param depends on the selected policy
-
-
struct sched_param
-
{
-
int sched_priority;
-
};
-
int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param);
-
-
int sched_getscheduler(pid_t pid);
The sched_setscheduler() system call sets both the scheduling policy and parameters for the thread whose ID is specified in pid.
If pid equals zero, the scheduling policy and parameters of the calling thread will be set.
The sched_getscheduler() returns the current scheduling policy of the thread identified by pid. If pid equals zero, the policy of the
calling thread will be retrieved.
Similar to the above functions are :
-
int sched_setparam(pid_t pid, const struct sched_param *param);
-
-
int sched_getparam(pid_t pid, struct sched_param *param);
We can use the following two functions to detect the value range of priority under a policy .
-
int sched_get_priority_max(int policy);
-
-
int sched_get_priority_min(int policy);
about SCHED_OTHER Strategy ,sched_priority Only for 0. about SCHED_FIFO,SCHED_RR Strategy ,sched_priority from 1 To 99.
3、 Through the following two functions, we can set / View the scheduling properties of the thread .
-
// Scheduling attribute structure
-
struct sched_attr {
-
u32 size;
/* Size of this structure */
-
u32 sched_policy;
/* Policy (SCHED_*) */
-
u64 sched_flags;
/* Flags */
-
s32 sched_nice;
/* Nice value (SCHED_OTHER,
-
SCHED_BATCH) */
-
u32 sched_priority;
/* Static priority (SCHED_FIFO,
-
SCHED_RR) */
-
/* Remaining fields are for SCHED_DEADLINE */
-
u64 sched_runtime;
-
u64 sched_deadline;
-
u64 sched_period;
-
};
-
int sched_setattr(pid_t pid, struct sched_attr *attr, unsigned int flags);
-
-
int sched_getattr(pid_t pid, struct sched_attr *attr, unsigned int size, unsigned int flags);
Below API Be used more :
4、 initialization / The destruction struct pthread_attr_t
-
int pthread_attr_init(pthread_attr_t *attr);
-
-
int pthread_attr_destroy(pthread_attr_t *attr);
After initialization attr By default SCHED_OTHER Strategy , The priority for 0.
5、 Set up / View the scheduling policy to attr in
-
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
-
-
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
6、 Set up / View the scheduling priority to attr in
-
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);
7、 Inherit scheduling properties inheritsched.
-
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
-
-
int pthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched);
Function inheritsched The values for :PTHREAD_INHERIT_SCHED perhaps PTHREAD_EXPLICIT_SCHED. The former is to inherit the scheduling policy of creating threads
Omission and priority , The latter specifies that the scheduling policy and priority are not inherited , Instead, you use your own scheduling policies and priorities . No matter when , When you need to control the tone of a thread
Degree policy or priority , Must be inheritsched Property is set to PTHREAD_EXPLICIT_SCHED.
8、 Dynamic setting / View scheduling properties .
-
<span style=
"font-size:14px;">
int pthread_setschedparam(
pthread_t thread,
int policy,
const struct sched_param *param);
-
-
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param);</span>
In this method, there is no need to call a function to set inheritsched The attribute is , because pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED) The object set by the function is pthread_attr_t !!!
Pay attention to SCHED_RR or SCHED_FIFO The strategy requires the use of sudo Permission to run the program .
Be careful :
1) Scheduling policy and priority are described separately . The former uses predefined SCHED_RR、SCHED_FIFO、SCHED_OTHER, The latter is through the result body
struct sched_param Given .
2) These functions that set the scheduling policy and priority operate on the properties of the thread pthread_attr_t( Clause 8 With the exception of ), Instead of directly operating the thread's scheduling policy and
Priority . The first argument of the function is pthread_attr_t.
Let's use the code to detect :
-
#include <pthread.h>
-
#include <unistd.h>
-
#include <stdio.h>
-
#include <assert.h>
-
-
void display()
-
{
-
sched_param param;
-
int policy;
-
-
pthread_getschedparam(pthread_self(), &policy, &m);
-
switch(policy)
-
{
-
case SCHED_RR:
-
printf(
"%X: policy:SCHED_RR priority:%d\n", pthread_self(), param.sched_priority);
-
break;
-
case SCHED_FIFO:
-
printf(
"%X: policy:SCHED_FIFO priority:%d\n", pthread_self(), param.sched_priority);
-
break;
-
case SCHED_OTHER:
-
printf(
"%X: policy:SCHED_OTHER priority:%d\n", pthread_self(), param.sched_priority);
-
break;
-
default:
-
printf(
"error\n");
-
}
-
}
-
-
void* fun(void *p)
-
{
-
display();
-
-
sched_param param;
-
param.sched_priority =
20;
-
// Dynamically modify the scheduling policy
-
pthread_setschedparam(pthread_self(), SCHED_FIFO, &m);
-
-
display();
-
}
-
-
int main()
-
{
-
pthread_t pid;
-
sched_param param;
-
int policy;
-
pthread_attr_t attr;
-
-
pthread_attr_init(&attr);
-
param.sched_priority =
10;
-
policy = SCHED_RR;
-
assert(pthread_attr_setschedpolicy(&attr, policy) ==
0);
-
assert(pthread_attr_setschedparam(&attr, &m) ==
0);
-
assert(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) ==
0);
-
-
assert(pthread_create(&pid, &attr, fun,
NULL) ==
0);
-
-
pthread_join(pid,
NULL);
-
pthread_attr_destroy(&attr);
-
-
return
0;
-
}
Use sudo After operation :
-
B75E8B40: policy:SCHED_RR priority:
10
-
B75E8B40: policy:SCHED_FIFO priority:
20
If we don't use sudo function , You're going to report a mistake :
Assertion `pthread_create(&pid, &attr, fun, __null) == 0' failed.
边栏推荐
- Image interpolation (nearest neighbor, bilinear)
- Chromedriver所有版本下載
- Assembly learning Chapter 4 of assembly language (Third Edition) written by Wang Shuang
- Set the way that liquid (Jekyll) is displayed in markdown but not parsed
- chrome浏览器查看cookie方法
- Shutter margin negative margin
- Authority management of okcc call center
- antd 框架:点击链接重开浏览器页面——基础积累
- How to authorize a picture in a store to another store? What are the methods of unauthorized replication
- How to view cookies in Chrome browser
猜你喜欢

How to import Taobao products into another store

Image interpolation (nearest neighbor, bilinear)

antd 框架:点击链接重开浏览器页面——基础积累

Real MySQL interview questions (20) -- video data analysis practice

Matlab uses deep learning recurrent neural network RNN long-term and short-term memory LSTM to predict waveform time series data

Coursera self driving car Part4 motion planning finalproject principle and key code analysis

Batch collection, grab Taobao baby, upload and collect commodity software

架构图颜色搭配

C语言实现的简易考试系统

Propeller framework v2.3 releases high reusable operator library Phi: Restructure development paradigm, reduce cost and increase efficiency
随机推荐
JS implementation of random generation of 16 bit key -- Basic accumulation
精益生产|精益管理
Article editing test of CSDN
Get through version - bargain activity
What are the ways for Taobao merchants to put their babies on the shelves in batches
Solution to the problem of "brand abuse such as brand inconsistency and stacking in the published product information" prompted by copying and uploading
js实现随机生成16位的密钥——基础积累
Rviz ROS wiki official website tutorial learning notes (1) - User Guide
vue连接mysql数据库失败
How to import and upload a CSV generated by a third-party platform to a Taobao store
Open source get through version - integral function
Canoe learning notes (9) sending module can Ig diagram
Wechat games (2)
Matlab用深度学习循环神经网络RNN长短期记忆LSTM进行波形时间序列数据预测
Set the way that liquid (Jekyll) is displayed in markdown but not parsed
Selenium anti crawl and analog mobile browser
How to solve 'webdriver' object has no attribute 'switch_ to_ window‘
咖啡供应链是如何被趟平的?
Detailed explanation of capturing the whole store treasure and uploading it to multiple stores
Open version - order delivery