JLU《操作系统》课程设计[基于反馈(Feed Back,FB)排队算法的CPU调度的模拟实现]程序代码:// 223.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <STRING.H> //Definitions for memory and string functions.#include <CTYPE.H> //Defines the ctype macros.#include <MALLOC.H> //memory management functions and variables.#include <STDIO.H>#include <STDLIB.H>#include <IO.H> //Definitions for low level I/O functions.#include <PROCESS.H> //Symbols and structures for process management.#include <CONIO.H> //Direct MSDOS console input/output.#include <WINDOWS.H>#include <TIME.H> // Struct and function declarations for dealing with time. #include <DOS.H> //Defines structs, unions, macros, and functions for dealing //with MSDOS and the Intel iAPX86 microprocessor family.// 函数结果状态代码#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2//////////////////////////////////////////typedef int Status; //指定用Status和Boolean代表int类型typedef int Boolean;//////////////////////////////////////////typedef struct QNode{char name[5];int time;int timeType;struct QNode *next;}QNode,*QueuePtr;typedef struct{QueuePtr front;//队头指针QueuePtr rear; // 队尾指针}LinkQueue;int count=0; //时间计数变量LinkQueue qRun,qWait,qReady1,qReady2,qReady3,qReady4;//////////////////////////////////////////////////////////////////////////void menu1();void menu2();void gotoxy(int x,int y);void clrscr(void); //清屏函数void clreol(void); //在文本窗口中清除字符到行末void clreoscr(void); //clear end of screenStatus InitQueue(LinkQueue &Q);Status creatPro(LinkQueue &quePro);void dealTime();void runPro(void);void run();void wait();void wake();void endPro();////////////////////////////////////////////////////////////////////////////DOS界面坐标定位函数/////////////////////////////////////////////////////void gotoxy(int x,int y){CONSOLE_SCREEN_BUFFER_INFO csbiInfo; //variablendklaration HANDLE hConsoleOut;hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);csbiInfo.dwCursorPosition.X = x; //cursorposition X koordinate festlegencsbiInfo.dwCursorPosition.Y = y; //cursorposition Y koordinate festlegenSetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition); //den cursor an die festgelegte koordinate setzen}Status InitQueue(LinkQueue &Q){ // 构造一个空队列Qif(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))exit(OVERFLOW);Q.front->next=NULL;return OK;}Status EnQueue(LinkQueue &Q,char e[5],int proTime,int tType){ // 插入元素e为Q的新的队尾元素QueuePtr p;if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 存储分配失败exit(OVERFLOW);strcpy(p->name,e);p->time=proTime;p->timeType=tType;p->next=NULL;Q.rear->next=p;Q.rear=p;return OK;}Status DeQueue(LinkQueue &Q,char e[5]){ // 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR QueuePtr p;if(Q.front==Q.rear)return ERROR;p=Q.front->next;strcpy(e,p->name);Q.front->next=p->next;if(Q.rear==p)Q.rear=Q.front;free(p);return OK;}Status QueueTraverse(LinkQueue &Q,int x,int y){QueuePtr p;p=Q.front->next;while(p){gotoxy(x,y);printf("%s",p->name);gotoxy(x,y+1);printf("%d",p->time);p=p->next;x+=6;}printf("\n");return OK;}void print(){if(qRun.front!=qRun.rear){QueueTraverse(qRun,17,5);}if(qWait.front!=qWait.rear){QueueTraverse(qWait,17,8);}if(qReady1.front!=qReady1.rear){QueueTraverse(qReady1,17,11);}if(qReady2.front!=qReady2.rear){QueueTraverse(qReady2,17,14);}if(qReady3.front!=qReady3.rear){QueueTraverse(qReady3,17,17);}if(qReady4.front!=qReady4.rear){QueueTraverse(qReady4,17,20);}}Status creatPro(LinkQueue &quePro){char proName[5];int proTime;QueuePtr p;b: gotoxy(22,3);printf("进程名: ");gotoxy(36,3);printf("所需时间: ");gotoxy(30,3);scanf("%s",&proName);gotoxy(46,3);scanf("%d",&proTime);if(proTime<=0){gotoxy(22,3);printf("信息提示: 输入时间错误!请按Enter键返回!");gotoxy(15,3); getchar();getchar();gotoxy(0,0);menu1();goto b;}getchar();if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 存储分配失败exit(OVERFLOW);strcpy(p->name,proName);p->time=proTime;p->timeType=10; //进入时间片为10的就绪队列,即优先级最高的就绪队列p->next=NULL;quePro.rear->next=p;quePro.rear=p;return OK;}void dealTime(){char e[5];int tType=qRun.front->next->timeType;++count;qRun.front->next->time--;if(qRun.front->next->time==0){DeQueue(qRun,e);runPro();count=0;}elseif(qRun.front->next->timeType==count){if(qRun.front->next->timeType==10)EnQueue(qReady2,qRun.front->next->name,qRun.front->next->time,tType+10); if(qRun.front->next->timeType==20)EnQueue(qReady3,qRun.front->next->name,qRun.front->next->time,tType+20);if(qRun.front->next->timeType==40)EnQueue(qReady4,qRun.front->next->name,qRun.front->next->time,80);if(qRun.front->next->timeType==80)EnQueue(qReady4,qRun.front->next->name,qRun.front->next->time,80);DeQueue(qRun,e);runPro();count=0;}}void runPro(void){char e[5];int pTime,f1=0,f2=0,f3=0,f4=0;if(qReady1.front!=qReady1.rear){pTime=qReady1.front->next->time;EnQueue(qRun,qReady1.front->next->name,pTime,qReady1.front->next->timeType);DeQueue(qReady1,e);f1=1; }else{if(qReady2.front!=qReady2.rear){pTime=qReady2.front->next->time;EnQueue(qRun,qReady2.front->next->name,pTime,qReady2.front->next->timeType); DeQueue(qReady2,e);f2=1;}else{if(qReady3.front!=qReady3.rear){pTime=qReady3.front->next->time;EnQueue(qRun,qReady3.front->next->name,pTime,qReady3.front->next->timeType);DeQueue(qReady3,e);f3=1;}else{if(qReady4.front!=qReady4.rear){pTime=qReady4.front->next->time;EnQueue(qRun,qReady4.front->next->name,pTime,qReady4.front->next->timeType);DeQueue(qReady4,e);f4=1;}}}}gotoxy(0,4);menu2();if(f1==0 && f2==0 && f3==0 && f4==0){gotoxy(0,0);menu1();gotoxy(22,3);printf("信息提示:无就绪进程,请输入其他功能选项!");}gotoxy(0,4);menu2();}void run(){if(qRun.front==qRun.rear)runPro();elsedealTime();}void endPro(){char e[5];if(qRun.front==qRun.rear){gotoxy(0,0);menu1();gotoxy(22,3);printf("信息提示:无运行进程,请按Enter键运行进程!");gotoxy(15,3);}else{DeQueue(qRun,e);gotoxy(0,0);menu1();gotoxy(22,3);printf("信息提示:选择菜单功能或按Enter键执行进程!");gotoxy(0,4);menu2();print();} }void wait(){char e[5];if(qRun.front!=qRun.rear){EnQueue(qWait,qRun.front->next->name,qRun.front->next->time,qRun.front->next->timeType); DeQueue(qRun,e);gotoxy(0,4);menu2();print();gotoxy(22,3);printf("信息提示: 选择菜单功能或按Enter键执行进程!");} else{gotoxy(0,0);menu1();gotoxy(22,3);printf("信息提示:无运行进程,请输入其他功能选项!");}}void wake(){char e[5];if(qWait.front!=qWait.rear){EnQueue(qReady1,qWait.front->next->name,qWait.front->next->time,qWait.front->next->timeType );DeQueue(qWait,e);gotoxy(0,4);menu2();print();gotoxy(22,3);printf("信息提示: 选择菜单功能或按Enter键执行进程!");}else{gotoxy(0,0);menu1();gotoxy(22,3);printf("信息提示:无等待进程,请输入其他功能选项!");}}void menu1(){printf(" ┏━━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┓\n");printf(" ┃ 1-> 创建进程┃2-> 撤销进程┃3-> 阻塞进程┃4-> 唤醒进程┃0->退出系统┃\n");printf(" ┣━━━━━━━╋━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┫\n");printf(" ┃菜单选择: ┃┃\n");}void menu2(){printf(" ┣━━━━━┳━┻┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┫\n");printf(" ┃Run: ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┃Time ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┣━━━━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━┫\n");printf(" ┃Wait: ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┃Time ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┣━━━━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━┫\n");printf(" ┃Ready1 ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┃Time:10 ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┣━━━━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━┫\n");printf(" ┃Ready2 ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┃Time:20 ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┣━━━━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━┫\n");printf(" ┃Ready3 ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┃Time:40 ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┣━━━━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━╋━━┫\n");printf(" ┃Ready4 ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┃Time:80 ┃┃┃┃┃┃┃┃┃┃┃\n");printf(" ┗━━━━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┛\n");}void main(){char a=NULL;system("color 0B");InitQueue(qRun);InitQueue(qWait);InitQueue(qReady1);InitQueue(qReady2);InitQueue(qReady3);InitQueue(qReady4);menu1();menu2();gotoxy(15,3);scanf("%c",&a);while(1){gotoxy(0,0);menu1();switch(a){case '1':creatPro(qReady1);print();gotoxy(22,3);printf("信息提示: 选择菜单功能或按Enter键执行进程!");while(1){f: gotoxy(15,3);a=getchar();if(a=='\n'){gotoxy(22,3);printf("信息提示: 选择菜单功能或按Enter键执行进程!"); run();gotoxy(0,4);menu2();print();}elsebreak;}getchar();break;case '2':endPro();goto f;break;case '3':wait(); goto f; print(); break;case '4':wake(); goto f; print(); break;case '0': gotoxy(22,3); exit(0); break;default:gotoxy(22,3);printf("信息提示: 输入错误!请选择正确的菜单功能选项!"); goto f;}print();}}。