时间片轮询算法
一.代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define n 10//进程名最大长度
#define MAX 5//进程数
//定义进程类型
typedef struct pcb {
char name[n];
struct pcb* next;
int arrivaltime;
int runtime;
int flag;
}PCB;
int timecount = 0;
PCB progress[MAX];
PCB* run = NULL;
//定义队列
typedef struct {
PCB* front;
PCB* rear;
}LinkQueen;
LinkQueen* CreateQueen(LinkQueen* q) {
q = (LinkQueen*)malloc(sizeof(LinkQueen));
q->front = q->rear = NULL;
return q;
}
LinkQueen* DestroyQueen(LinkQueen* q) {
PCB* pre = q->front, * p;
if (pre != NULL) {
p = pre->next;
while (p != NULL) {
free(pre);
pre = p; p = p->next;
}
free(pre);
}
free(q);
return q;
}
int QueenEmpty(LinkQueen* q) {
if (q->rear == NULL)
return 1;
return 0;
}
LinkQueen* enQueen(LinkQueen* q, PCB* e) {
PCB* p = NULL;
p = (PCB*)malloc(sizeof(PCB));
p->arrivaltime = e->arrivaltime;
p->flag = e->flag;
for (int i = 0; p->name[i] != '\0'; i++)
p->name[i] = e->name[i];
p->runtime = e->runtime;
p->next = NULL;
if (q->rear == NULL) {
q->front = p;
q->rear = p;
}
else {
q->rear->next = p;
q->rear = p;
}
return q;
}
//输入函数
void input() {
for (int i = 0; i < MAX; i++) {
scanf("%s", &progress[i].name);
scanf("%d %d", &progress[i].arrivaltime, &progress[i].runtime);
progress[i].flag = 0;
progress[i].next = NULL;
}
}
void Print() {
printf("当前时间片: %d到%d\t", timecount, timecount + 1);
printf("运行当前进程:%s\t", run->name);
printf("当前进程剩余时间:%d\t", run->runtime);
printf("此时状态flag=%d\t\n", run->flag);
}
//得到最晚到达的时间
int getlatest() {
int max = 0;
for (int i = 0; i < MAX; i++)
if (progress[i].arrivaltime > max)
max = progress[i].arrivaltime;
return max;
}
//得到结束时间
int getendtime() {
int sum = 0;
for (int i = 0; i < MAX; i++)
sum += progress[i].runtime;
return sum;
}
LinkQueen* isarrival(LinkQueen* q) {
for (int i = 0; i < MAX; i++)
if (progress[i].arrivaltime == timecount)
q = enQueen(q, &progress[i]);
return q;
}
void isterminate() {
if (run->runtime == 0)
run->flag = -1;
}
//进程运行操作
LinkQueen* running(LinkQueen* q) {
if (run == NULL) {
if (QueenEmpty(q))
return q;
if (q->front == q->rear) {
run = q->front;
q->front = q->rear = NULL;
run->runtime--;
isterminate();
Print();
}
else {
run = q->front;
q->front = q->front->next;
run->runtime--;
isterminate();
Print();
}
}
else {
if (run->flag != -1) {
if (QueenEmpty(q)) {
run->runtime--;
isterminate();
Print();
}
else {
q->rear->next = run;
q->rear = run;
run = q->front;
q->front = q->front->next;
run->runtime--;
isterminate();
Print();
}
}
else {
free(run);
run = NULL;
if (QueenEmpty(q))
return q;
if (q->front == q->rear) {
run = q->front;
q->front = q->rear = NULL;
run->runtime--;
isterminate();
Print();
}
else {
run = q->front;
q->front = q->front->next;
run->runtime--;
isterminate();
Print();
}
}
}
return q;
}
//CPU操作
void CPU(LinkQueen* q) {
while (1) {
if (timecount <= getlatest()) {
q = isarrival(q);
q = running(q);
timecount++;
}
else {
if (timecount >= getendtime()) {
printf("over!\n");
q = DestroyQueen(q);
break;
}
q = running(q);
timecount++;
}
}
}
int main() {
LinkQueen* q = NULL;
q = CreateQueen(q);
input();
CPU(q);
}
二.思路
三.运行结果