资源简介

本实验通过编写C#语言的代码实现先来先服务(FCFS),最短作业优先(SJF)、响应比高者优先(HRN)的调度算法。最后在界面上显示出进程在调度过程中的情况。另附上C++的进程调度实验,算法不相同同。

资源截图

代码片段和文件信息

#include 
#include 
#include
#include
#include 
using namespace std;

#define P_NUM 5  //进程数
#define P_TIME 50//时间片长度
#define MIN -9999 //最小优先级

enum state{
    ready   //就绪
    execute //执行
    block   //阻塞
    finish   //完成
};

struct pcb{  //进程控制块结构
    char name[4]; //进程名
    int priority; //优先级
    int cputime;  //占用CPU时间
    int needtime; //还需要执行的时间
    int count;
    int round; 
    state process; //进程状态
    pcb * next;
};

pcb * get_process()//获取进程信息
{
    pcb *q;
    pcb *t;
    pcb *p;//进程链表头指针
    int i=0;
    cout<<“input “<    while (i        q=(struct pcb *)malloc(sizeof(pcb));
        cin>>q->name;
        cin>>q->needtime;
        q->cputime   = 0;
        q->priority  = P_TIME - q->needtime;//计算优先级
     q->round = 0;
     q->count = 0;
        q->process   = ready;
        q->next      = NULL;
        if (i==0)  //第一个pcb
     {
            p = q;
            t = q;
        }
        else
  {
            t->next = q;
            t = q;
        }
        i++;
    }  //while
    return p;
}

void  display(pcb *p){
    cout<<“NAME“<<“    “<<“CPUTIME“<<“    “<<“NEEDTIME“
  <<“    “<<“PRIORITY“<<“    “<<“STATE“<    while(p){
        cout<name;
        cout<<“        “;
        cout<cputime;
        cout<<“          “;
        cout<needtime;
        cout<<“          “;
        cout<priority;
        cout<<“          “;
        switch(p->process){
            case ready:cout<<“ready“<            case execute:cout<<“execute“<            case block:cout<<“block“<            case finish:cout<<“finish“<        }
        p=p->next;
    }
}

int process_finish(pcb *q){//判断进程是否均已执行完毕
    int bl=1;
    while(bl&&q)
{
        bl = bl&&(q->needtime==0);
        q = q->next;
    }
    return bl;
}

void cpuexe(pcb *q){
    pcb* t=q;
    int tp= MIN;
    while(q){  //寻找优先级最大的进程
        if (q->process!=finish){
            q->process=ready;
            if(q->needtime==0){
                q->process=finish;
            }
        }
        if(tppriority&&q->process!=finish){
            tp=q->priority;
            t=q;
        }
        q=q->next;
    }
    if(t->needtime!=0){  //执行进程t
        t->priority-=3;
        t->needtime--;
        t->process=execute;
        t->cputime++;
    }
}

void priority_cal()//优先级调度
{
    pcb * p;
    system(“cls“);//清屏clrscr();
    p = get_process(); //获取进程链表
system(“cls“);
    int cpu=0;
system(“cls“);  //clrscr();
while(!process_finish(p)){
cpu++;
        cout<<“cputime:“<        cpuexe(p);
        display(p);
Sleep(2000);
system(“cls“);
}
cout<<“All processes have finishedpress any key to exit“<    getch();
}

void display_menu()//显示菜单
{
    cout<<“CHOOSE THE ALGORITHM:“<

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      20480  2010-01-06 22:43  Scheduling\bin\Debug\Scheduling.exe

     文件      19968  2010-01-06 22:43  Scheduling\bin\Debug\Scheduling.pdb

     文件       5632  2005-11-11 22:25  Scheduling\bin\Debug\Scheduling.vshost.exe

     文件      20480  2010-01-06 22:43  Scheduling\obj\Debug\Scheduling.exe

     文件      19968  2010-01-06 22:43  Scheduling\obj\Debug\Scheduling.pdb

     文件        381  2010-01-06 22:49  Scheduling\obj\Scheduling.csproj.FileListAbsolute.txt

     文件      10662  2010-01-06 22:39  Scheduling\Program.cs

     文件       1191  2009-12-14 10:00  Scheduling\Properties\AssemblyInfo.cs

     文件       1954  2009-12-14 10:00  Scheduling\Scheduling.csproj

     文件        919  2009-12-14 10:00  Scheduling.sln

     目录          0  2009-12-14 10:00  Scheduling\obj\Debug\TempPE

     目录          0  2009-12-14 10:14  Scheduling\bin\Debug

     目录          0  2010-01-06 22:43  Scheduling\obj\Debug

     目录          0  2009-12-14 10:00  Scheduling\bin

     目录          0  2009-12-14 10:06  Scheduling\obj

     目录          0  2009-12-14 10:00  Scheduling\Properties

     目录          0  2010-01-06 22:39  Scheduling

     文件       5234  2009-12-14 11:46  schudule.cpp

----------- ---------  ---------- -----  ----

               106869                    18


评论

共有 条评论