
#include "malloc.h"
#include "string.h"
typedef struct node{
int time
int name
char statement
int num
struct node *next
}node,*L
void createL(L &l,int n){
l=(L)malloc(sizeof(node))
if(!l)
printf("error!")
else
l->next=NULL
L p,q
q=l
for(int i=0i<ni++){
p=(L)malloc(sizeof(node))
printf("请输入进程的名字name:\n")
scanf("%d",&p->name)
getchar()
printf("请输入该进程的运行时间time:\n")
scanf("%d",&p->time)
printf("请输入其优先级数num:\n")
scanf("%d",&p->num)
getchar()
printf("请输入其状态:\n")
p->statement=getchar()
p->next=q->next
q->next=p
q=p
getchar()
}
}
void traL(L &l){
L p
p=l->next
printf("进程名\t运行时间\t优先数\t状态\n")
while(p){
printf(" %d\t%5d\t%11d\t %c",p->name,p->time,p->num,p->statement)
printf("\n")
p=p->next
}
}
void Sort(L &l)
{
L tail=NULL
while(tail!= l->next)
{
L pre = l
L cur = pre->next
while(cur != tail &&cur->next != tail)
{
if( cur->num <cur->next->num )
{
pre->next = cur->next
cur->next = cur->next->next
pre->next->next = cur
}
pre = pre->next
cur = pre->next
}
tail = cur
}
}
void run(L &l){
Sort(l)
L p,r,q
q=l
if(l->next!=NULL)
p=l->next
int j=0
printf("第k次运行\t进程名\t运行时间\t优先数\t状态\n")
while(p!=NULL){
j++
printf("%5d\t %d\t%5d\t%11d\t %c",j,p->name,p->time,p->num,p->statement)
printf("\n")
p->num--
p->time--
if(p->time==0){
p->statement='E'
r=p
if(p->next!=NULL)
{
l->next=p->next
free(r)
p=l->next
}
else
break
}
else{
Sort(l)
if(l->next!=NULL)
p=l->next
else
break
}
}
}
void main(){
L l
int n
printf("请输入进程的数目n:\n")
scanf("%d",&n)
createL(l,n)
traL(l)
run(l)
}
//这是运行过的,应该符合你的要求!
#include <stdio.h>#include <stdlib.h>//提供atoi()函数
#include <conio.c>//提供clrscr()函数
#define M 10 //字符串大小常量
#define N 3 //进程数常量
#define SLOT 2
typedef struct node{
char name[M]
int prio //优先级
int round //时间片长度
int cputime//已经使用的cpu时间
int needtime//需要多少cpu时间
int count //计数器
char state//进程的当前状态
struct node *next //指向下一个进程
}PCB
PCB *finish,*ready,*tail,*run
void ShowHead(char *s1,char *s2)
int Menu_Select()
void Creat(int select)
void InsertPriority(PCB *q)
void InsertSlot(PCB *q)
void PrintOneProcess(int select,PCB *q)
void PrintAllProcess(int select)
void FirstIn()
void FIFODo(int select)
void PriorityDo(int select)
void SlotDo(int select)
void Quit()
main()
{
while(1)
{
clrscr()
switch(Menu_Select())
{
case 1:
Creat(1)
FIFODo(1)
printf("\n\n\t\t按回车键返回菜单...")
getchar()
getchar()
break
case 2:
Creat(2)
PriorityDo(2)
printf("\n\n\t\t按回车键返回菜单...")
getchar()
getchar()
break
case 3:
Creat(3)
SlotDo(3)
printf("\n\n\t\t按回车键返回菜单...")
getchar()
getchar()
break
case 0:
Quit()
break
}
}
}
/*打印每个界面的开头显示*/
void ShowHead(char *s1,char *s2)
{
printf("\t ==================%s========================\n\n",s1)
printf("\t\t\t\t%s\n\n",s2)
printf("\t ========================================================\n\n")
}
/*主菜单*/
int Menu_Select()
{
int choose
char s[2]
clrscr()
printf("====================进程调度算法模拟程序=====================\n\n")
printf("\t\t 1.先进先出调度策略\n")
printf("\t\t 2.优先数调度策略\n")
printf("\t\t 3.时间片轮转调度策略\n")
printf("\t\t 0.退出系统\n\n")
printf("=============================================================\n\n")
do
{
printf("\n请输入你的选择(0-3):")
scanf("%s",s)
choose=atoi(s)
}while(choose<0 || choose>3)
return choose
}
/*创建调度算法的链表*/
void Creat(int select)
{
PCB *p,*q//q为就绪队列的最后一个结点
int i,time,rounds
char na[M]
ready=NULL
finish=NULL
run=NULL
if(select==1) //先进先出调度创建
{
printf("\nEnter name and time of process:\n")
for(i=1i<=Ni++)
{
p=(PCB *)malloc(sizeof(PCB))
scanf("%s",na)
scanf("%d",&time)
strcpy(p->name,na)
p->cputime=0
p->needtime=time
p->state='W'
p->next=NULL
if(ready!=NULL) //就绪队列不为空
{
q->next=p
q=p
}
else //就绪队列为空
{
p->next=ready
ready=p
q=ready
}
}
clrscr()
ShowHead("先进先出调度策略","FIFO dispatching algorithm ")
printf("\t\tname cputimeneedtime state\n")
PrintAllProcess(select)//打印出初始状态的信息
}
else if(select==2) //优先数调度创建
{
printf("\nEnter name and time of process:\n")
for(i=1i<=Ni++)
{
p=(PCB *)malloc(sizeof(PCB))
scanf("%s",na)
scanf("%d",&time)
strcpy(p->name,na)
p->cputime=0
p->needtime=time
p->state='W'
p->prio=50-time
if(ready!=NULL) //就绪队列不为空的时候
InsertPriority(p)
else //就绪队列为空
{
p->next=ready
ready=p
}
}//end of for()
clrscr()
ShowHead("优先级调度策略","Priority dispatching algorithm ")
printf("\t\tname cputimeneedtimepriostate\n")
PrintAllProcess(select)//打印出初始状态的信息
}//end of else if()
else if(select==3) //时间片轮转调度创建
{
printf("\nEnter name and the time of process:\n")
for(i=1i<=Ni++)
{
p=(PCB *)malloc(sizeof(PCB))
scanf("%s",na)
scanf("%d",&time)
strcpy(p->name,na)
p->cputime=0
p->needtime=time
p->count=0 //计数器
p->round=SLOT
p->state='W'
if(ready!=NULL)
InsertSlot(p)
else
{
p->next=ready
ready=p
}
}
clrscr()
ShowHead("时间片轮转调度策略","Time slot dispatching algorithm ")
printf("\n\t\tname cputimeneedtime count slotstate\n")
PrintAllProcess(select)//打印出初始状态的信息
}
run=ready//从就绪队列取一个进程,使其运行,同时就绪队列的头指针后移
run->state='R'
ready=ready->next
}
/*优先调度:把进程插入到合适的位置,就绪队列按优先级由高到低的顺序排列*/
void InsertPriority(PCB *q)
{
PCB *pre,*p
int flag=1
pre=NULL
p=ready
while(p!=NULL &&flag)
{
if(p->prio>q->prio)
{
pre=p
p=p->next
}
else
flag=0
}
if(pre==NULL)
{
q->next=ready
ready=q
}
else
{
pre->next=q
q->next=p
}
}
/*时间片轮转:把结点插入到就绪队列的末尾*/
void InsertSlot(PCB *q)
{
PCB *pre,*p
pre=NULL
p=ready
while(p!=NULL)
{
pre=p
p=p->next
}
pre->next=q
q->next=NULL/*由于插入到队列的末尾,所以必须要使其的下一个结点为空值*/
}
/*打印一个信息*/
void PrintOneProcess(int select,PCB *q)
{
if(select==1)
printf("\t\t%-10s%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->state)
else if(select==2)
printf("\t\t%-10s%-10d%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->prio,q->state)
else if(select==3)
printf("\t\t%-10s%-10d%-10d%-10d%-10d%c\n",q->name,q->cputime,q->needtime,q->count,q->round,q->state)
}
/*将所有的进程打印出来,按运行,就绪,完成顺序打印*/
void PrintAllProcess(int select)
{
PCB *p
if(run!=NULL)
PrintOneProcess(select,run)
p=ready
while(p!=NULL)
{
PrintOneProcess(select,p)
p=p->next
}
p=finish
while(p!=NULL)
{
PrintOneProcess(select,p)
p=p->next
}
}
/*把就绪队列的第一个进程调入运行*/
void FirstIn()
{
run=ready
ready=ready->next
run->state='R'
}
/*先进先出调度策略*/
void FIFODo(int select)
{
while(run!=NULL)
{
run->cputime=run->cputime+1
run->needtime=run->needtime-1
if(run->needtime==0) //进程执行结束
{
printf("\n\t\tname cputimeneedtime state\n")
run->next=finish
finish=run
run->state='F'
run=NULL
if(ready!=NULL)
FirstIn()
PrintAllProcess(1)
}
}
}
/*优先级算法*/
void PriorityDo(int select)
{
while(run!=NULL)
{
printf("\n\t\tname cputimeneedtimepriostate\n")
run->cputime=run->cputime+1
run->needtime=run->needtime-1
run->prio=run->prio-3
if(run->needtime==0)
{
run->next=finish
finish=run
run->state='F'
run=NULL
if(ready!=NULL)
FirstIn()
}
else if((ready!=NULL) &&(run->prio <ready->prio))
{
run->state='W'
InsertPriority(run)
FirstIn()
}
PrintAllProcess(select)
}
}
/*时间片轮转算法*/
void SlotDo(int select)
{
while(run!=NULL)
{
printf("\n\t\tname cputimeneedtime count slotstate\n")
run->count=run->count+1
run->cputime=run->cputime+1
run->needtime=run->needtime-1
if(run->needtime==0) /*运行完成时*/
{
run->next=finish
finish=run
run->state='F'
run=NULL
if(ready!=NULL)
FirstIn()//就绪队列不为空,将就绪队列的第一个进程投入运行
}
else if(run->count==run->round) /*时间片已经到了,还未运行完成*/
{
run->count=0//时间片
if(ready!=NULL)
{
run->state='W'
InsertSlot(run)
FirstIn()
}
}
PrintAllProcess(select)//打印本次的所有记录
}
}
void Quit()
{
char ch
clrscr()
gotoxy(10,5)
printf("==========================================================\n\n")
printf(" Thank you for you using\n\n")
gotoxy(10,9)
printf("==========================================================\n\n")
gotoxy(13,15)
getchar()
printf("\n\t\tDo you really want to quit(y/Y or n/N):")
scanf("%c",&ch)
if(ch=='y' || ch=='Y')
{
printf("\n\t\t按任意键退出系统...")
getchar()
exit(0)
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)