根据以下要求,写出三个相对应的可运行的c语言程序。实验三 简单文件系统设计 一、 实验目的 (1)加深对进程概念的理解,明确进程和程序的区别 (2)深入理解系统如何组织进程 (3)理解常用进程调度算法的具体实现 二、 实验内容 编写C程序模拟实现单处理机系统中的进程调度算法,实现对多个进程的调度模拟,要求采用常见进程调度算法(如先来先服务、时间片轮转和优先级调度等算法)进行模拟调度。
时间: 2024-03-14 17:48:39 浏览: 51
C语言实现房屋管理系统
程序一:先来先服务调度算法
```c
#include<stdio.h>
#include<stdlib.h>
#define MAX_PROCESS 10
struct Process {
int pid; // 进程id
int arrival_time; // 到达时间
int burst_time; // 执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
void FCFS(struct Process processes[], int n) {
int time = 0;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("\nFCFS Scheduling:\n");
for(int i = 0; i < n; i++) {
printf("\nProcess %d:\n", processes[i].pid);
printf("Arrival Time: %d\n", processes[i].arrival_time);
printf("Burst Time: %d\n", processes[i].burst_time);
if(time < processes[i].arrival_time) {
time = processes[i].arrival_time;
}
processes[i].waiting_time = time - processes[i].arrival_time;
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
time += processes[i].burst_time;
printf("Waiting Time: %d\n", processes[i].waiting_time);
printf("Turnaround Time: %d\n", processes[i].turnaround_time);
avg_waiting_time += processes[i].waiting_time;
avg_turnaround_time += processes[i].turnaround_time;
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("\nAverage Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
}
int main() {
struct Process processes[MAX_PROCESS];
int n;
printf("Enter number of processes (max %d): ", MAX_PROCESS);
scanf("%d", &n);
printf("\nEnter process details:\n");
for(int i = 0; i < n; i++) {
printf("\nProcess %d:\n", i+1);
processes[i].pid = i+1;
printf("Arrival Time: ");
scanf("%d", &processes[i].arrival_time);
printf("Burst Time: ");
scanf("%d", &processes[i].burst_time);
}
FCFS(processes, n);
return 0;
}
```
程序二:时间片轮转调度算法
```c
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define MAX_PROCESS 10
#define TIME_QUANTUM 2
struct Process {
int pid; // 进程id
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_burst_time; // 剩余执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
void RR(struct Process processes[], int n) {
int time = 0, completed_processes = 0, i = 0;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("\nRound Robin Scheduling:\n");
while(completed_processes < n) {
if(processes[i].remaining_burst_time > 0) {
if(processes[i].remaining_burst_time > TIME_QUANTUM) {
time += TIME_QUANTUM;
processes[i].remaining_burst_time -= TIME_QUANTUM;
}
else {
time += processes[i].remaining_burst_time;
processes[i].waiting_time = time - processes[i].arrival_time - processes[i].burst_time;
processes[i].turnaround_time = time - processes[i].arrival_time;
completed_processes++;
processes[i].remaining_burst_time = 0;
printf("\nProcess %d:\n", processes[i].pid);
printf("Waiting Time: %d\n", processes[i].waiting_time);
printf("Turnaround Time: %d\n", processes[i].turnaround_time);
avg_waiting_time += processes[i].waiting_time;
avg_turnaround_time += processes[i].turnaround_time;
}
}
i = (i+1) % n;
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("\nAverage Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
}
int main() {
struct Process processes[MAX_PROCESS];
int n;
printf("Enter number of processes (max %d): ", MAX_PROCESS);
scanf("%d", &n);
printf("\nEnter process details:\n");
for(int i = 0; i < n; i++) {
printf("\nProcess %d:\n", i+1);
processes[i].pid = i+1;
printf("Arrival Time: ");
scanf("%d", &processes[i].arrival_time);
printf("Burst Time: ");
scanf("%d", &processes[i].burst_time);
processes[i].remaining_burst_time = processes[i].burst_time;
}
RR(processes, n);
return 0;
}
```
程序三:优先级调度算法
```c
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define MAX_PROCESS 10
struct Process {
int pid; // 进程id
int arrival_time; // 到达时间
int burst_time; // 执行时间
int priority; // 优先级
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
};
void PS(struct Process processes[], int n) {
int time = 0, completed_processes = 0, i = 0;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("\nPriority Scheduling:\n");
while(completed_processes < n) {
int min_priority = INT_MAX, min_priority_idx = -1;
for(int j = 0; j < n; j++) {
if(processes[j].arrival_time <= time && processes[j].priority < min_priority && processes[j].burst_time > 0) {
min_priority = processes[j].priority;
min_priority_idx = j;
}
}
if(min_priority_idx == -1) {
time++;
continue;
}
processes[min_priority_idx].burst_time--;
if(processes[min_priority_idx].burst_time == 0) {
completed_processes++;
processes[min_priority_idx].waiting_time = time - processes[min_priority_idx].arrival_time - processes[min_priority_idx].burst_time;
processes[min_priority_idx].turnaround_time = time - processes[min_priority_idx].arrival_time;
printf("\nProcess %d:\n", processes[min_priority_idx].pid);
printf("Waiting Time: %d\n", processes[min_priority_idx].waiting_time);
printf("Turnaround Time: %d\n", processes[min_priority_idx].turnaround_time);
avg_waiting_time += processes[min_priority_idx].waiting_time;
avg_turnaround_time += processes[min_priority_idx].turnaround_time;
}
time++;
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("\nAverage Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
}
int main() {
struct Process processes[MAX_PROCESS];
int n;
printf("Enter number of processes (max %d): ", MAX_PROCESS);
scanf("%d", &n);
printf("\nEnter process details:\n");
for(int i = 0; i < n; i++) {
printf("\nProcess %d:\n", i+1);
processes[i].pid = i+1;
printf("Arrival Time: ");
scanf("%d", &processes[i].arrival_time);
printf("Burst Time: ");
scanf("%d", &processes[i].burst_time);
printf("Priority (higher value means higher priority): ");
scanf("%d", &processes[i].priority);
}
PS(processes, n);
return 0;
}
```
阅读全文