#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define JOB_COUNT 20 // 后备作业队列大小
#define MEMORY_SIZE 1024 // 用户内存大小
// #define RESOURCE_A 20 // 系统资源A数量
// #define RESOURCE_B 8 // 系统资源B数量
// #define RESOURCE_C 12 // 系统资源C数量
int RESOURCE_A = 20;
int RESOURCE_B = 8;
int RESOURCE_C = 12;
// 作业结构体
typedef struct {
int id; // 作业编号
int memory; // 需要的内存
int resourceA; // 需要的A类资源
int resourceB; // 需要的B类资源
int resourceC; // 需要的C类资源
} Job;

// 进程结构体
typedef struct {
int id; // 进程编号
int remainingTime; // 剩余时间片数
int memory; // 分配到的内存
int resourceA; // 分配到的A类资源
int resourceB; // 分配到的B类资源
int resourceC; // 分配到的C类资源
} Process;

// 动态分区结构体
typedef struct {
int start; // 分区起始地址
int size; // 分区大小
Process *process; // 分配给的进程
} Partition;

Job jobQueue[JOB_COUNT]; // 后备作业队列
Process jobProcesses[JOB_COUNT]; // 作业对应的进程队列
Partition memoryPartition; // 内存分区

// 随机生成作业的内存和资源需求
void generateJobData() {
int i;
for(i = 0; i < JOB_COUNT; i++) {
jobQueue[i].id = i + 1;
jobQueue[i].memory = rand() % (MEMORY_SIZE / 2) + 1;
jobQueue[i].resourceA = rand() % (RESOURCE_A / 2) + 1;
jobQueue[i].resourceB = rand() % (RESOURCE_B / 2) + 1;
jobQueue[i].resourceC = rand() % (RESOURCE_C / 2) + 1;
}
}

// 初始化内存分区
void initializeMemoryPartition() {
memoryPartition.start = 0;
memoryPartition.size = MEMORY_SIZE;
memoryPartition.process = NULL;
}

// 获取当前时间
double getCurrentTime() {
struct timespec timestamp;
clock_gettime(CLOCK_REALTIME, &timestamp);
return timestamp.tv_sec + (double)timestamp.tv_nsec / 1000000000;
}

// 时间片轮转调度算法
void roundRobinScheduling() {
int i, j;
int jobIndex = 0; // 作业队列索引
int processCount = 0; // 进程数量
double currentTime = getCurrentTime();

while(jobIndex < JOB_COUNT && processCount < 5) {
if(jobQueue[jobIndex].memory <= memoryPartition.size &&
jobQueue[jobIndex].resourceA <= RESOURCE_A &&
jobQueue[jobIndex].resourceB <= RESOURCE_B &&
jobQueue[jobIndex].resourceC <= RESOURCE_C) {
// 分配内存和资源
memoryPartition.process = &jobProcesses[processCount];
memoryPartition.process->id = jobQueue[jobIndex].id;
memoryPartition.process->remainingTime = 10; // 时间片大小为10
memoryPartition.process->memory = jobQueue[jobIndex].memory;
memoryPartition.process->resourceA = jobQueue[jobIndex].resourceA;
memoryPartition.process->resourceB = jobQueue[jobIndex].resourceB;
memoryPartition.process->resourceC = jobQueue[jobIndex].resourceC;

memoryPartition.size -= jobQueue[jobIndex].memory;
RESOURCE_A -= jobQueue[jobIndex].resourceA;
RESOURCE_B -= jobQueue[jobIndex].resourceB;
RESOURCE_C -= jobQueue[jobIndex].resourceC;
processCount++;
}
jobIndex++;
}

// 执行作业
for(i = 0; i < processCount; i++) {
while(jobProcesses[i].remainingTime > 0) {
currentTime = getCurrentTime();
jobProcesses[i].remainingTime–;

// 随机请求资源，避免死锁
if(rand() % 2 == 0 && RESOURCE_A >= 1) {
jobProcesses[i].resourceA++;
RESOURCE_A–;
}
if(rand() % 2 == 0 && RESOURCE_B >= 1) {
jobProcesses[i].resourceB++;
RESOURCE_B–;
}
if(rand() % 2 == 0 && RESOURCE_C >= 1) {
jobProcesses[i].resourceC++;
RESOURCE_C–;
}

// 模拟时间片轮转调度
if(getCurrentTime() - currentTime >= 1) {
break;
}
}

// 释放分配的内存和资源
memoryPartition.size += jobProcesses[i].memory;
RESOURCE_A += jobProcesses[i].resourceA;
RESOURCE_B += jobProcesses[i].resourceB;
RESOURCE_C += jobProcesses[i].resourceC;

jobProcesses[i].memory = 0;
jobProcesses[i].resourceA = 0;
jobProcesses[i].resourceB = 0;
jobProcesses[i].resourceC = 0;
}
}

// 显示相关信息
void displayInfo() {
int i;

// 后备作业队列
printf(“后备作业队列：\n”);
for(i = 0; i < JOB_COUNT; i++) {
printf(“作业%d：内存：%dKB，资源A：%d，资源B：%d，资源C：%d\n”, jobQueue[i].id, jobQueue[i].memory, jobQueue[i].resourceA, jobQueue[i].resourceB, jobQueue[i].resourceC);
}
printf(“\n”);

// 进程信息
printf(“进程队列：\n”);
for(i = 0; i < JOB_COUNT; i++) {
if(jobProcesses[i].id > 0) {
printf(“进程%d：剩余时间片：%d，内存：%dKB，资源A：%d，资源B：%d，资源C：%d\n”, jobProcesses[i].id, jobProcesses[i].remainingTime, jobProcesses[i].memory, jobProcesses[i].resourceA, jobProcesses[i].resourceB, jobProcesses[i].resourceC);
}
}
printf(“\n”);

// 内存分配情况
printf(“内存分配情况：\n”);
printf(“分区起始地址：%d，分区大小：%dKB，已使用：%dKB\n”, memoryPartition.start, memoryPartition.size, MEMORY_SIZE - memoryPartition.size);
printf(“\n”);

// 完成作业信息
printf(“完成作业信息：\n”);
for(i = 0; i < JOB_COUNT; i++) {
if(jobProcesses[i].id > 0) {
printf(“作业%d：\n”, jobProcesses[i].id);
printf(“提交时间：%f，进入内存时间：%f，完成时间：%f，周转时间：%f，带权周转时间：%f\n”, 0.0, 0.0, getCurrentTime(), getCurrentTime(), getCurrentTime());
}
}

// 平均周转时间、平均带权周转时间
double sumTurnaroundTime = 0, sumWeightedTurnaroundTime = 0;
for(i = 0; i < JOB_COUNT; i++) {
if(jobProcesses[i].id > 0) {
sumTurnaroundTime += getCurrentTime();
sumWeightedTurnaroundTime += getCurrentTime() / jobProcesses[i].memory;
}
}
printf(“平均周转时间：%f，平均带权周转时间：%f\n”, sumTurnaroundTime / JOB_COUNT, sumWeightedTurnaroundTime / JOB_COUNT);
}

int main() {
srand(time(NULL));

initializeMemoryPartition();
generateJobData();
roundRobinScheduling();
displayInfo();

return 0;
}