⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit5.cpp

📁 广东工业大学 洪凌郁 操作系统的课程设计:包括进程管理
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>
#include "Unit5.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "PERFGRAP"
#include <Math.hpp>
#define getjcb(type) (type*)malloc(sizeof(type))
#define NULL 0
TForm5 *Form5;
void input2();
creat2();
insert2();
running2();
void test2();
disp2();
dispblock2();
disprun2();
sortinsert2();
void runmain2();
struct jcb{

           int id;              //作业序号
           int reach_time;       // 作业到达时间
           int need_time;           //所需的时间片个数
           int run_time;           //已经运行的时间片个数
           int finish_time ;     //完成时间
           float ci;               //作业周转时间
           double ti;              //作业带权周转时间
           char state;          //作业状态
            struct jcb *next;
          } *run=NULL,*hready=NULL,*hblock=NULL,*p;//hready,hblock分别为指向后备队列和缓冲队列
          typedef struct jcb JCB;
int i,n,time1Inteval,count,num=0,flag;
float sum_ci,sum_ti;                 //n表示作业总数
//------------------------------------------------------------------------------

JCB *insert2(JCB *head,JCB*jcb)  //将作业插入到链尾,带两个指针形参
{JCB *pi,*p1;
 p1=head;
 pi=jcb;
 if(head==NULL)
 {head=pi;pi->next=NULL;}
 else
    {while(p1->next!=NULL)
      {p1=p1->next;
       }
     p1->next=pi;
     pi->next=NULL;}
 return(head);
}
//------------------------------------------------------------------------------
JCB *increat2(void)   // 建立一个PCB结构体型的空链表
{
 JCB *head;
  head=NULL;
  return(head);
}
//------------------------------------------------------------------------------
void input2()
{
AnsiString  str,str1;
n=StrToInt(Form5->Edit2->Text);//读取要进行作业运行的个数
time1Inteval=StrToInt(Form5->Edit6->Text);//读取时间片长度
Form5->Timer1->Interval=time1Inteval;
for(i=1;i<=n;i++)  //初始化作业信息,直到到达要初始化的作业总个数
{
p=getjcb(JCB);
p->id=i;
p->reach_time=i-1;
p->need_time=(random(9)+1);//所需服务时间
p->run_time=0;
p->next=NULL;
if(i==1)
{str1+="    ";
str1+=IntToStr(p->id);str1+="\t\t";
str1+=IntToStr(p->reach_time);str1+="\t\t";
str1+=IntToStr(p->need_time);str1+="\t\t";
str1+=IntToStr(p->run_time);str1+="\r\n";
Form5->Memo2->Lines->Add(str1);
  run=insert2(run,p);
  } //if
else
{str+="    ";
p->state='W';
str+=IntToStr(p->id);str+="\t\t";
str+=IntToStr(p->reach_time);str+="\t\t";
str+=IntToStr(p->need_time);str+="\t\t";
str+=IntToStr(p->run_time);str+="\r\n";
  hblock=insert2(hblock,p);

  } //else
  } //for
  Form5->Memo3->Lines->Add(str);
}//input

//------------------------------------------------------------------------------
JCB *running2(JCB *head)
{
JCB *p1, *p2,*p3;
p1=head;
p2=hready;
AnsiString str;
Form5->ProgressBar1->Max=p1->need_time;
  (p1->run_time)++;
Form5->ProgressBar1->Position=p1->run_time;
  count++;     // 计算已经运行的时间片个数
  if(p1->need_time==p1->run_time)//如果运行的时间片达到所需个数
  {


    p1->finish_time=count;
    p1->ci=(p1->finish_time)-(p1->reach_time);
    p1->ti=(p1->ci)/(p1->need_time);
    p1->ti=((p1->ti)+0.005)*100/100.0;
    sum_ci+=(p1->ci);
    sum_ti+=(p1->ti);
    str+="     ";
    str+=IntToStr(p1->id);str+="\t"; str+="       ";
    str+=IntToStr(p1->reach_time);str+="\t"; str+="               ";
    str+=IntToStr(p1->need_time);str+="    ";str+="    \t    ";
    str+=IntToStr(p1->finish_time);str+="             ";str+="  \t";  str+="   ";
    str+=FloatToStr(p1->ci);str+=""; str+="\t\t";
    p1->ti=RoundTo((p1->ti), -3);
    str+=FloatToStr(p1->ti);
    Form5->Memo1->Lines->Add(str);

    free(p1);
    run=NULL;
    if(p2==NULL)
         return(p2);
  //if(!p2->next)
    hready=p2->next;
    run=insert2(run,p2);
    num-=1;


  }//if
  return(run);
}//running

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
JCB *sortinsert2(JCB *head,JCB*jcb)
 {JCB *fir,*sec,*pi,*p1;
  int ins=0;
  p1=head;
  pi=jcb;

  if((p1==NULL)||((pi->need_time)<(p1->need_time)))
    { head=pi;
       pi->next=NULL;
	 }
   else{fir=p1;
        sec=fir->next;
        pi->next=NULL;
		while(sec!=NULL)
		{
		 if((pi->need_time)<(sec->need_time))
		              {            pi->next=sec;
					   fir->next=pi;
					   sec=NULL;
					   ins=1;
					   }
			else{fir=fir->next;
			     sec=sec->next;
				 }
			}//while
		if(ins==0)
                {
                fir->next=pi;}
		}

	return(head);
}
//------------------------------------------------------------------------------
 void test2() //检测当前后备队列是否已满,不满,将缓冲区的一个作业插入到后备队列
{JCB *p1,*p2;
 p1=hblock;
 p2=hblock;
  if((hblock!=NULL)&&(p1!=NULL))
  {
      if((num<5)&&(hblock!=NULL))
       {if(p1==hblock)
          {
           hblock=p1->next;
      if(flag==0)
     hready=insert2(hready,p1);   //将缓冲区一个作业插入到后备队列
      if(flag==1)
     hready=sortinsert2(hready,p1);
               num+=1;
          // p1=hblock;
          } //if(p1==hblock)
          else
          {p2->next=p1->next;
       if(flag==0) hready=insert2(hready,p1);
       if(flag==1) hready=sortinsert2(hready,p1);
       p1=p2->next;
          }//else
         } //大if
  /*     else
       {p2=p1;
        p1=p1->next;
       } //else      */
} //whlie
}//block
//------------------------------------------------------------------------------
//从链表起始地址开始输出该链表的内容
void disp2(JCB *head)
{JCB *pr;
 pr=head;
 AnsiString str1;
  if(head!=NULL)      //链表非空
  {
     do
      {
        str1+="    ";
        str1+=IntToStr(pr->id);str1+="\t\t";
        str1+=IntToStr(pr->reach_time);str1+="\t\t";
        str1+=IntToStr(pr->need_time);str1+="\t\t";
        str1+=IntToStr(pr->run_time);str1+="\r\n";
        pr=pr->next;
       }while(pr!=NULL); //不断输出作业的信息,直到链尾!
  } //if
  else
  {  str1+="     **************************后  备  队  列  中  没  有  作  业!*****************************\r\n" ;}
   Form5->Memo2->Lines->Add(str1);
    }//disp
//------------------------------------------------------------------------------
void dispblock2(JCB *head)
{JCB *pr;
 pr=head;
 AnsiString str1;
  if(head!=NULL)      //链表非空
  {
     do
      {
        str1+="    ";
        str1+=IntToStr(pr->id);str1+="\t\t";
        str1+=IntToStr(pr->reach_time);str1+="\t\t";
        str1+=IntToStr(pr->need_time);str1+="\t\t";
        str1+=IntToStr(pr->run_time);str1+="\r\n";
        pr=pr->next;
       }while(pr!=NULL); //不断输出作业的信息,直到链尾!
  } //if
  else
  {  str1+="     **************************缓  冲  区  中  已  没  有  作  业!*****************************\r\n" ;}
   Form5->Memo3->Lines->Add(str1);
    }//disp
//------------------------------------------------------------------------------
void disprun2(JCB *head)
{JCB *pr;
 pr=head;
 AnsiString str1;
  if(head!=NULL)      //链表非空
  {
     do
      {
        str1+="    ";
        str1+=IntToStr(pr->id);str1+="\t\t";
        str1+=IntToStr(pr->reach_time);str1+="\t\t";
        str1+=IntToStr(pr->need_time);str1+="\t\t";
        str1+=IntToStr(pr->run_time);//str1+="\r\n";
        pr=pr->next;
       }while(pr!=NULL); //不断输出作业的信息,直到链尾!
  } //if
  else
  {  str1+="     **************************运  行  队  列  中  没  有  作  业!*****************************" ;}
   Form5->Memo4->Lines->Add(str1);
    }//disp
//------------------------------------------------------------------------------
void runmain2()
{//AnsiString str;
run=running2(run);
 test2();
disprun2(run);
disp2(hready);
dispblock2(hblock);

}//runmain
//---------------------------------------------------------------------------
__fastcall TForm5::TForm5(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm5::RadioButton1Click(TObject *Sender)
{
 if(RadioButton1->Checked)
flag=0;
else if(RadioButton2->Checked)
flag=1;       
}
//---------------------------------------------------------------------------
void __fastcall TForm5::RadioButton2Click(TObject *Sender)
{
 if(RadioButton1->Checked)
flag=0;
else if(RadioButton2->Checked)
flag=1;       
}
//---------------------------------------------------------------------------
void __fastcall TForm5::Button3Click(TObject *Sender)
{
 input2();
Button1->Enabled=true;
Button3->Enabled=false;
Form5->Edit2->Enabled=false;
Form5->Edit6->Enabled=false;       
}
//---------------------------------------------------------------------------
void __fastcall TForm5::Button1Click(TObject *Sender)
{

   Form5->Timer1->Enabled=true;
   Button1->Enabled=false;
   Button3->Enabled=false;       
}
//---------------------------------------------------------------------------
void __fastcall TForm5::Button2Click(TObject *Sender)
{
 Form5->Memo1->Clear();
    Form5->Memo2->Clear();
    Form5->Memo3->Clear();
    Form5->Memo4->Clear();
   Button1->Enabled=false;
    Button3->Enabled=true;
    Form5->ProgressBar1->Position=0;
    count=0;num=0;sum_ci=0;sum_ti=0;
  Form5->Edit2->Enabled=true;
  Form5->Edit6->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm5::Timer1Timer(TObject *Sender)
{
  AnsiString  str;
  double avr_ci,avr_ti;
  //if(run2!=NULL)
  runmain2();
  if((run==NULL)&&(hready==NULL)&&(hblock==NULL))
  {
  Form5->Timer1->Enabled=false;
  avr_ci=sum_ci/n;
  avr_ti=sum_ti/n;
  str+="<<<<<<<<<<<<<<<<<<<<该 组 作 业 已 经 运 行 完 成 !>>>>>>>>>>>>>>>>>>>>>>>\r\n";
  str+="    这 组 作 业 平 均 周 转 时 间 为:  ";
  avr_ci=RoundTo(avr_ci, -3);str+=FloatToStr(avr_ci);
  avr_ti=RoundTo(avr_ti, -3);str+="\r\n    这 组 作 业 带 权 平 均 周 转 时 间为 :  ";
  str+=FloatToStr(avr_ti);
  Form5->Memo1->Lines->Add(str);
  }//if


  //else
}
//---------------------------------------------------------------------------
void __fastcall TForm5::FormCreate(TObject *Sender)
{
 if(RadioButton1->Checked)
flag=0;
else if(RadioButton2->Checked)
flag=1;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -