用c语言(1)设计随机进程产生程序,说明随机性能对算法可能产生的影响 (2)设计一一个基于动态优先级的时间片轮转调度算法 (3)若实现可视化效果(图形、动画显示等)
时间: 2023-12-11 11:03:47 浏览: 189
(1) 以下是用C语言实现随机进程产生程序的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PROCESS_NUM 10
#define MAX_ARRIVE_TIME 20
#define MAX_RUN_TIME 10
typedef struct Process {
int pid; // 进程ID
int arrive_time; // 到达时间
int run_time; // 运行时间
} Process;
void generate_processes(Process *processes, int num) {
srand(time(NULL));
for (int i = 0; i < num; i++) {
processes[i].pid = i + 1;
processes[i].arrive_time = rand() % MAX_ARRIVE_TIME;
processes[i].run_time = rand() % MAX_RUN_TIME + 1; // 运行时间不能为0
}
}
void print_processes(Process *processes, int num) {
printf("PID\tArrive Time\tRun Time\n");
for (int i = 0; i < num; i++) {
printf("%d\t%d\t\t%d\n", processes[i].pid, processes[i].arrive_time, processes[i].run_time);
}
}
int main() {
Process processes[MAX_PROCESS_NUM];
generate_processes(processes, MAX_PROCESS_NUM);
print_processes(processes, MAX_PROCESS_NUM);
return 0;
}
```
随机性能对算法可能产生的影响:当进程的到达时间、运行时间等参数具有一定的随机性时,会导致不同的调度算法的表现不同。例如,在FCFS算法中,如果随机生成的进程中有一个长时间运行的进程先到达,那么后面到达的进程就需要等待很长时间,而在SJF算法中,长时间运行的进程会优先执行,可以减少等待时间。
(2) 以下是用C语言实现基于动态优先级的时间片轮转调度算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PROCESS_NUM 10
#define MAX_ARRIVE_TIME 20
#define MAX_RUN_TIME 10
#define TIME_SLICE 2
typedef struct Process {
int pid; // 进程ID
int arrive_time; // 到达时间
int run_time; // 运行时间
int priority; // 优先级
int runned_time; // 已经运行的时间
} Process;
void generate_processes(Process *processes, int num) {
srand(time(NULL));
for (int i = 0; i < num; i++) {
processes[i].pid = i + 1;
processes[i].arrive_time = rand() % MAX_ARRIVE_TIME;
processes[i].run_time = rand() % MAX_RUN_TIME + 1; // 运行时间不能为0
processes[i].priority = rand() % 10 + 1; // 优先级为1~10之间的整数
processes[i].runned_time = 0;
}
}
void print_processes(Process *processes, int num) {
printf("PID\tArrive Time\tRun Time\tPriority\n");
for (int i = 0; i < num; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrive_time, processes[i].run_time, processes[i].priority);
}
}
void dynamic_priority_rr(Process *processes, int num) {
int time = 0;
int finished_num = 0;
int current_process = -1;
while (finished_num < num) {
current_process = -1;
// 选取优先级最高的进程
for (int i = 0; i < num; i++) {
if (processes[i].run_time > 0 && processes[i].arrive_time <= time) {
if (current_process == -1 || processes[i].priority > processes[current_process].priority) {
current_process = i;
}
}
}
if (current_process == -1) {
// 当前时间段没有可运行的进程
time++;
continue;
}
// 执行一个时间片
printf("Time %d: Process %d runs for %d units.\n", time, processes[current_process].pid, TIME_SLICE);
processes[current_process].run_time -= TIME_SLICE;
processes[current_process].runned_time += TIME_SLICE;
if (processes[current_process].run_time == 0) {
// 进程执行完毕
printf("Time %d: Process %d finishes.\n", time + TIME_SLICE, processes[current_process].pid);
finished_num++;
} else {
// 进程未执行完毕,更新优先级
processes[current_process].priority--;
if (processes[current_process].priority == 0) {
// 优先级降为0时,重置为初始优先级
processes[current_process].priority = rand() % 10 + 1;
}
}
time += TIME_SLICE;
}
}
int main() {
Process processes[MAX_PROCESS_NUM];
generate_processes(processes, MAX_PROCESS_NUM);
print_processes(processes, MAX_PROCESS_NUM);
dynamic_priority_rr(processes, MAX_PROCESS_NUM);
return 0;
}
```
(3) 若要实现可视化效果,可以使用图形库(如SDL2、GTK+、Qt等)来实现图形界面和动画效果,也可以使用命令行界面(如ncurses)来显示进程的运行状态。以下是使用ncurses库实现简单的命令行界面的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <ncurses.h>
#define MAX_PROCESS_NUM 10
#define MAX_ARRIVE_TIME 20
#define MAX_RUN_TIME 10
#define TIME_SLICE 2
typedef struct Process {
int pid; // 进程ID
int arrive_time; // 到达时间
int run_time; // 运行时间
int priority; // 优先级
int runned_time; // 已经运行的时间
} Process;
void generate_processes(Process *processes, int num) {
srand(time(NULL));
for (int i = 0; i < num; i++) {
processes[i].pid = i + 1;
processes[i].arrive_time = rand() % MAX_ARRIVE_TIME;
processes[i].run_time = rand() % MAX_RUN_TIME + 1; // 运行时间不能为0
processes[i].priority = rand() % 10 + 1; // 优先级为1~10之间的整数
processes[i].runned_time = 0;
}
}
void print_processes(Process *processes, int num) {
printf("PID\tArrive Time\tRun Time\tPriority\n");
for (int i = 0; i < num; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrive_time, processes[i].run_time, processes[i].priority);
}
}
void dynamic_priority_rr(Process *processes, int num) {
int time = 0;
int finished_num = 0;
int current_process = -1;
initscr();
cbreak();
noecho();
curs_set(0);
mvprintw(0, 0, "Time\tPID\tPriority\tRun Time\tStatus");
while (finished_num < num) {
current_process = -1;
// 选取优先级最高的进程
for (int i = 0; i < num; i++) {
if (processes[i].run_time > 0 && processes[i].arrive_time <= time) {
if (current_process == -1 || processes[i].priority > processes[current_process].priority) {
current_process = i;
}
}
}
if (current_process == -1) {
// 当前时间段没有可运行的进程
time++;
usleep(200000);
continue;
}
// 执行一个时间片
mvprintw(time + 1, 0, "%d\t%d\t%d\t\t%d\t\trunning", time, processes[current_process].pid, processes[current_process].priority, processes[current_process].run_time);
processes[current_process].run_time -= TIME_SLICE;
processes[current_process].runned_time += TIME_SLICE;
if (processes[current_process].run_time == 0) {
// 进程执行完毕
mvprintw(time + 1, 44, "finished");
finished_num++;
} else {
// 进程未执行完毕,更新优先级
processes[current_process].priority--;
if (processes[current_process].priority == 0) {
// 优先级降为0时,重置为初始优先级
processes[current_process].priority = rand() % 10 + 1;
}
}
time += TIME_SLICE;
usleep(200000);
}
mvprintw(time + 2, 0, "Press any key to exit...");
getch();
endwin();
}
int main() {
Process processes[MAX_PROCESS_NUM];
generate_processes(processes, MAX_PROCESS_NUM);
print_processes(processes, MAX_PROCESS_NUM);
dynamic_priority_rr(processes, MAX_PROCESS_NUM);
return 0;
}
```
阅读全文