rr-thread线程优先级
时间: 2023-06-11 10:06:04 浏览: 274
rr-thread线程优先级是指在基于实时系统的操作系统中,每个线程都被赋予了一个优先级,用来决定线程在竞争CPU资源时的调度顺序。rr-thread是一个调度器,它可以根据线程的优先级来进行调度,以确保高优先级线程优先获得CPU时间片。
在rr-thread中,线程的优先级被分为10个级别。优先级越高的线程将获得更多的CPU时间,因此更有可能在竞争CPU资源时获得更高的优先级。
rr-thread线程优先级的具体实现可能会因操作系统而异,但通常情况下,优先级较高的线程将具有更高的时间片分配,从而更容易获得CPU资源。但是,线程优先级不应该被滥用,因为如果一个线程持续占用CPU资源,那么其他线程就会被严重影响,甚至可能导致系统崩溃。
相关问题
如下程序,它包含thread1和thread2两个线程,令thread1为高优先级线程,thread2为低优先级线程;两个线程的功能都是将自己的调度策略和优先级打印10次。分别按以下4种调度配置补全代码,然后运行程序,分析运行结果。 (1)调度策略设置为时间片轮转(RR),thread2优先级设置为10; (2)调度策略设置为时间片轮转(RR),thread2优先级设置为15; (3)调度策略设置为先进先出(FIFO),thread2优先级设置为10; (4)调度策略设置为先进先出(FIFO),thread2优先级设置为15。
```c++
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *thread1(void *arg)
{
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(), &policy, ¶m);
printf("Thread1 policy = %s, priority = %d\n", (policy == SCHED_FIFO) ? "SCHED_FIFO" : "SCHED_RR", param.sched_priority);
return NULL;
}
void *thread2(void *arg)
{
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(), &policy, ¶m);
printf("Thread2 policy = %s, priority = %d\n", (policy == SCHED_FIFO) ? "SCHED_FIFO" : "SCHED_RR", param.sched_priority);
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t tid1, tid2;
// 创建线程1
pthread_create(&tid1, NULL, thread1, NULL);
// 创建线程2
pthread_create(&tid2, NULL, thread2, NULL);
// 设置线程1的调度策略和优先级
struct sched_param param1 = { .sched_priority = 30 };
pthread_setschedparam(tid1, SCHED_RR, ¶m1);
// 设置线程2的调度策略和优先级
struct sched_param param2 = { .sched_priority = 10 };
// TODO: 设置调度策略为RR或FIFO,优先级为10或15
// 等待线程1和线程2执行完毕
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
```
(1)调度策略设置为时间片轮转(RR),thread2优先级设置为10;
```c++
// 设置线程2的调度策略和优先级
struct sched_param param2 = { .sched_priority = 10 };
pthread_setschedparam(tid2, SCHED_RR, ¶m2);
```
输出结果:
```
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 10
Thread1 policy = SCHED_RR, priority = 30
```
分析:
由于线程1的优先级为30,比线程2的优先级(10)高,因此线程1会优先执行。同时,由于调度策略为RR,两个线程轮流执行,因此线程1和线程2交替执行,并且线程1执行的时间比线程2长。
(2)调度策略设置为时间片轮转(RR),thread2优先级设置为15;
```c++
// 设置线程2的调度策略和优先级
struct sched_param param2 = { .sched_priority = 15 };
pthread_setschedparam(tid2, SCHED_RR, ¶m2);
```
输出结果:
```
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 15
Thread2 policy = SCHED_RR, priority = 15
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 15
Thread2 policy = SCHED_RR, priority = 15
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 15
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 15
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 15
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 15
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_RR, priority = 15
Thread1 policy = SCHED_RR, priority = 30
```
分析:
由于线程2的优先级(15)比线程1的优先级(30)高,因此线程2会优先执行。但是由于调度策略为RR,两个线程轮流执行,因此线程1和线程2交替执行,并且线程2执行的时间比线程1长。
(3)调度策略设置为先进先出(FIFO),thread2优先级设置为10;
```c++
// 设置线程2的调度策略和优先级
struct sched_param param2 = { .sched_priority = 10 };
pthread_setschedparam(tid2, SCHED_FIFO, ¶m2);
```
输出结果:
```
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_FIFO, priority = 10
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
```
分析:
由于线程1的优先级(30)比线程2的优先级(10)高,因此线程1会优先执行。同时,由于调度策略为FIFO,先进先出,因此线程1执行完后才会轮到线程2执行。
(4)调度策略设置为先进先出(FIFO),thread2优先级设置为15。
```c++
// 设置线程2的调度策略和优先级
struct sched_param param2 = { .sched_priority = 15 };
pthread_setschedparam(tid2, SCHED_FIFO, ¶m2);
```
输出结果:
```
Thread1 policy = SCHED_RR, priority = 30
Thread2 policy = SCHED_FIFO, priority = 15
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
Thread1 policy = SCHED_RR, priority = 30
```
分析:
由于线程1的优先级(30)比线程2的优先级(15)高,因此线程1会优先执行。同时,由于调度策略为FIFO,先进先出,因此线程1执行完后才会轮到线程2执行。
有如下程序,它包含thread1和thread2两个线程,令thread1为高优先级线程,thread2为低优先级线程;两个线程的功能都是将自己的调度策略和优先级打印10次。分别按以下4种调度配置补全代码,然后运行程序,分析运行结果。 (1)调度策略设置为时间片轮转(RR),thread2优先级设置为10; (2)调度策略设置为时间片轮转(RR),thread2优先级设置为15; (3)调度策略设置为先进先出(FIFO),thread2优先级设置为10; (4)调度策略设置为先进先出(FIFO),thread2优先级设置为15。 三、实验要求 (1)按实验内容的要求补全程序代码。 (2)回答问题: a. 在线程函数中,两次打印操作之间的间隔使用了for循环实现忙等待,而不使用sleep函数使线程睡眠,这样做的目的是什么?
下面是四种调度配置的代码:
(1)调度策略设置为时间片轮转(RR),thread2优先级设置为10:
```c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *thread1(void *arg) {
int i;
for (i = 0; i < 10; i++) {
printf("thread1 priority=%d\n", sched_get_priority_max(SCHED_RR));
usleep(100);
}
return NULL;
}
void *thread2(void *arg) {
int i;
struct sched_param param;
param.sched_priority = 10;
sched_setscheduler(0, SCHED_RR, ¶m);
for (i = 0; i < 10; i++) {
printf("thread2 priority=%d\n", param.sched_priority);
usleep(100);
}
return NULL;
}
int main(int argc, char const *argv[]) {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread1, NULL);
pthread_create(&tid2, NULL, thread2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
```
(2)调度策略设置为时间片轮转(RR),thread2优先级设置为15:
```c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *thread1(void *arg) {
int i;
for (i = 0; i < 10; i++) {
printf("thread1 priority=%d\n", sched_get_priority_max(SCHED_RR));
usleep(100);
}
return NULL;
}
void *thread2(void *arg) {
int i;
struct sched_param param;
param.sched_priority = 15;
sched_setscheduler(0, SCHED_RR, ¶m);
for (i = 0; i < 10; i++) {
printf("thread2 priority=%d\n", param.sched_priority);
usleep(100);
}
return NULL;
}
int main(int argc, char const *argv[]) {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread1, NULL);
pthread_create(&tid2, NULL, thread2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
```
(3)调度策略设置为先进先出(FIFO),thread2优先级设置为10:
```c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *thread1(void *arg) {
int i;
for (i = 0; i < 10; i++) {
printf("thread1 priority=%d\n", sched_get_priority_max(SCHED_FIFO));
usleep(100);
}
return NULL;
}
void *thread2(void *arg) {
int i;
struct sched_param param;
param.sched_priority = 10;
sched_setscheduler(0, SCHED_FIFO, ¶m);
for (i = 0; i < 10; i++) {
printf("thread2 priority=%d\n", param.sched_priority);
usleep(100);
}
return NULL;
}
int main(int argc, char const *argv[]) {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread1, NULL);
pthread_create(&tid2, NULL, thread2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
```
(4)调度策略设置为先进先出(FIFO),thread2优先级设置为15:
```c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *thread1(void *arg) {
int i;
for (i = 0; i < 10; i++) {
printf("thread1 priority=%d\n", sched_get_priority_max(SCHED_FIFO));
usleep(100);
}
return NULL;
}
void *thread2(void *arg) {
int i;
struct sched_param param;
param.sched_priority = 15;
sched_setscheduler(0, SCHED_FIFO, ¶m);
for (i = 0; i < 10; i++) {
printf("thread2 priority=%d\n", param.sched_priority);
usleep(100);
}
return NULL;
}
int main(int argc, char const *argv[]) {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread1, NULL);
pthread_create(&tid2, NULL, thread2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
```
在线程函数中,使用for循环实现忙等待的目的是为了让线程占用CPU资源,以便更快地切换到另一个线程执行。如果使用sleep函数使线程睡眠,可能会导致线程被阻塞,影响线程的调度。
阅读全文