我正在用c语言制作SPN模拟器,现在代码也做FCFS和SRT,但这些工作得很好。我认为这是它计算开始时间的方式,但我还没能修复它。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX_PROCESSES 100
// Define a struct to represent a process
typedef struct {
char name;
int arrival_time;
int service_time;
int start_time;
int finish_time;
int wait_time;
int turnaround_time;
} Process;
// Define a function to simulate the processes using the SPN scheduling algorithm
void spn(Process* processes, int num_processes) {
int current_time = 0;
int remaining_times[MAX_PROCESSES];
int completed_processes = 0;
// initialize remaining times array
for (int i = 0; i < num_processes; i++) {
remaining_times[i] = processes[i].service_time;
}
while (completed_processes < num_processes) {
int shortest_time = INT_MAX;
int shortest_index = -1;
// find process with shortest remaining time
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival_time <= current_time && remaining_times[i] < shortest_time && remaining_times[i] > 0) {
shortest_time = remaining_times[i];
shortest_index = i;
}
}
if (shortest_index == -1) {
current_time++;
continue;
}
// calculate start time
if (current_time < processes[shortest_index].arrival_time) {
processes[shortest_index].start_time = processes[shortest_index].arrival_time;
current_time = processes[shortest_index].arrival_time;
} else {
processes[shortest_index].start_time = current_time;
}
// execute process for 1 unit of time
remaining_times[shortest_index]--;
current_time++;
// check if process is completed
if (remaining_times[shortest_index] == 0) {
// calculate finish time, wait time, and turnaround time
processes[shortest_index].finish_time = current_time;
processes[shortest_index].wait_time = processes[shortest_index].start_time - processes[shortest_index].arrival_time;
processes[shortest_index].turnaround_time = processes[shortest_index].finish_time - processes[shortest_index].arrival_time;
completed_processes++;
}
}
}
int main() {
int num_processes;
char name;
int arrival_time;
int service_time;
char temp;
int choice;
// Read the number of processes from the user
printf("Enter the number of processes: ");
scanf("%d", &num_processes);
// Allocate memory for the array of processes
Process* processes = (Process*)malloc(num_processes * sizeof(Process));
// Read the process information from the user
for (int i = 0; i < num_processes; i++) {
scanf("%c", &temp);
printf("Enter the name, arrival time, and service time of process %d: ", i + 1);
scanf("%c %d %d", &name, &arrival_time, &service_time);
// Copy the process information to the array of processes
processes[i].name = name;
processes[i].arrival_time = arrival_time;
processes[i].service_time = service_time;
// Initialize the remaining fields
processes[i].start_time = -1;
processes[i].finish_time = -1;
processes[i].wait_time = -1;
processes[i].turnaround_time = -1;
}
// Prompt the user to choose between FCFS and SPN
printf("Enter 1 for FCFS, 2 for SPN, or 3 for SRT: ");
scanf("%d", &choice);
if (choice == 1) {
// Sort the processes by arrival time (FCFS)
fcfs_sort(processes, num_processes);
// Simulate the processes using the FCFS scheduling algorithm
fcfs_simulate(processes, num_processes);
// Print the results for FCFS
printf("FCFSn");
printf("%-8s %-14s %-12s %-10s %-11s %-9s %-15sn", "Process", "Arrival Time", "Service Time", "Start Time", "Finish Time", "Wait Time", "Turnaround Time");
for (int i = 0; i < num_processes; i++) {
printf("%-8c %-14d %-12d %-10d %-11d %-9d %-15dn", processes[i].name, processes[i].arrival_time, processes[i].service_time, processes[i].start_time, processes[i].finish_time, processes[i].wait_time, processes[i].turnaround_time);
}
// Draw the Gantt chart for FCFS
draw_gantt_chart(processes, num_processes);
}
else if (choice == 2) {
// Sort the processes by shortest processing time (SPN)
spn(processes, num_processes);
// Print the results for SPN
printf("SPNn");
printf("%-8s %-14s %-12s %-10s %-11s %-9s %-15sn", "Process", "Arrival Time", "Service Time", "Start Time", "Finish Time", "Wait Time", "Turnaround Time");
for (int i = 0; i < num_processes; i++) {
printf("%-8c %-14d %-12d %-10d %-11d %-9d %-15dn", processes[i].name, processes[i].arrival_time, processes[i].service_time, processes[i].start_time, processes[i].finish_time, processes[i].wait_time, processes[i].turnaround_time);
}
// Draw the Gantt chart for SPN
draw_gantt_chart(processes, num_processes);
}
else if (choice == 3) {
// Simulate the processes using the SRT scheduling algorithm
srt(processes, num_processes);
// Print the results for SRT
printf("SRTn");
printf("%-8s %-14s %-12s %-10s %-11s %-9s %-15sn", "Process", "Arrival Time", "Service Time", "Start Time", "Finish Time", "Wait Time", "Turnaround Time");
for (int i = 0; i < num_processes; i++) {
printf("%-8c %-14d %-12d %-10d %-11d %-9d %-15dn", processes[i].name, processes[i].arrival_time, processes[i].service_time, processes[i].start_time, processes[i].finish_time, processes[i].wait_time, processes[i].turnaround_time);
}
// Draw the Gantt chart for SRT
draw_gantt_chart(processes, num_processes);
}
// Free the memory
free(processes);
return 0;
}
输入如下表所示:Process Arrival Time服务时间A 0 3B 2 6C 4 4D 6 5E 8 2
,输出应该如下所示:流程到达时间服务时间开始时间结束时间等待时间周转时间A 0 3 0 3 0 3B 2 6 3 9 1711 .答案:CD 5 5 15 20 5 14E 8 2 9 11 1 3
但我得到:流程到达时间服务时间开始时间结束时间等待时间周转时间A 0 3 2 3 2 3B 6 14 15 12 13C 4 4 7 8 3 4D 6 5 19 20 13 14E 8 2 9 10 1 2
我一直在摆弄SPN函数,但除了更多的错误答案之外,它没有产生任何结果
我认为这是它计算开始时间的方式
确实,这就是为什么开始时间是错误的原因-它们在进程运行时每单位时间都被错误地重新计算,而不是在开始时设置一次,如:
// calculate start time
if (processes[shortest_index].start_time < 0)
processes[shortest_index].start_time = current_time;
但这还不是全部——Finish Time也是错误的,因为你编写了一个抢占式算法,而SPN是非抢占式的。