用C语言实现FCFS,SPN和RR算法,每个进程由一个进程控制块(PCB)表示,进程控制块包含进程名、到达时间、完成时间、需要运行时间、已用CPU时间、周转时间、带权周转时间、进程状态
时间: 2023-10-20 17:23:58 浏览: 41
以下是用C语言实现FCFS、SPN和RR算法的代码示例,每个进程由一个进程控制块(PCB)表示,进程控制块包含进程名、到达时间、完成时间、需要运行时间、已用CPU时间、周转时间、带权周转时间、进程状态:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESS 10
typedef struct process_control_block {
char name[10];
int arrival_time;
int burst_time;
int start_time;
int finish_time;
int turnaround_time;
float weighted_turnaround_time;
int remaining_time;
int status; // 0: waiting, 1: running, 2: finished
} PCB;
void FCFS(PCB process[], int n);
void SPN(PCB process[], int n);
void RR(PCB process[], int n, int quantum);
int main() {
PCB process[MAX_PROCESS];
int n, i, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
// Input process information
for (i = 0; i < n; i++) {
printf("Enter the name, arrival time, and burst time of process %d: ", i + 1);
scanf("%s %d %d", &process[i].name, &process[i].arrival_time, &process[i].burst_time);
process[i].remaining_time = process[i].burst_time;
process[i].status = 0;
}
// Run FCFS algorithm
printf("\nFCFS Algorithm\n");
FCFS(process, n);
// Run SPN algorithm
printf("\nSPN Algorithm\n");
SPN(process, n);
// Run RR algorithm
printf("\nEnter the time quantum for RR algorithm: ");
scanf("%d", &quantum);
printf("\nRR Algorithm (Time Quantum = %d)\n", quantum);
RR(process, n, quantum);
return 0;
}
void FCFS(PCB process[], int n) {
int i, current_time = 0;
// Sort processes by arrival time
for (i = 0; i < n - 1; i++) {
int j, min_index = i;
for (j = i + 1; j < n; j++) {
if (process[j].arrival_time < process[min_index].arrival_time) {
min_index = j;
}
}
PCB temp = process[i];
process[i] = process[min_index];
process[min_index] = temp;
}
// Run processes
for (i = 0; i < n; i++) {
process[i].start_time = current_time;
process[i].status = 1;
current_time += process[i].burst_time;
process[i].finish_time = current_time;
process[i].turnaround_time = process[i].finish_time - process[i].arrival_time;
process[i].weighted_turnaround_time = (float) process[i].turnaround_time / process[i].burst_time;
process[i].status = 2;
}
// Print results
printf("Name\tArrival Time\tBurst Time\tStart Time\tFinish Time\tTurnaround Time\tWeighted Turnaround Time\n");
for (i = 0; i < n; i++) {
printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", process[i].name, process[i].arrival_time,
process[i].burst_time, process[i].start_time, process[i].finish_time, process[i].turnaround_time,
process[i].weighted_turnaround_time);
}
}
void SPN(PCB process[], int n) {
int i, current_time = 0;
// Sort processes by arrival time
for (i = 0; i < n - 1; i++) {
int j, min_index = i;
for (j = i + 1; j < n; j++) {
if (process[j].arrival_time < process[min_index].arrival_time) {
min_index = j;
}
}
PCB temp = process[i];
process[i] = process[min_index];
process[min_index] = temp;
}
// Run processes
for (i = 0; i < n; i++) {
int j, min_index = i;
for (j = i + 1; j < n; j++) {
if (process[j].arrival_time <= current_time && process[j].remaining_time < process[min_index].remaining_time) {
min_index = j;
}
}
process[min_index].start_time = current_time;
process[min_index].status = 1;
current_time += process[min_index].remaining_time;
process[min_index].finish_time = current_time;
process[min_index].turnaround_time = process[min_index].finish_time - process[min_index].arrival_time;
process[min_index].weighted_turnaround_time = (float) process[min_index].turnaround_time / process[min_index].burst_time;
process[min_index].status = 2;
i--;
}
// Print results
printf("Name\tArrival Time\tBurst Time\tStart Time\tFinish Time\tTurnaround Time\tWeighted Turnaround Time\n");
for (i = 0; i < n; i++) {
printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", process[i].name, process[i].arrival_time,
process[i].burst_time, process[i].start_time, process[i].finish_time, process[i].turnaround_time,
process[i].weighted_turnaround_time);
}
}
void RR(PCB process[], int n, int quantum) {
int i, current_time = 0, remaining_processes = n;
// Sort processes by arrival time
for (i = 0; i < n - 1; i++) {
int j, min_index = i;
for (j = i + 1; j < n; j++) {
if (process[j].arrival_time < process[min_index].arrival_time) {
min_index = j;
}
}
PCB temp = process[i];
process[i] = process[min_index];
process[min_index] = temp;
}
// Run processes
while (remaining_processes > 0) {
for (i = 0; i < n; i++) {
if (process[i].status == 0 && process[i].arrival_time <= current_time) {
process[i].status = 1;
}
if (process[i].status == 1) {
if (process[i].remaining_time <= quantum) {
current_time += process[i].remaining_time;
process[i].remaining_time = 0;
process[i].finish_time = current_time;
process[i].turnaround_time = process[i].finish_time - process[i].arrival_time;
process[i].weighted_turnaround_time = (float) process[i].turnaround_time / process[i].burst_time;
process[i].status = 2;
remaining_processes--;
} else {
current_time += quantum;
process[i].remaining_time -= quantum;
}
}
}
}
// Print results
printf("Name\tArrival Time\tBurst Time\tStart Time\tFinish Time\tTurnaround Time\tWeighted Turnaround Time\n");
for (i = 0; i < n; i++) {
printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", process[i].name, process[i].arrival_time,
process[i].burst_time, process[i].start_time, process[i].finish_time, process[i].turnaround_time,
process[i].weighted_turnaround_time);
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)