以下是使用C语言设计的数据结构和功能实现:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define MAX_JOBS 20
#define MAX_MEMORY 1024
#define MAX_A_RESOURCES 6
#define MAX_B_RESOURCES 3
#define MAX_C_RESOURCES 4
#define MAX_RUNTIME 5
#define TIME_QUANTUM 2
typedef struct {
int job_id;
int memory;
int a_resources;
int b_resources;
int c_resources;
int runtime;
int arrival_time;
int completion_time;
int turnaround_time;
float weight_turnaround_time;
} Job;
typedef struct {
int start_address;
int size;
bool allocated;
} MemoryBlock;
int total_a_resources = 20;
int total_b_resources = 8;
int total_c_resources = 12;
MemoryBlock memory[MAX_MEMORY];
Job createJob(int job_id) {
Job job;
job.job_id = job_id;
job.memory = (rand() % 256) + 1;
job.a_resources = (rand() % 6) + 1;
job.b_resources = (rand() % 3) + 1;
job.c_resources = (rand() % 4) + 1;
job.runtime = (rand() % 5) + 1;
job.arrival_time = 0;
job.completion_time = 0;
job.turnaround_time = 0;
job.weight_turnaround_time = 0.0;
return job;
}
void initializeMemory() {
for (int i = 0; i < MAX_MEMORY; i++) {
memory[i].allocated = false;
}
}
bool allocateMemory(int job_id, int size) {
for (int i = 0; i < MAX_MEMORY; i++) {
if (!memory[i].allocated && memory[i].size >= size) {
memory[i].allocated = true;
memory[i].size = size;
return true;
}
}
return false;
}
void deallocateMemory(int job_id) {
for (int i = 0; i < MAX_MEMORY; i++) {
if (memory[i].allocated && memory[i].job_id == job_id) {
memory[i].allocated = false;
break;
}
}
}
void printJob(Job job) {
printf("作业ID: %d\n", job.job_id);
printf("内存需求: %dKB\n", job.memory);
printf("A类资源需求: %d\n", job.a_resources);
printf("B类资源需求: %d\n", job.b_resources);
printf("C类资源需求: %d\n", job.c_resources);
printf("运行时间: %d\n", job.runtime);
printf("到达时间: %d\n", job.arrival_time);
printf("完成时间: %d\n", job.completion_time);
printf("周转时间: %d\n", job.turnaround_time);
printf("带权周转时间: %.2f\n", job.weight_turnaround_time);
printf("------------------------------\n");
}
void printMemoryAllocation() {
printf("内存分配情况:\n");
for (int i = 0; i < MAX_MEMORY; i++) {
printf("Memory Block %d: ", i);
if (memory[i].allocated) {
printf("已分配 大小: %dKB\n", memory[i].size);
} else {
printf("未分配\n");
}
}
printf("------------------------------\n");
}
void calculateMetrics(Job jobs[], int num_jobs) {
int total_turnaround_time = 0;
float total_weight_turnaround_time = 0.0;
for (int i = 0; i < num_jobs; i++) {
Job job = jobs[i];
job.turnaround_time = job.completion_time - job.arrival_time;
job.weight_turnaround_time = (float) job.turnaround_time / job.runtime;
total_turnaround_time += job.turnaround_time;
total_weight_turnaround_time += job.weight_turnaround_time;
printJob(job);
}
float average_turnaround_time = (float) total_turnaround_time / num_jobs;
float average_weight_turnaround_time = total_weight_turnaround_time / num_jobs;
printf("平均周转时间: %.2f\n", average_turnaround_time);
printf("平均带权周转时间: %.2f\n", average_weight_turnaround_time);
}
int main() {
srand(time(NULL));
Job jobs[MAX_JOBS];
for (int i = 0; i < MAX_JOBS; i++) {
jobs[i] = createJob(i + 1);
}
initializeMemory();
for (int i = 0; i < MAX_JOBS; i++) {
Job job = jobs[i];
printf("作业 %d 到达时间: 0\n", job.job_id);
if (!allocateMemory(job.job_id, job.memory)) {
printf("内存不足,作业 %d 未分配内存\n", job.job_id);
continue;
}
printf("作业 %d 进入内存\n", job.job_id);
printf("------------------------------\n");
int runtime = job.runtime;
int execution_time = 0;
while (runtime > 0) {
int time_slice = TIME_QUANTUM;
if (runtime < TIME_QUANTUM) {
time_slice = runtime;
}
printf("作业 %d 开始执行时间片: %d\n", job.job_id, time_slice);
runtime -= time_slice;
execution_time += time_slice;
if (execution_time % 3 == 0) {
int random_resource = (rand() % 3) + 1;
switch (random_resource) {
case 1: // A类资源
if (job.a_resources < MAX_A_RESOURCES && total_a_resources > 0) {
job.a_resources++;
total_a_resources--;
printf("作业 %d 请求A类资源\n", job.job_id);
}
break;
case 2: // B类资源
if (job.b_resources < MAX_B_RESOURCES && total_b_resources > 0) {
job.b_resources++;
total_b_resources--;
printf("作业 %d 请求B类资源\n", job.job_id);
}
break;
case 3: // C类资源
if (job.c_resources < MAX_C_RESOURCES && total_c_resources > 0) {
job.c_resources++;
total_c_resources--;
printf("作业 %d 请求C类资源\n", job.job_id);
}
break;
}
}
if (execution_time % 6 == 0) {
int random_resource = (rand() % 3) + 1;
switch (random_resource) {
case 1: // A类资源
if (job.a_resources > 0) {
job.a_resources--;
total_a_resources++;
printf("作业 %d 释放A类资源\n", job.job_id);
}
break;
case 2: // B类资源
if (job.b_resources > 0) {
job.b_resources--;
total_b_resources++;
printf("作业 %d 释放B类资源\n", job.job_id);
}
break;
case 3: // C类资源
if (job.c_resources > 0) {
job.c_resources--;
total_c_resources++;
printf("作业 %d 释放C类资源\n", job.job_id);
}
break;
}
}
}
deallocateMemory(job.job_id);
job.completion_time = job.arrival_time + execution_time;
jobs[i] = job;
printf("作业 %d 执行完毕\n", job.job_id);
printf("------------------------------\n");
if (i == 9) {
printf("执行十个作业后的相关信息:\n");
for (int j = 0; j < 5; j++) {
printJob(jobs[j]);
}
printMemoryAllocation();
}
}
printf("结束时的相关信息:\n");
calculateMetrics(jobs, MAX_JOBS);
printMemoryAllocation();
return 0;
}
该程序使用了结构体来表示作业和内存块,并实现了作业调度、内存分配以及相关信息的计算。其中,MAX_JOBS表示作业队列的最大容量,MAX_MEMORY表示内存大小,MAX_A_RESOURCES、MAX_B_RESOURCES和MAX_C_RESOURCES分别表示A、B和C类资源的总数,MAX_RUNTIME表示作业的最大运行时间,TIME_QUANTUM表示时间片的大小。