C语言编写多级反馈队列算法代码和最高响应比算法代码
时间: 2023-10-03 21:07:18 浏览: 88
多级反馈队列算法代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int front1 = -1, rear1 = -1; // Queue 1
int front2 = -1, rear2 = -1; // Queue 2
int front3 = -1, rear3 = -1; // Queue 3
int queue1[MAX], queue2[MAX], queue3[MAX];
void enQueue(int queue[], int *front, int *rear, int data) {
if (*rear == MAX - 1) {
printf("Queue is full!\n");
return;
}
if (*front == -1) {
*front = 0;
}
*rear += 1;
queue[*rear] = data;
}
int deQueue(int queue[], int *front, int *rear) {
if (*front == -1) {
printf("Queue is empty!\n");
return -1;
}
int data = queue[*front];
if (*front == *rear) {
*front = -1;
*rear = -1;
} else {
*front += 1;
}
return data;
}
void executeProcess(int process, int time) {
printf("Process %d executed for %d seconds\n", process, time);
}
void MLFQ(int processes[], int n) {
int i, j;
// Add all processes to queue 1
for (i = 0; i < n; i++) {
enQueue(queue1, &front1, &rear1, processes[i]);
}
// Execute processes in the queues until all queues are empty
while (front1 != -1 || front2 != -1 || front3 != -1) {
// Execute processes in queue 1
while (front1 != -1) {
int process = deQueue(queue1, &front1, &rear1);
executeProcess(process, 1);
// Move the process to queue 2
enQueue(queue2, &front2, &rear2, process);
}
// Execute processes in queue 2
while (front2 != -1) {
int process = deQueue(queue2, &front2, &rear2);
executeProcess(process, 2);
// Move the process to queue 3
enQueue(queue3, &front3, &rear3, process);
}
// Execute processes in queue 3
while (front3 != -1) {
int process = deQueue(queue3, &front3, &rear3);
executeProcess(process, 3);
// Move the process back to queue 3
enQueue(queue3, &front3, &rear3, process);
}
}
}
int main() {
int processes[] = {1, 2, 3, 4, 5};
int n = sizeof(processes) / sizeof(processes[0]);
MLFQ(processes, n);
return 0;
}
```
最高响应比算法代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct {
int process;
int burstTime;
int waitingTime;
int responseTime;
int turnaroundTime;
} Process;
void swap(Process *xp, Process *yp)
{
Process temp = *xp;
*xp = *yp;
*yp = temp;
}
void sort(Process *processes, int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (processes[j].responseTime < processes[j + 1].responseTime) {
swap(&processes[j], &processes[j + 1]);
}
}
}
}
void executeProcess(Process process, int time) {
printf("Process %d executed for %d seconds\n", process.process, time);
}
void HRRN(Process processes[], int n) {
int i, j, currentTime = 0;
// Calculate the response time for each process
for (i = 0; i < n; i++) {
processes[i].responseTime = currentTime;
currentTime += processes[i].burstTime;
}
// Sort the processes by response time
sort(processes, n);
// Execute the processes
for (i = 0; i < n; i++) {
executeProcess(processes[i], processes[i].burstTime);
}
}
int main() {
Process processes[] = {
{1, 10, 0, 0, 0},
{2, 5, 0, 0, 0},
{3, 8, 0, 0, 0},
{4, 7, 0, 0, 0},
{5, 6, 0, 0, 0},
};
int n = sizeof(processes) / sizeof(processes[0]);
HRRN(processes, n);
return 0;
}
```
阅读全文