会的帮做下!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了会的帮做下!相关的知识,希望对你有一定的参考价值。
实验项目名称:实现模拟进程调度的程序
方案设想:用计算机编程语言,任选C、C++或者VC或VB、JAVA等都可以用来设计本道程序,系统平台可以是Linux也可以是windows;实现一个简单模拟进程调度,给出5个进程,然后按照任选一种调度算法来调度进程。
typedef struct procedure
/*char[15] procName;进程名*/
int procID; /*进程的内部标志*/
int priority; /*进程的优先权数规定,小数级别高*/
int needTime; /*总共所需执行的时间片数*/
int cpuTime; /*已经执行情况的次数*/
char procStatus; /*进程状态,-r运行,-w就绪,-f完成*/
struct procedure * nextProc; /*指向下一个进程的指针*/
proc,*procPoint;
procPoint CreatePCBList(int n); /*创建进程链表*/
void PrintPCBList(procPoint PCBList); /*打印进程链表*/
void DestroyPCBList(procPoint PCBList); /*释放进程链表空间*/
void RoundRabin(procPoint PCBList); /*时间片调度算法,每执行一次,needTime-1,cpuTime+1*/
void compositor(proc **_PCBList); /*连表排序,由小优先级到大*/
void priority(procPoint PCBList,procPoint finishList); /*优先级调度算法,priority小,优先级高*/
/**********************************************/
main()
int num=0;
char method;
procPoint PCBList,finishList;
printf("input the numbers of procedure:");
scanf("%d",&num);
PCBList = NULL;
finishList = NULL;
PCBList = CreatePCBList(num);
/* PrintPCBList(finishList);*/
/*选择算法*/
printf("\\nwhat method do you want to use? (r / p)======[r-RoundRabin; p-Priority]");
do
scanf("%c",&method);
printf("\\n");
while (method != \'r\' && method != \'p\');
printf("\\nPCB procID priority needTime cpuTime procStatus nextProc");
if (method == \'r\')
RoundRabin(PCBList);
else
priority(PCBList,finishList);
DestroyPCBList(PCBList);
DestroyPCBList(finishList);
getchar();
/*********************************************/
/*创建进程链表*/
procPoint CreatePCBList(int n)
int k;
procPoint PCBList = NULL;
procPoint PCBTail,newProc;
PCBTail=PCBList;
while (PCBTail->nextProc != NULL)
PCBTail = PCBTail -> nextProc;
printf("intput the %d procedures\' [priority] [needTime] and [status](w-wait/f-finish):\\n",n);
for (k=0;k<n;k++)
newProc = (procPoint)malloc(sizeof(struct procedure));
if(k==0) PCBList=newProc;
scanf("%d,%d,%c",&newProc -> priority,&newProc -> needTime,&newProc -> procStatus);
newProc -> procID = k;
newProc -> cpuTime = 0;
newProc -> nextProc = NULL;
PCBTail -> nextProc = newProc;
PCBTail = newProc;
printf("\\nthe PCB list has been created as below:\\n");
printf("\\nPCB procID priority needTime cpuTime procStatus nextProc");
PrintPCBList(PCBList);
return PCBList;
/*打印进程链表*/
void PrintPCBList(procPoint PCBList)
int n=0;
procPoint PCBTail = NULL;
PCBTail = PCBList;
while (PCBTail != NULL)
printf("\\nPCB[%d] %d %d %d %d %c",n,PCBTail->procID,PCBTail->priority,PCBTail->needTime,PCBTail->cpuTime,PCBTail->procStatus);
PCBTail = PCBTail -> nextProc;
if (PCBTail != NULL)
printf(" %d",PCBTail->procID);
else
printf(" NULL");
n=n++;
printf("\\n");
/*释放进程链表空间*/
void DestroyPCBList(procPoint PCBList)
procPoint thisProc;
while (PCBList != NULL)
thisProc = PCBList;
PCBList = PCBList -> nextProc;
free(thisProc);
printf("...one procedure despost!\\n");
printf("procedure list space released successfuly!\\n");
/*======================================*/
/*时间片调度算法,每执行一次,needTime-1,cpuTime+1*/
void RoundRabin(procPoint PCBList)
int allF = 1; /*标志每此循环后是否还有未结束进程,有则为1*/
procPoint thisProc;
thisProc = PCBList;
while (allF == 1)
thisProc = PCBList;
allF = 0;
while (thisProc !=NULL)
if (thisProc -> procStatus != \'f\')
thisProc -> cpuTime += 1;
thisProc -> needTime -= 1;
if (thisProc -> needTime <= 0)
thisProc -> procStatus = \'f\';
else
allF = 1;
thisProc = thisProc -> nextProc; /*指针指向下一进程*/
PrintPCBList(PCBList); /*打印一次循环后的结果*/
getchar();
/*===========================================*/
/*排序链表,由小到大*/
void compositor(proc ** _PCBList)
/*修改成指针的指针时,此处多次出现不匹配错误,原因是:相应的函数声明未修改*/
procPoint p,q,r;
procPoint PCBList;
PCBList = *_PCBList;
if (PCBList == NULL || PCBList ->nextProc == NULL)
return;
p = PCBList;
q = p -> nextProc;
r = q -> nextProc;
while (r != NULL)
if (q -> priority > r ->priority)
q -> nextProc = r->nextProc;
p -> nextProc = r;
r -> nextProc = q;
p = q;
q = r;
r = r -> nextProc;
p = PCBList -> nextProc;
if (PCBList -> priority > p -> priority)
PCBList -> nextProc = p -> nextProc;
p -> nextProc = PCBList;
PCBList = p;
*_PCBList = p;
/*优先权调度算法--约定小数优先,每执行一次,优先数+2,cputime+1,needtime-1*/
void priority(procPoint PCBList,procPoint finishList)
procPoint thisProc,p,q,fPointer;
thisProc = PCBList;
while (thisProc != NULL)
/*将状态为\'f\'的进程放入已完成队列,并从就绪队列中删除*/
if (PCBList -> procStatus == \'f\')
if (finishList == NULL)
finishList = PCBList;
PCBList = PCBList -> nextProc;
finishList -> nextProc = NULL;
else
fPointer = finishList;
while (fPointer -> nextProc != NULL)
fPointer = fPointer -> nextProc;
fPointer -> nextProc = PCBList;
PCBList = PCBList ->nextProc;
fPointer = fPointer -> nextProc;
fPointer -> nextProc = NULL;
compositor(&PCBList);
/*此处出错,PCBList在函数中的改变未传递回来*/
PCBList -> priority += 2; /*进程的优先权数规定,小数级别高*/
PCBList -> needTime -= 1; /*总共所需执行的时间片数*/
PCBList -> cpuTime += 1; /*已经执行情况的次数*/
if (PCBList -> needTime <= 0)
PCBList -> procStatus = \'f\';
PrintPCBList(PCBList);
PrintPCBList(finishList);
thisProc = PCBList;
getchar();
参考技术A 你自己做吧,百度可不是用来做作业的.
以上是关于会的帮做下!的主要内容,如果未能解决你的问题,请参考以下文章