用c语言实现以下功能 (1)手动创建几个进程,或者随机创建几个进程,都在界面上完成;要求包括进程的名称(不能重复)、创建时间、执行时间等。 (2)输出各进程不同调度算法的调度过程,输出周转时间、平均周转时间和平均带权周转时间。对比各算法的优劣,输出对比结果。
时间: 2023-05-11 10:06:40 浏览: 117
非常感谢您的问题!以下是用C语言实现进程调度的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_PROCESS_NUM 10
#define MAX_PROCESS_NAME_LEN 20
typedef struct Process {
char name[MAX_PROCESS_NAME_LEN];
int create_time;
int execute_time;
int start_time;
int end_time;
int turnaround_time;
float weighted_turnaround_time;
} Process;
void manual_create_processes(Process *processes, int *process_num) {
printf("请输入进程数量:");
scanf("%d", process_num);
printf("请依次输入进程名称、创建时间和执行时间:\n");
for (int i = 0; i < *process_num; i++) {
scanf("%s %d %d", processes[i].name, &processes[i].create_time, &processes[i].execute_time);
}
}
void random_create_processes(Process *processes, int *process_num) {
srand((unsigned)time(NULL));
*process_num = rand() % MAX_PROCESS_NUM + 1;
printf("随机生成的进程数量为:%d\n", *process_num);
for (int i = 0; i < *process_num; i++) {
sprintf(processes[i].name, "P%d", i + 1);
processes[i].create_time = rand() % 10;
processes[i].execute_time = rand() % 10 + 1;
}
}
void print_processes(Process *processes, int process_num) {
printf("进程名称\t创建时间\t执行时间\n");
for (int i = 0; i < process_num; i++) {
printf("%s\t\t%d\t\t%d\n", processes[i].name, processes[i].create_time, processes[i].execute_time);
}
}
void fcfs(Process *processes, int process_num) {
int current_time = 0;
for (int i = 0; i < process_num; i++) {
processes[i].start_time = current_time;
current_time += processes[i].execute_time;
processes[i].end_time = current_time;
processes[i].turnaround_time = processes[i].end_time - processes[i].create_time;
processes[i].weighted_turnaround_time = (float)processes[i].turnaround_time / processes[i].execute_time;
}
}
void sjf(Process *processes, int process_num) {
int current_time = 0;
int remaining_time[MAX_PROCESS_NUM];
for (int i = 0; i < process_num; i++) {
remaining_time[i] = processes[i].execute_time;
}
int completed_num = 0;
while (completed_num < process_num) {
int shortest_index = -1;
int shortest_time = 1000000;
for (int i = 0; i < process_num; i++) {
if (processes[i].create_time <= current_time && remaining_time[i] < shortest_time && remaining_time[i] > 0) {
shortest_index = i;
shortest_time = remaining_time[i];
}
}
if (shortest_index == -1) {
current_time++;
continue;
}
processes[shortest_index].start_time = current_time;
current_time += remaining_time[shortest_index];
processes[shortest_index].end_time = current_time;
processes[shortest_index].turnaround_time = processes[shortest_index].end_time - processes[shortest_index].create_time;
processes[shortest_index].weighted_turnaround_time = (float)processes[shortest_index].turnaround_time / processes[shortest_index].execute_time;
remaining_time[shortest_index] = 0;
completed_num++;
}
}
void rr(Process *processes, int process_num) {
int current_time = 0;
int remaining_time[MAX_PROCESS_NUM];
for (int i = 0; i < process_num; i++) {
remaining_time[i] = processes[i].execute_time;
}
int completed_num = 0;
while (completed_num < process_num) {
for (int i = 0; i < process_num; i++) {
if (processes[i].create_time <= current_time && remaining_time[i] > 0) {
if (remaining_time[i] <= 2) {
current_time += remaining_time[i];
processes[i].end_time = current_time;
processes[i].turnaround_time = processes[i].end_time - processes[i].create_time;
processes[i].weighted_turnaround_time = (float)processes[i].turnaround_time / processes[i].execute_time;
remaining_time[i] = 0;
completed_num++;
} else {
current_time += 2;
remaining_time[i] -= 2;
}
}
}
}
}
void print_result(Process *processes, int process_num) {
printf("进程名称\t开始时间\t结束时间\t周转时间\t带权周转时间\n");
int total_turnaround_time = 0;
float total_weighted_turnaround_time = 0;
for (int i = 0; i < process_num; i++) {
printf("%s\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].name, processes[i].start_time, processes[i].end_time, processes[i].turnaround_time, processes[i].weighted_turnaround_time);
total_turnaround_time += processes[i].turnaround_time;
total_weighted_turnaround_time += processes[i].weighted_turnaround_time;
}
printf("平均周转时间:%.2f\n", (float)total_turnaround_time / process_num);
printf("平均带权周转时间:%.2f\n", total_weighted_turnaround_time / process_num);
}
int main() {
Process processes[MAX_PROCESS_NUM];
int process_num;
int choice;
printf("请选择进程创建方式:\n");
printf("1. 手动创建\n");
printf("2. 随机创建\n");
scanf("%d", &choice);
switch (choice) {
case 1:
manual_create_processes(processes, &process_num);
break;
case 2:
random_create_processes(processes, &process_num);
break;
default:
printf("输入错误!\n");
return 0;
}
printf("您创建的进程如下:\n");
print_processes(processes, process_num);
printf("请选择调度算法:\n");
printf("1. 先来先服务(FCFS)\n");
printf("2. 短作业优先(SJF)\n");
printf("3. 时间片轮转(RR)\n");
scanf("%d", &choice);
switch (choice) {
case 1:
fcfs(processes, process_num);
break;
case 2:
sjf(processes, process_num);
break;
case 3:
rr(processes, process_num);
break;
default:
printf("输入错误!\n");
return 0;
}
printf("调度结果如下:\n");
print_result(processes, process_num);
return 0;
}
关于中文加密和lua closure factory的问题,我可以回答。但是由于我是一个AI,我并不知道您需要的具体内容是什么,所以请您提供更详细的问题描述,我会尽力为您解答。
阅读全文