1.设计一个有几个进程并发执行的进程调度程序,每个进程由一个进程控制块(PCB)表示,进程控制块通常应包括下述信息:进程名,进程优先数,进程需要运行的时间,占用CPU的时间以及进程的状态等,且可按照调度算法的不同而增删。 2.调度程序应包含2—3种不同的调度算法,运行时可以任选一种,以利于各种方法的分析和比较。 3.系统应能显示或打印各进程状态和参数的变化情况,便于观察。
时间: 2024-02-12 09:05:30 浏览: 107
以下是一个基于命令行界面的简单进程调度程序,包含了先来先服务(FCFS)和最短作业优先(SJF)两种调度算法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROCESSES 10
enum process_status {
READY, RUNNING, BLOCKED, TERMINATED
};
struct process {
char name[16];
int priority;
int arrival_time;
int burst_time;
int remaining_time;
int cpu_time;
int io_time;
enum process_status status;
};
struct process_queue {
struct process *processes[MAX_PROCESSES];
int front, rear;
};
void enqueue(struct process_queue *queue, struct process *p) {
if (queue->rear == MAX_PROCESSES - 1) {
printf("Error: Queue is full.\n");
exit(1);
}
queue->processes[++queue->rear] = p;
}
struct process *dequeue(struct process_queue *queue) {
if (queue->front == queue->rear) {
printf("Error: Queue is empty.\n");
exit(1);
}
return queue->processes[++queue->front];
}
struct process *create_process(char *name, int priority, int arrival_time, int burst_time) {
struct process *p = (struct process *) malloc(sizeof(struct process));
strcpy(p->name, name);
p->priority = priority;
p->arrival_time = arrival_time;
p->burst_time = burst_time;
p->remaining_time = burst_time;
p->cpu_time = 0;
p->io_time = 0;
p->status = READY;
return p;
}
void print_process(struct process *p) {
printf("%s\t%d\t%d\t%d\t%d\t%d\n", p->name, p->priority, p->arrival_time, p->burst_time, p->cpu_time, p->io_time);
}
void print_queue(struct process_queue *queue) {
printf("Name\tPriority\tArrival Time\tBurst Time\tCPU Time\tIO Time\n");
for (int i = queue->front + 1; i <= queue->rear; i++) {
print_process(queue->processes[i]);
}
}
void fcfs(struct process *processes[], int n) {
struct process_queue ready_queue = { .front = -1, .rear = -1 };
struct process_queue blocked_queue = { .front = -1, .rear = -1 };
struct process *running_process = NULL;
int current_time = 0;
int completed = 0;
while (completed < n) {
// 将到达时间为当前时间的进程添加到就绪队列
for (int i = 0; i < n; i++) {
if (processes[i]->arrival_time == current_time) {
enqueue(&ready_queue, processes[i]);
processes[i]->status = READY;
}
}
// 如果没有正在运行的进程,则从就绪队列中取出一个进程并运行
if (running_process == NULL && ready_queue.front != ready_queue.rear) {
running_process = dequeue(&ready_queue);
running_process->status = RUNNING;
}
// 运行当前进程1个时间单位
if (running_process != NULL) {
running_process->cpu_time++;
running_process->remaining_time--;
// 如果当前进程已经运行完毕,则将其标记为终止状态,并统计总时间
if (running_process->remaining_time == 0) {
running_process->status = TERMINATED;
completed++;
printf("[%d] %s terminated.\n", current_time, running_process->name);
running_process = NULL;
}
// 如果当前进程需要进行I/O操作,则将其标记为阻塞状态并添加到阻塞队列
else if (running_process->cpu_time == running_process->io_time) {
running_process->status = BLOCKED;
printf("[%d] %s blocked.\n", current_time, running_process->name);
enqueue(&blocked_queue, running_process);
running_process = NULL;
}
}
// 如果当前没有正在运行的进程,则从就绪队列中取出一个进程并运行
if (running_process == NULL && ready_queue.front != ready_queue.rear) {
running_process = dequeue(&ready_queue);
running_process->status = RUNNING;
}
// 如果当前没有正在运行的进程且阻塞队列不为空,则从阻塞队列中取出一个进程并运行
if (running_process == NULL && blocked_queue.front != blocked_queue.rear) {
running_process = dequeue(&blocked_queue);
running_process->status = RUNNING;
}
current_time++;
}
}
void sjf(struct process *processes[], int n) {
struct process_queue ready_queue = { .front = -1, .rear = -1 };
struct process_queue blocked_queue = { .front = -1, .rear = -1 };
struct process *running_process = NULL;
int current_time = 0;
int completed = 0;
while (completed < n) {
// 将到达时间为当前时间的进程添加到就绪队列
for (int i = 0; i < n; i++) {
if (processes[i]->arrival_time == current_time) {
enqueue(&ready_queue, processes[i]);
processes[i]->status = READY;
}
}
// 如果没有正在运行的进程,则从就绪队列中取出一个最短作业并运行
if (running_process == NULL && ready_queue.front != ready_queue.rear) {
struct process *shortest_job = ready_queue.processes[ready_queue.front + 1];
for (int i = ready_queue.front + 2; i <= ready_queue.rear; i++) {
if (ready_queue.processes[i]->remaining_time < shortest_job->remaining_time) {
shortest_job = ready_queue.processes[i];
}
}
running_process = shortest_job;
dequeue(&ready_queue);
running_process->status = RUNNING;
}
// 运行当前进程1个时间单位
if (running_process != NULL) {
running_process->cpu_time++;
running_process->remaining_time--;
// 如果当前进程已经运行完毕,则将其标记为终止状态,并统计总时间
if (running_process->remaining_time == 0) {
running_process->status = TERMINATED;
completed++;
printf("[%d] %s terminated.\n", current_time, running_process->name);
running_process = NULL;
}
// 如果当前进程需要进行I/O操作,则将其标记为阻塞状态并添加到阻塞队列
else if (running_process->cpu_time == running_process->io_time) {
running_process->status = BLOCKED;
printf("[%d] %s blocked.\n", current_time, running_process->name);
enqueue(&blocked_queue, running_process);
running_process = NULL;
}
}
// 如果当前没有正在运行的进程,则从就绪队列中取出一个最短作业并运行
if (running_process == NULL && ready_queue.front != ready_queue.rear) {
struct process *shortest_job = ready_queue.processes[ready_queue.front + 1];
for (int i = ready_queue.front + 2; i <= ready_queue.rear; i++) {
if (ready_queue.processes[i]->remaining_time < shortest_job->remaining_time) {
shortest_job = ready_queue.processes[i];
}
}
running_process = shortest_job;
dequeue(&ready_queue);
running_process->status = RUNNING;
}
// 如果当前没有正在运行的进程且阻塞队列不为空,则从阻塞队列中取出一个进程并运行
if (running_process == NULL && blocked_queue.front != blocked_queue.rear) {
running_process = dequeue(&blocked_queue);
running_process->status = RUNNING;
}
current_time++;
}
}
int main() {
struct process *processes[MAX_PROCESSES];
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
char name[16];
int priority, arrival_time, burst_time;
printf("Enter the name, priority, arrival time, and burst time of process %d: ", i + 1);
scanf("%s %d %d %d", name, &priority, &arrival_time, &burst_time);
processes[i] = create_process(name, priority, arrival_time, burst_time);
}
printf("Select a scheduling algorithm:\n");
printf("1. First-Come, First-Served (FCFS)\n");
printf("2. Shortest Job First (SJF)\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
fcfs(processes, n);
break;
case 2:
sjf(processes, n);
break;
default:
printf("Error: Invalid choice.\n");
exit(1);
}
return 0;
}
```
该程序通过命令行界面接受用户输入的进程信息,包括进程名、优先级、到达时间和所需CPU时间等,并根据用户选择的调度算法进行进程调度。程序将进程状态和参数的变化情况输出,便于用户观察和分析各种调度算法的优缺点。
阅读全文