简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
本篇目的:理解Linux之sched_setscheduler调度策略用法。
函数原型如下:
int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param);
参数说明:
返回值:
注意事项:
sched_setscheduler函数的作用是根据给定的调度策略和优先级设置进程的调度方式。它可以用于调整进程在系统中的调度优先级,并影响进程在CPU上的执行顺序。调度策略可以根据进程的需求选择合适的调度算法,不同的调度策略会影响进程的响应性、公平性和实时性。
需要注意的是,对于实时进程,例如使用SCHED_FIFO或SCHED_RR策略的进程,优先级较高的进程将优先执行,直到它完成或主动让出CPU。而对于普通进程,使用SCHED_OTHER策略的进程将遵循时间片轮转调度算法,按照一定的时间片轮流执行。
总结:sched_setscheduler函数用于设置进程的调度策略和优先级,允许开发者对进程进行精确的调度控制,以满足不同应用场景的需求。
#include
#include
int main() {
struct sched_param param;
int ret = sched_setscheduler(0, SCHED_OTHER, ¶m);
if (ret == -1) {
std::cout << "Failed to set scheduler!\n";
} else {
std::cout << "Successfully set scheduler to SCHED_OTHER\n";
}
return 0;
}
#include
#include
int main() {
struct sched_param param;
int ret = sched_setscheduler(0, SCHED_FIFO, ¶m);
if (ret == -1) {
std::cout << "Failed to set scheduler!\n";
} else {
std::cout << "Successfully set scheduler to SCHED_FIFO\n";
}
return 0;
}
#include
#include
int main() {
struct sched_param param;
int ret = sched_setscheduler(0, SCHED_RR, ¶m);
if (ret == -1) {
std::cout << "Failed to set scheduler!\n";
} else {
std::cout << "Successfully set scheduler to SCHED_RR\n";
}
return 0;
}
#include
#include
int main() {
struct sched_param param;
int ret = sched_setscheduler(0, SCHED_BATCH, ¶m);
if (ret == -1) {
std::cout << "Failed to set scheduler!\n";
} else {
std::cout << "Successfully set scheduler to SCHED_BATCH\n";
}
return 0;
}
#include
#include
int main() {
struct sched_param param;
int ret = sched_setscheduler(0, SCHED_IDLE, ¶m);
if (ret == -1) {
std::cout << "Failed to set scheduler!\n";
} else {
std::cout << "Successfully set scheduler to SCHED_IDLE\n";
}
return 0;
}
#include
#include
int main() {
pid_t pid = 1234; // 要设置调度策略的进程ID
struct sched_param param;
int ret = sched_setscheduler(pid, SCHED_OTHER, ¶m);
if (ret == -1) {
std::cout << "Failed to set scheduler for process " << pid << "!\n";
} else {
std::cout << "Successfully set scheduler to SCHED_OTHER for process " << pid << "\n";
}
return 0;
}