如下程序,它包含thread1和thread2两个线程,令thread1为高优先级线程,thread2为低优先级线程;两个线程的功能都是将自己的调度策略和优先级打印10次。分别按以下4种调度配置补全代码,然后运行程序,分析运行结果。 (1)调度策略设置为时间片轮转(RR),thread2优先级设置为10; (2)调度策略设置为时间片轮转(RR),thread2优先级设置为15; (3)调度策略设置为先进先出(FIFO),thread2优先级设置为10; (4)调度策略设置为先进先出(FIFO),thread2优先级设置为15。
时间: 2024-03-23 11:40:39 浏览: 51
Java多线程-Thread类的常用结构及线程优先级
```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执行。
阅读全文