#include
struct pcb
{
char name;
int time;
};
void main()
{
int n,i,j,flag=1;
struct pcb a[100];
printf("输入程序个数:");
scanf("%d",&n);
getchar();
for(i=0;i
{
printf("输入程序的名字:如A B C...\n");
scanf("%c",&a[i].name);
getchar();
printf("输入占用的时间片:");
scanf("%d",&a[i].time);
getchar();
}
i=0;
while(flag &&
n>0)
{
if(a[i].time!=0)
{
printf("%c",a[i].name);
a[i].time--;
}
for(j=0;j
if(a[j].time)
{
flag=1;
break;
}
else
flag=0;
i=(++i)%n;
}
}
#include
#include
#include
#include
#include
#include
int proc;
int piece;
//完成状态
#define FINISH 0
//运行状态
#define RUNNING 1
//就绪状态
#define READY 2
//io状态
#define IO 3
//等待IO
#define WAIT 4
//定义PCB结构
typedef struct tagPCBS{
// 编号
long pid;
// 进程名
char pname[10];
// 状态
int pstate;
// 总时间
int pneedtime;
// 开始时间
int piostarttime;
// 需要时间
int pioneedtime;
// 当前时间
int ptime;
// 优先级
int priority;
} pcbs, *ppcbs ;
class pcbnode;
//队列结点
class pcbnode
{
public:
pcbs *pcb;
pcbnode *link;
pcbnode();
~pcbnode();
int run();//运行操作
int runend();//运行结束
void runio();//IO操作
int ioend();//io结束
int insertnode(pcbnode *p,pcbnode *q);//在q后插入结点p
int deletenode(pcbnode *p,pcbnode *q);//删除p结点,q为p的前驱
int addnode(pcbnode *p);//增加结点
int pcbnode::isio();//是否开始IO
} ;
pcbnode::pcbnode(){
pcb = 0;
link = 0;}
pcbnode::~pcbnode()
{
if( link )
delete link;
if( pcb )
pcb->pstate = FINISH;
}
int pcbnode::run()
{
pcb->pstate = RUNNING ;
++ pcb->ptime ;
//优先级降低
pcb->priority -- ;
return 0;
}
int pcbnode::runend()
{
return ( pcb->pneedtime <=
pcb->ptime ) ;
}
int pcbnode::isio()
{
return ( (pcb->piostarttime ==
pcb->ptime) &&
(pcb->pioneedtime > 0) ) ;
}
int pcbnode::ioend()
{
return ( (pcb->pioneedtime) <= 0 )
;
}
void pcbnode::runio()
{
pcb->pstate = IO;
--(pcb->pioneedtime);
}
int pcbnode::addnode( pcbnode *p )
{
pcbnode *q;
q = this;
p->pcb->pstate = READY;
while( q->link )
q = q->link;
q->link = p;
return 0;
}
int pcbnode::insertnode( pcbnode *p,pcbnode *q )
{
p->link = q->link;
q->link = p;
return 0;
}
int pcbnode::deletenode( pcbnode *p, pcbnode *q )
{
q->link = p->link;
p->link = 0;
return 0;
}
int randInt( int seed )
{
int r;
r = rand();
while ( (r > seed) || ( r < 0 )
)
r = rand();
return r;
}
void newpcb( pcbs *pcb, int order )
//随机生成进程
{
char buf[10] ;
pcb->pid = order ;
strcpy( pcb->pname, "proc" ) ;
itoa( order, buf, 10 ) ;
strcat( pcb->pname, buf ) ;
pcb->pneedtime = randInt( 10 ) ;
pcb->piostarttime = randInt(
pcb->pneedtime ) ;
pcb->pioneedtime = randInt( 10 ) ;
pcb->ptime = 0 ;
pcb->priority = randInt( 10 ) ;
}
void pprint( pcbs *pcb, int count )
{
int i ;
// 打印进程状态
cout<
;
for( i = 0 ; i < count ; i ++ )
{
cout<
pcb[i].pname<
switch( pcb[i].pstate )
{
case IO :
cout<
break;
case RUNNING:
cout<
break;
case READY:
cout<
break;
case FINISH:
cout<
break;
case WAIT:
cout<
break;
}
cout<
;
cout<
;
cout<
;
cout<< endl ;
}
cout<
;
}
//时间片轮循法
void poll(pcbs * pcb, int pcbsnum)
{
pcbnode running, ready, blocked ;
pcbnode *p ;
pcbnode *q ;
cout<
cin>>piece;
int i ;
for( i = 0 ; i < pcbsnum ; i++ )
{
p = new pcbnode ;
p->pcb = pcb + i ;
ready.addnode( p ) ;
}
//开始调度
while( ready.link || running.link || blocked.link )
{
if( running.link )
{
//需要IO?
if( running.link->isio() )
{
blocked.addnode(running.link) ;
running.link->pcb->pstate = WAIT
;
}
//运行结束?
else if ( running.link->runend() )
{
p = running.link ;
running.link = 0 ;
delete p ;
}
//调度
else
{
ready.addnode(running.link) ;
running.link->pcb->pstate = READY
;
}
running.link = 0;
}
q = &ready;
p = ready.link;
if( ready.link )
{
ready.deletenode( p, q ) ;
running.addnode( p ) ;
p->pcb->pstate=RUNNING ;
}
p = blocked.link;
q = &blocked;
if( p )
{
int r;
r = p->ioend();
if( r )
{
blocked.deletenode( p, q ) ;
ready.addnode( p ) ;
p = q->link ;
}
}
int j = piece;
while( j-- )
{
p = running.link ;
q = &running ;
if( p )
{
//is IO start?
if( !(p->isio()) )
p->run();//RUN
}
p = blocked.link;
q = &blocked;
if (p)
p->runio();
//print proc state
pprint(pcb, pcbsnum);
}
}
}
//可抢占的优先调度
void priority( pcbs * pcb, int pcbsnum )
{
pcbnode running, ready, blocked ;
pcbnode *p, *f, *front ;
pcbnode *q ;
int i ;
for ( i = 0 ; i < pcbsnum ; i ++ )
{
p = new pcbnode ;
p->pcb = pcb + i ;
ready.addnode( p ) ;
}
while( ready.link || running.link || blocked.link )
{
//判断将运行队列中的进程是否要io或结束
if( running.link )
{
//需要IO?
if( running.link->isio() )
{
blocked.addnode( running.link ) ;
running.link->pcb->pstate = WAIT
;
running.link = 0 ;
}
//运行结束?
else if( running.link->runend() )
{
p = running.link;
running.link = 0;
delete p;
}
}
//寻找最大的一个优先级
p = ready.link;
q = p ;
f = &ready ;
front = f ;
if( p )
{
int maxpri = p->pcb->priority ;
while( p )
{
if( p->pcb->priority >
maxpri )
{
maxpri = p->pcb->priority ;
front = f;
q = p;
}
f = p;
p = p->link;
}
}
//如果最大优先级大于正在运行的优先级则强占cpu
p = running.link;
if( q )
{
if (p)
{
if( p->pcb->priority <
q->pcb->priority )
{
ready.addnode(p);
running.deletenode( p, &running ) ;
p->pcb->pstate = READY ;
running.addnode( q ) ;
ready.deletenode( q, front ) ;
q->pcb->pstate = RUNNING ;
}
}
else
{
running.addnode( q ) ;
ready.deletenode( q, front ) ;
q->pcb->pstate = RUNNING ;
}
}
p = blocked.link;
q = &blocked;
if( p )
{
int r;
r = p->ioend();
if( r )
{
blocked.deletenode( p, q ) ;
ready.addnode( p ) ;
p=q->link;
}
}
// 运行进程
p = running.link ;
q = &running ;
if( p )
{
//is IO start?
if( !(p->isio()) )
p->run() ; //RUN
}
// 进行IO
p = blocked.link;
q = &blocked;
if( p )
p->runio() ;
//动态计算就绪队列优先级
p = ready.link;
while( p )
{
(p->pcb->priority) ++ ;
p = p->link;
}
//print proc state
pprint(pcb, pcbsnum);
}
}
void generalpriority(pcbs * pcb, int pcbsnum)
{
pcbnode running,ready,blocked;
pcbnode *p,*f,*front;
pcbnode *q;
int i ;
// 将进程表中的进程加到就绪队列中
for( i=0 ; i < pcbsnum ; i ++ )
{
p = new pcbnode ;
p->pcb = pcb+i;
ready.addnode( p ) ;
}
while( ready.link || running.link || blocked.link )
{
//判断将运行队列中的进程是否要io或结束
if( running.link )
{//需要IO?
if( running.link->isio() )
{
blocked.addnode( running.link ) ;
running.link->pcb->pstate = WAIT
;
running.link = 0 ;
}
//运行结束?
else if( running.link->runend() )
{
p = running.link;
running.link = 0;
delete p;
}
}
if( !running.link )
{
//当运行队列为空
//寻找最大的一个优先级
p = ready.link;
q = p;
f = &ready;
front = f;
if( p )
{
int maxpri = p->pcb->priority;
while( p )
{
if( p->pcb->priority >
maxpri )
{
maxpri = p->pcb->priority;
front = f;
q = p;
}
f = p ;
p = p->link;
}
}
p = running.link;
if( q )
{
running.addnode(q) ;
ready.deletenode(q, front);
q->pcb->pstate = RUNNING ;
}
}
//将处理完IO的进程送到就绪队列
p = blocked.link;
q = &blocked;
if( p )
{
int r ;
r = p->ioend();
if( r )
{
blocked.deletenode(p, q) ;
ready.addnode(p);
p=q->link ;
}
}
//运行进程
p = running.link;
q = &running;
if( p )
{
//is IO start?
if( !(p->isio()) )
p->run() ; //RUN
}
// 进行IO
p = blocked.link;
q = &blocked;
if( p )
p->runio();
//print proc state
pprint( pcb, pcbsnum ) ;
}
}
void main()
{
pcbs *pcblist;//进程表
cout<
cin>>proc;
int i ;
remove("result.txt");
pcblist = new pcbs[proc]; // 为进程表分配空间
for( i = 0 ; i < proc ; i ++ )
newpcb(pcblist+i, i);//产生进程
pprint(pcblist, proc);
cout<
poll(pcblist, proc);//轮循法调度
cout<
cout<
generalpriority(pcblist,proc);//非抢占
cout<
cout<
priority(pcblist, proc);//可抢占优先法
delete pcblist;//释放进程空间
}
进程调度源程序如下:
jingchendiaodu.cpp
#include "stdio.h"
#include
#include
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb {
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
sort()
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->super)>(ready->super)))
{
p->link=ready;
ready=p;
}
else
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)>(second->super))
{
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else
{
first=first->link;
second=second->link;
}
}
if(insert==0) first->link=p;
}
}
input()
{
int i,num;
clrscr();
printf("\n 请输入进程号?");
scanf("%d",&num);
for(i=0;i
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程优先数:");
scanf("%d",&p->super);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='w';
p->link=NULL;
sort();
}
}
int space()
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
disp(PCB * pr)
{
printf("\n qname \t state \t super \t ndtime \t runtime
\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
check()
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:%s",p->name);
disp(p);
pr=ready;
printf("\n ****当前就绪队列状态为:\n");
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
destroy()
{
printf("\n 进程 [%s] 已完成.\n",p->name);
free(p);
}
running()
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();
else
{
(p->super)--;
p->state='w';
sort();
}
}
main()
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf("\n The execute number:%d \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n 按任一键继续......");
ch=getchar();
}
printf("\n\n 进程已经完成.\n");
ch=getchar();
}