Chinaunix首页 | 论坛 | 博客
  • 博客访问: 495337
  • 博文数量: 60
  • 博客积分: 2673
  • 博客等级: 少校
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-09 00:25
个人简介

目前主要从事C++软件开发

文章分类

全部博文(60)

文章存档

2013年(3)

2012年(3)

2010年(6)

2009年(23)

2008年(25)

我的朋友

分类:

2008-10-25 07:54:49

#include "stdio.h"
#include "stdlib.h"
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))

struct pcb { /* 定义进程控制块PCB */
      char name[10];
      char state;
      int super;
      int ntime;
      int rtime;
      struct pcb* next;
}*ready=NULL,*p;

typedef struct pcb PCB;

void sort(PCB *a) /* 建立对进程进行优先级排列函数*/
{
   PCB *first, *second;
   int insert=0;
if((ready==NULL)||((a->super)>(ready->super))) /*优先级最大者,插入队首*/
{
     a->next=ready;
     ready=a;
}
else /* 进程比较优先级,插入适当的位置中*/
{
    first=ready;
    second=first->next;
   while(second!=NULL)
   {
      if((a->super)>(second->super))
   { /*若插入进程比当前进程优先数大,插入到当前进程前面*/
         a->next=second;
         first->next=a;
         second=NULL;
         insert=1;
   }
      else /* 插入进程优先数最低,则插入到队尾*/
   {
         first=first->next;
         second=second->next;
   }
   }
if(insert==0) first->next=a;
}
}

void createpcb() /* 建立进程控制块函数*/
{
int i,num;
printf("*----------------------------------------------------------*\n");
printf("|*****************最高优先权优先调度算法模拟***************|\n");
printf("*----------------------------------------------------------*\n");
printf("\n 请输入进程数目:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
      p=getpch(PCB);
      printf("\n 输入第%d个进程的名字、优先数及该进程要求服务的时间:",i);
      scanf("%s%d%d",p->name,&p->super,&p->ntime);
      p->rtime=0; p->state='w';
      p->next=NULL;
      sort(p);
}
}

/*
int space()
{
   int l=0;
   PCB* pr=ready;
   while(pr!=NULL)
   {
       l++;
       pr=pr->next;
   }
return(l);
} */



void display1() /*建立进程显示函数,用于显示当前进程*/
{
printf("\n进程名 状态 优先数 要求服务的时间 已运行时间 \n");
}

void display2(PCB * pr)
{
printf("%3.5s %7c %6d %12d %10d",pr->name,pr->state,pr->super,pr->ntime,pr->rtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB *pr;
printf("\n------------------------------------------------------");
printf("\n **** 当前正在运行的进程是%s,它的状态如下:",p->name); /*显示当前运行进程*/
display1();
display2(p);
pr=ready;
printf("\n ****当前就绪队列中进程的状态如下:\n"); /*显示就绪队列状态*/
if(pr==NULL) printf(" ****就绪队列为空!");
else
{
display1();
     while(pr!=NULL)
{
        //printf("%s",pr->name);

        display2(pr);
        pr=pr->next;
}
}
}

void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
   printf("\n 进程 [%s] 已完成.\n",p->name);
   free(p);
}

void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime) destroy(); /* 调用destroy函数*/
else
{
     (p->super)--;
     p->state='w';
     sort(p); /*调用sort函数*/
}
}

void main()
{
//int len,h=0;

char ch;
createpcb();
printf("\n 初始时就绪队列的状态如下:");
display1();
p=ready;
while(p!=NULL)
{
   //printf("%s",pr->name);

   display2(p);
   p=p->next;
}
printf("\n 开始运行:..............");
//len=space();

while(/*(len!=0)&&*/ready!=NULL)
{
ch=getchar();
//h++;

//printf("\n The execute number:%d \n",h);

p=ready;
ready=p->next;
p->next=NULL;
p->state='R';
check();
running();
printf("\n 按任一键继续......");
ch=getchar();
}
printf("\n 进程已经完成.\n");
//ch=getchar();

}
 

阅读(1282) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~