📄 diaodu.cpp
字号:
#include <stdio.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#define INPUT "D:\\input.txt"//文件输入流的地址,可适当修改
int suanfa;
#define WAIT 'W'
#define RUN 'R'
typedef struct PCB_Prioriy
{
char name[25];
int priority;
int needTime;
int CPUTime;
char state;
struct PCB_Prioriy *next;
}PCB_Prioriy; //优先数算法的pcb控制模块
PCB_Prioriy *head_Prioriy = NULL; //头指针
typedef struct PCB_RoundRobin
{
char name[25];
int CPUTime;
int needTime;
int count;
int round;
char state;
struct PCB_RoundRobin *next;
}PCB_RoundRobin; //时间片轮转算法的pcb控制模块
PCB_RoundRobin *head_RoundRobin = NULL; //头指针
int len_PCB_RoundRobin; //pcb模块的长度
void Insert_priority(PCB_Prioriy *p)//优先数算法的插入函数
{
PCB_Prioriy *p1, *p2;
bool insertTail = true;
if( head_Prioriy==NULL || p->priority > head_Prioriy->priority ) // 插入队首
{
p->next = head_Prioriy;
head_Prioriy = p;
}
else
{
p1 = head_Prioriy;
while(p1->next != NULL)
{
p1 = p1->next;
if( p1->priority < p->priority ) // 找到插入点
{
insertTail = false;
break;
}
}
if(insertTail) // 插入队尾
p1->next = p;
else
{
p2 = head_Prioriy;
while(p2->next != p1)
p2 = p2->next;
p2->next = p;
p->next = p1;
}
}
}
void Insert_Tail(PCB_RoundRobin *p)//时间片轮转算法的插入
{
PCB_RoundRobin *tail;
if(head_RoundRobin == NULL)
{
p->next = head_RoundRobin;
head_RoundRobin = p;
}
else
{
tail = head_RoundRobin;
while(tail->next != NULL)
tail = tail->next;
tail->next = p;
}
}
void OutputInfo_priority()//输出优先数算法的所有信息
{
PCB_Prioriy *p;
head_Prioriy->state = RUN;
p = head_Prioriy;
while(p != NULL)
{
printf("%s\t%d\t%d\t%d\t%c\n",p->name,p->CPUTime,p->needTime,p->priority,p->state);
p = p->next;
}
}
void OutputInfo_roundRobin()//输出时间片转算法的所有信息
{
PCB_RoundRobin *p;
head_RoundRobin->state = RUN;
p = head_RoundRobin;
while(p != NULL)
{
printf("%s\t%d\t%d\t%d\t%d\t%c\n",p->name,p->CPUTime,p->needTime,p->count,p->round,p->state);
p = p->next;
}
}
void ReadWork_priority() //建立进程控制块函数_优先数算法
{
ifstream fin(INPUT);
PCB_Prioriy *p = (PCB_Prioriy*)malloc(sizeof(PCB_Prioriy));
while(fin>> p->name >> p->needTime)
{
p->CPUTime = 0 ;
p->next = NULL;
p->priority = 50 - p->needTime; //初始值50-needTime
p->state = WAIT;
Insert_priority(p);//调用优先数算法的插入函数
p = (PCB_Prioriy*)malloc(sizeof(PCB_Prioriy));
}
}
void ReadWork_roundRobin()//建立进程控制块函数_时间片转算法
{
ifstream fin(INPUT);
PCB_RoundRobin *p = (PCB_RoundRobin*)malloc(sizeof(PCB_RoundRobin));
while(fin>> p->name >> p->needTime)
{
p->count = 0;
p->CPUTime = 0;
p->next = NULL;
p->round = 1;
p->state = WAIT;
Insert_Tail(p);//时间片轮转算法的插入函数
p = (PCB_RoundRobin*)malloc(sizeof(PCB_RoundRobin));
}
}
void Processing_priority() //建立进程查看函数
{
PCB_Prioriy *p_Running;
OutputInfo_priority();
p_Running = head_Prioriy;
head_Prioriy = p_Running->next;
p_Running->next = NULL;
p_Running->needTime--;//needTime--
if(p_Running->needTime==0)//当needTime等于0时,说明此进程已完成
{
printf("进程%s已完成!\n",p_Running->name);
free(p_Running);
}
else
{
p_Running->priority -= 3;
p_Running->CPUTime++;
p_Running->state = WAIT;
Insert_priority(p_Running);
}
}
void Processing_roundRobin()//建立进程查看函数
{
PCB_RoundRobin *p_Running;
OutputInfo_roundRobin();
p_Running = head_RoundRobin;
head_RoundRobin = p_Running->next;
p_Running->next = NULL;
p_Running->needTime -= 2;//needTime-=2
if(p_Running->needTime <= 0)//当needTime此时已小于0,说明此进程已完成
{
printf("进程%s已完成!\n",p_Running->name);
free(p_Running);
}
else
{
p_Running->count++;
p_Running->CPUTime += 2;
p_Running->state = WAIT;
Insert_Tail(p_Running);
}
}
int main()
{
int CurrentTime = 0;//当前时间控制
printf("选择算法:\n"); //算法的选择
printf("1.优先数算法\n");
printf("2.时间片轮转算法\n");
scanf("%d",&suanfa);
if(suanfa==1) //若选择优先数算法
{
ReadWork_priority(); //建立优先数算法的进程控制块函数
while(head_Prioriy != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("\nCurrentTime = %d :\n",CurrentTime);
printf("NAME CPUTIME NEEDTIME PRIORITY STATE\n");
CurrentTime++; //当前时间增长
Processing_priority(); //调用优先数算法的进程查看函数
}
}
else if(suanfa==2) //若选择时间片轮转算法
{
ReadWork_roundRobin(); //建立时间片轮转算法的进程控制块函数
PCB_RoundRobin *p = head_RoundRobin; //使轮转的头与pcb控制块的指针一致
len_PCB_RoundRobin = 0; //长度先赋为0
while(p != NULL) //使P指向链尾,并记录链长
{
len_PCB_RoundRobin++;
p = p->next;
}
int roundPCB = 0; //计数器先赋0
while(head_RoundRobin != NULL)
{
getchar(); //停住,防止在win98里不能察看
printf("\nCurrentTime = %d :\n",CurrentTime);
printf("NAME CPUTIME NEEDTIME COUNT ROUND STATE\n");
CurrentTime += 2; //使当前时间增二
if(roundPCB == len_PCB_RoundRobin)
{
p = head_RoundRobin;
len_PCB_RoundRobin = 0;
while(p != NULL)
{
p->round++;
len_PCB_RoundRobin++;
p = p->next;
}
roundPCB = 0;
}
roundPCB++; //计数器递增
Processing_roundRobin(); //调用时间片轮转的进程查看函数
}
}
else //否则没有相对应的算法
{
printf("没找到算法!\n");
return 0;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -