#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct pcb{
char name[10];//进程名
int time; //进程的总时间
int runtime;//进程已经占用的cpu时间
int needtime;//进程还需要的时间
char state;//进程运行状态:w(wait) or r(runing)
struct pcb *next;
}pcb,*PCB;
typedef struct queue{
char name[10];
int timeSlice; //时间片的大小
PCB mPcb;
struct queue *next;
}queue, *QUEUE;
QUEUE queueHead = NULL;
PCB pcbHead = NULL;
PCB pcbTail = NULL;
PCB pcbHead_2 = NULL;
PCB pcbTail_2 = NULL;
bool isFinish = false;
char finishName[10] = {};
/**
* 创建好队列
*/
void createQueue(){
int m,n; //就绪队列数
QUEUE q;
printf("请输入就绪队列数: ");
scanf("%d", &m);
n = m; //记录m的值
if(m!=0){
queueHead = (QUEUE)malloc(sizeof(queue));
queueHead->next = NULL;
q = queueHead;
}
m = m-1;
while(m>0){
q->next = (QUEUE)malloc(sizeof(queue));
q = q->next;
q->next = NULL;
m--;
}
q = queueHead;
for(int i=1;i<=n;i++){
printf("请输入队列%d的名字:", i);
scanf("%s", &q->name);
printf("请输入队列%d的时间片大小:", i);
scanf("%d", &q->timeSlice);
q->mPcb = NULL;
if(i!=n){
q = q->next;
}else{
q->next = NULL;
}
}
}
/**
* 创建进程加入就绪队列的第一个队列
*/
void createProcessInQueue(QUEUE queueHead){
int m, n;
PCB p;
printf("请输入要创建的进程数:");
scanf("%d", &m);
n = m;
if(m!=0){
pcbHead = pcbTail = (PCB)malloc(sizeof(pcb));
pcbHead->next = NULL;
p = pcbHead;
}
m--;
while(m>0){
p->next = (PCB)malloc(sizeof(pcb));
p = p->next;
pcbTail = p;
p->next = NULL;
m--;
}
p = pcbHead;
for(int i=1;i<=n;i++){
printf("请输入进程%d的名字:", i);
scanf("%s", &p->name);
printf("输入进程%d的总时间:",i);
scanf("%d",&p->time);
p->runtime = 0;
p->needtime = p->time;
p->state = 'w';
if(i!=n){
p = p->next;
}else{
p->next = NULL;
}
}
queueHead->mPcb = pcbHead;
}
/**
* 打印一队列里面的进程
*/
void printProcessInQueue(PCB mPcb){
PCB p1 = mPcb;
printf("进程名 进程总时间 进程已经运行的时间 进程还需要的时间 进程状态\n");
while(p1!=NULL){
printf("%s ", p1->name);
printf("%d ", p1->time);
printf("%d ", p1->runtime);
printf("%d ", p1->needtime);
printf("%c ", p1->state);
printf("\n");
if(p1->next==NULL){
p1 = NULL;
}else{
p1 = p1->next;
}
}
}
/**
* 打印各个队列的进程列表
*/
void printQueue(){
QUEUE mQ = queueHead;
while(mQ!=NULL){
printf(" %s队列里面的进程状态如下(时间片大小是 %d):\n", mQ->name, mQ->timeSlice);
printf("--------------------------------------------------------------------\n");
if(mQ->mPcb!=NULL){
printProcessInQueue(mQ->mPcb);
}
printf("--------------------------------------------------------------------\n");
printf("\n");
mQ = mQ->next;
}
if(isFinish==true){
printf(" %s 进程已经运行完毕", finishName);
isFinish = false;
strcpy(finishName, "");
}
getchar();
getchar();
}
/**
* 打印等待状态的进程列表
*/
void printQueueOfWait(){
system("cls");
printf(" **************************************\n");
printf(" * CPU等待 *\n");
printf(" **************************************\n\n");
//打印一下状态
printQueue();
}
/**
* 打印运行状态的进程列表
*/
void printQueueOfRun(){
system("cls");
printf(" **************************************\n");
printf(" * CPU运行 *\n");
printf(" **************************************\n\n");
//打印一下状态
printQueue();
}
/**
* 轮转法
*/
void runAsRoundRobin(QUEUE nQ, int timeSlice){
while(pcbHead!=NULL){
pcbHead->runtime += timeSlice;
pcbHead->needtime = pcbHead->time - pcbHead->runtime;
pcbHead->state = 'r';
if(pcbHead->needtime<0){
pcbHead->needtime = 0;
}
//打印进程队列
printQueueOfRun();
if(pcbHead->needtime==0){
isFinish = true;
strcpy(finishName, pcbHead->name);
}else if(pcbHead->needtime>0){
pcbTail->next = (PCB)malloc(sizeof(pcb));
pcbTail = pcbTail->next;
strcpy(pcbTail->name, pcbHead->name);
pcbTail->time = pcbHead->time;
pcbTail->runtime = pcbHead->runtime;
pcbTail->needtime = pcbHead->needtime;
pcbTail->state = 'w';
pcbTail->next = NULL;
}
if(pcbHead->next!=NULL){
pcbHead = pcbHead->next;
}else{
pcbHead = NULL;
}
nQ->mPcb = pcbHead;
printQueueOfWait();
}
}
/**
* 运行一下
*/
void run(QUEUE nQ, QUEUE nQ_next, int timeSlice){
if(pcbHead!=NULL){
pcbHead->runtime += timeSlice;
pcbHead->needtime = pcbHead->time - pcbHead->runtime;
pcbHead->state = 'r';
if(pcbHead->needtime<0){
pcbHead->needtime = 0;
}
//打印进程队列
printQueueOfRun();
if(pcbHead->needtime==0){
isFinish = true;
strcpy(finishName, pcbHead->name);
}
//如果尚未运行结束,将其放到下一个就绪队列的进程列表中
if(pcbHead->needtime>0){
if(nQ_next->mPcb==NULL){
pcbHead_2 = pcbTail_2 = (PCB)malloc(sizeof(pcb));
strcpy(pcbHead_2->name, pcbHead->name);
pcbHead_2->time = pcbHead->time;
pcbHead_2->runtime = pcbHead->runtime;
pcbHead_2->needtime = pcbHead->needtime;
pcbHead_2->state = 'w';
pcbTail_2->next = NULL;
}else{
PCB node = (PCB)malloc(sizeof(pcb));
strcpy(node->name, pcbHead->name);
node->time = pcbHead->time;
node->runtime = pcbHead->runtime;
node->needtime = pcbHead->needtime;
node->state = 'w';
node->next = NULL;
pcbTail_2->next = node;
pcbTail_2 = pcbTail_2->next;
}
}
if(pcbHead->next!=NULL){
pcbHead = pcbHead->next;
}else{
pcbHead = NULL;
}
nQ_next->mPcb = pcbHead_2;
nQ->mPcb = pcbHead;
}
}
/**
* 运行当前的就绪队列
*/
void runQueueAt(QUEUE nQ){
QUEUE nQ_next = NULL;
PCB nP;
int timeSlice;
while(pcbHead!=NULL){
//打印状态
printQueueOfWait();
timeSlice = nQ->timeSlice; //获取当前就绪队列的时间片
if(nQ->next==NULL){
system("cls");
//当前是最后一个就绪队列,将进行轮转法调度
runAsRoundRobin(nQ, nQ->timeSlice);
}else{
nQ_next = nQ->next;
run(nQ, nQ_next, timeSlice);
}
}
}
int main(){
createQueue();
createProcessInQueue(queueHead);
//定义一个当前要运行的进程
QUEUE nowQueue = queueHead;
while(pcbHead!=NULL){
runQueueAt(nowQueue);
nowQueue = nowQueue->next;
pcbHead = pcbHead_2;
pcbTail = pcbTail_2;
pcbHead_2 = pcbTail_2 = NULL;
}
//打印状态
printQueueOfWait();
printf(" 全部进程运行完毕,");
return 0;
}