进程调度算法c语言代码

以下是一个简单的进程调度算法的C语言代码示例:

#include <stdio.h>

#define MAX_PROCESS 10

typedef struct {
    int pid;  // 进程ID
    int burst_time;  // 执行时间
    int waiting_time;  // 等待时间
    int turnaround_time;  // 周转时间
} Process;

void fcfs(Process processes[], int n) {
    int total_waiting_time = 0;
    int total_turnaround_time = 0;
    
    // 计算每个进程的等待时间和周转时间
    for (int i = 0; i < n; i++) {
        if (i == 0) {
            processes[i].waiting_time = 0;
        } else {
            processes[i].waiting_time = processes[i-1].waiting_time + processes[i-1].burst_time;
        }
        
        processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
        
        total_waiting_time += processes[i].waiting_time;
        total_turnaround_time += processes[i].turnaround_time;
    }
    
    // 打印每个进程的等待时间和周转时间
    printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
    for (int i = 0; i < n; i++) {
        printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
    }
    
    // 打印平均等待时间和平均周转时间
    printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);
    printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}

int main() {
    Process processes[MAX_PROCESS];
    int n;
    
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    
    printf("Enter burst time for each process:\n");
    for (int i = 0; i < n; i++) {
        processes[i].pid = i + 1;
        printf("Process %d: ", i + 1);
        scanf("%d", &processes[i].burst_time);
    }
    
    fcfs(processes, n);
    
    return 0;
}

这个示例代码实现了一个简单的先来先服务(FCFS)调度算法。用户需要输入进程的数量和每个进程的执行时间,然后程序会计算每个进程的等待时间和周转时间,并打印出来。最后,程序会计算平均等待时间和平均周转时间,并打印出来。

除了先来先服务(FCFS)调度算法外,还有其他常见的进程调度算法,如短作业优先(SJF)、最高响应比优先(HRRN)、轮转调度(Round Robin)等。下面是这些算法的简单实现代码:

短作业优先(SJF)调度算法:

#include <stdio.h>

#define MAX_PROCESS 10

typedef struct {
    int pid;  // 进程ID
    int burst_time;  // 执行时间
    int waiting_time;  // 等待时间
    int turnaround_time;  // 周转时间
} Process;

void sjf(Process processes[], int n) {
    int total_waiting_time = 0;
    int total_turnaround_time = 0;
    
    // 对进程按照执行时间进行排序
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            if (processes[j].burst_time > processes[j+1].burst_time) {
                Process temp = processes[j];
                processes[j] = processes[j+1];
                processes[j+1] = temp;
            }
        }
    }
    
    // 计算每个进程的等待时间和周转时间
    for (int i = 0; i < n; i++) {
        if (i == 0) {
            processes[i].waiting_time = 0;
        } else {
            processes[i].waiting_time = processes[i-1].waiting_time + processes[i-1].burst_time;
        }
        
        processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
        
        total_waiting_time += processes[i].waiting_time;
        total_turnaround_time += processes[i].turnaround_time;
    }
    
    // 打印每个进程的等待时间和周转时间
    printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
    for (int i = 0; i < n; i++) {
        printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
    }
    
    // 打印平均等待时间和平均周转时间
    printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);
    printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}

int main() {
    Process processes[MAX_PROCESS];
    int n;
    
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    
    printf("Enter burst time for each process:\n");
    for (int i = 0; i < n; i++) {
        processes[i].pid = i + 1;
        printf("Process %d: ", i + 1);
        scanf("%d", &processes[i].burst_time);
    }
    
    sjf(processes, n);
    
    return 0;
}

最高响应比优先(HRRN)调度算法:

#include <stdio.h>

#define MAX_PROCESS 10

typedef struct {
    int pid;  // 进程ID
    int burst_time;  // 执行时间
    int waiting_time;  // 等待时间
    int turnaround_time;  // 周转时间
    float response_ratio;  // 响应比
} Process;

void hrrn(Process processes[], int n) {
    int total_waiting_time = 0;
    int total_turnaround_time = 0;
    
    // 计算每个进程的等待时间、周转时间和响应比
    for (int i = 0; i < n; i++) {
        if (i == 0) {
            processes[i].waiting_time = 0;
        } else {
            processes[i].waiting_time = processes[i-1].waiting_time + processes[i-1].burst_time;
        }
        
        processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
        
        processes[i].response_ratio = (float)processes[i].turnaround_time / processes[i].burst_time;
        
        total_waiting_time += processes[i].waiting_time;
        total_turnaround_time += processes[i].turnaround_time;
    }
    
    // 打印每个进程的等待时间、周转时间和响应比
    printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\tResponse Ratio\n");
    for (int i = 0; i < n; i++) {
        printf("%d\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].pid, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time, processes[i].response_ratio);
    }
    
    // 打印平均等待时间和平均周转时间
    printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);
    printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}

int main() {
    Process processes[MAX_PROCESS];
    int n;
    
    printf("Enter the number of processes: ");
    scanf("%d", &n);
    
    printf("Enter burst time for each process:\n");
    for (int i = 0; i < n; i++) {
        processes[i].pid = i + 1;
        printf("Process %d: ", i + 1);
        scanf("%d", &processes[i].burst_time);
    }
    
    hrrn(processes, n);
    
    return 0;
}

轮转调度(Round Robin)算法:

#include <stdio.h>

#define MAX_PROCESS 10
#define TIME_QUANTUM 2

typedef struct {
    int pid;  // 进程ID
    int burst_time;  // 执行时间
    int waiting_time;  // 等待时间
    int turnaround_time;  // 周转时间
} Process;

void round_robin(Process processes[], int n) {
    int total_waiting_time = 0;
    int total_turnaround_time = 0;
    int remaining_time[MAX_PROCESS];
    
    // 初始化剩余时间数组
    for (int i = 0; i < n; i++) {
        remaining_time[i] = processes[i].burst_time;
    }
    
    int time = 0;
    int completed = 0;
    
    // 执行轮转调度算法
    while (completed != n) {
        for (int i = 0; i < n; i++) {
            if (remaining_time[i] > 0) {
                if (remaining_time[i] <= TIME_QUANTUM) {
                    time += remaining_time[i];
                    remaining_time[i] = 0;
                    processes[i].waiting_time = time - processes[i].burst_time;
                    processes[i].turnaround_time = time;
                    completed++;
                } else {
                    time += TIME_QUANTUM;
                    remaining_time[i] -= TIME_QUANTUM;
                }
            }
        }
    }
    
    // 计算总的等待时间和周转时间
    for (int i = 0; i < n; i++) {
        total_waiting_time += processes[i].waiting_time;
        total_turnaround_time += processes[i].turnaround_time;
    }
    
    // 打印每个进程的等待时间和周转时间
    printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
    for (int i = 0; i < n; i++) {
        printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
    }
    
    // 打印平均等待时间和平均周转时间
    printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);
    printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}

int main() {
    Process processes[MAX_PROCESS];
    int n;
    
    printf("Enter the number of processes: ")