2013-05-19《进程管理模拟的实现》

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define getpcb (type) (type*)malloc(sizeof(type))
#define getpcb(type) (type*)malloc(sizeof(type))

typedef struct pcb{
 int id;     //进程序号
 int ra;     //所需资源A的数量
 int rb;     //所需资源B的数量
 int rc;     //所需资源C的数量
 int ntime;  //所需的时间片个数
 int rtime;  //已经运行的时间片个数
 char state; //进程状态
 struct pcb *next;
}PCB;
PCB *ready=NULL,*block=NULL;     //hready,hblock分别为指向就绪和阻塞队列

int ra=10,rb=10,rc=10,h=6,pCur=3;
//pTotal为要模拟的进程个数,pCur为初始化进程个数,ra,rb,rc分别为A,B,C
//三类资源的总量 ,h为时间片运行的次数
//------------------------------------------------------------------------------

//建立一个PCB结构体的空链表
PCB *increat(void)
{
 PCB *head;
 head=NULL;
 return(head);
}
//------------------------------------------------------------------------------
//从链表起始地址开始输出该链表的内容
void disp(PCB *head)
{
 PCB *pcb;
 pcb=head;
 while (pcb != NULL)
 {
  printf("\t %d",pcb->id);
  printf("\t%c",pcb->state);
  printf("\t%d",pcb->ra);
  printf("\t%d",pcb->rb);
  printf("\t%d",pcb->rc);
  printf("\t%d",pcb->ntime);
  printf("\t\t%d\n",pcb->rtime);
  pcb = pcb->next;
 }
 //printf("all process display\n");
//disp
 
void dispRun(PCB *head)
{
 PCB *pcb;
 pcb=head;
 printf("\nrunning process information*************************************************\n");
 printf("\tPID\tSTATU\tRA\tRB\tRC\tneed Time\trun Time\n");
 printf("\t%d",pcb->id);
 printf("\t%c",pcb->state);
 printf("\t%d",pcb->ra);
 printf("\t%d",pcb->rb);
 printf("\t%d",pcb->rc);
 printf("\t%d",pcb->ntime);
 printf("\t\t%d\n",pcb->rtime);
 pcb = pcb->next;
//dispRun
//------------------------------------------------------------------------------

PCB *insert(PCB *head,PCB *pNew)  //带两个指针形参,head为已有的进程队列,pcb为新建的进程
{
 PCB *pHead;
 pHead = head; 
 
 return head;
}

void init()  //对进程进行初始化

 int i;
 PCB *pcb;
 for(i=1;i<=pCur;i++)             //初始化进程信息,直到到达要初始化个数
 {
  pcb=getpcb(PCB);
  pcb->id=i;
  pcb->ra=(3);
  pcb->rb=(4);
  pcb->rc=(5);
  pcb->ntime=(rand() % h+1);
  pcb->rtime=0;
  pcb->next=NULL;
  if (((ra-(pcb->ra))>=0)&&((rb-(pcb->rb))>=0)&&((rc-(pcb->rc))>=0)) //如果资源符合所需要求
                                                             //则写入就绪队列队尾
   ra=ra-(pcb->ra);     //当前所剩A类资源数目
   rb=rb-(pcb->rb);   //当前所剩B类资源数目
   rc=rc-(pcb->rc); //当前所剩C类资源数目
   pcb->state='W';
   ready=insert(ready,pcb);   //将进程插入就绪队列
  }//if
  else
  {
   pcb->state='B';
   block=insert(block,pcb);
  }
 }//for
}//input

//------------------------------------------------------------------------------
//当某进程的资源满足时,一次性的获得所有的资源
void resAssigned()
{
 PCB *pcb = block,*pre = block;
 while (pcb != NULL)
 {
  
 }
}


//------------------------------------------------------------------------------
void check()       //输出就绪队列和阻塞队列的信息
{
 printf("\nready queue information**************************************************\n"); 
 printf("\tPID\tSTATU\tRA\tRB\tRC\tneed Time\trun Time\n");
 disp(ready);
 printf("\nblocked queue information*************************************************\n");
 printf("\tPID\tSTATU\tRA\tRB\tRC\tneed Time\trun Time\n");
 disp(block);
}//check

//---------------------------------------------------------------------------
PCB *running(PCB *head)   //运行就绪队列的头进程
{
 PCB *pcb = head;
 if(pcb == NULL)
 {
  return NULL;
 
 pcb->state='R';        //进程状态由就绪转向运行
 (pcb->rtime)++;
 h++;
 dispRun(pcb);
 if(pcb->ntime == pcb->rtime)    //如果已经运行的时间片到达所需次数
    {
  printf("\r\n\r\n\t\tPID %d's process has finished\n",pcb->id);
  ra = ra+(pcb->ra);
  rb = rb+(pcb->rb);
  rc = rc+(pcb->rc);
  head = head->next;
  free(pcb);   //释放当前指针
  pcb = NULL;
  

 }
 else
 {
  pcb->state='W';
  head = head->next;
  head=insert(head,pcb);
 }
 return head;
}//running

int main()
{
 
 init();
 check();
 while (ready)
 {
  ready = running(ready);
  printf("Press enter key to continue...");
  getchar();
  resAssigned();
  check();  
 }
 return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值