• 大小: 13KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-05-28
  • 语言: C/C++
  • 标签: c  yuya  

资源简介

c语言版本,使用数据结构简单实现抢占式动态优先级调度算法

资源截图

代码片段和文件信息

#pragma warning(disable:4996)

#include 
#include 
#include 
#include 
#include 

using namespace std;

#define JOBSUM    7    //进程/作业总数
#define JOBNUM    4    //允许的作业道数
#define PRITOP    6    //最高优先级级数
#define TIMELIMIT 5    //时间限制

struct Job {    //作业
char jname[40];    //作业名
int start;         //到达时间
    int pri;           //优先级
    int worktime;      //工作时间
    Job *next;         //链接指针
};

struct PCB {
    PCB* next;
    char pname[40];    //进程名
int pcbpri;        //优先级
    int time;          //进程运行时间 
    char status;       //运行状态
};

bool CreateJob(Job* jobtable char name[])    //创建作业,将作业放入作业调度表
{
//随机生成一个作业
    Job *p = new Job;
    strcpy(p->jname name);
    p->start = rand() % (TIMELIMIT - 1) + 1;
    p->worktime = rand() % (TIMELIMIT - p->start) + 1;
    p->pri = rand() % PRITOP;
    p->next = NULL;

    //将作业放入作业调度表
    Job* now = jobtable;

//将作业放入作业调度表,按到达时间排序
    if (now->next == NULL) {    //后备队列还是空的时候
now->next = p;
}
else {
if (p->start <= now->next->start) {    //到达时间:当前作业<后备队列第一个作业——p放入队首
p->next = now->next;
            now->next = p;
}
 else { 
Job *q = now->next;
while ((p->start > q->start) && (q->next != NULL)) {  //标识p插入的位置
q = q->next;
}
if ((p->start > q->start) && q->next == NULL) {       //到达时间:当前作业>后备队列所有作业——p放入队尾
}
else if (p->start <= q->start) {    //插入p的位置:到达时间小于下一个节点
Job *t = now->next;
                while (t->next != q) {
                    t = t->next;
}
                t->next = p;
                p->next = q;
}
}
}
     return true;
 }

bool AddHoubei(Job *jobtable Job *p Job *&jhead)    //将作业p放入后备队列jhead,按优先级大小放置
{
     //将作业p从作业调度表jobtable中去除
Job* q = jobtable;
    while (q->next != p && q->next != NULL) {
        q = q->next;
}
if (q->next == p) {
q->next = p->next;
        p->next = NULL;
}
    //将作业p放入后备队列jhead,按优先级大小放置
    if (jhead == NULL) {    //后备队列为空
        jhead = p;
}
    else {
        if (p->pri >= jhead->pri) {    //新生成的作业的优先级比后备队列第一个作业优先级大——放在队首
            p->next = jhead;
            jhead = p;
}
        else {
            Job *q = jhead;
while ((p->pri < q->pri) && (q->next != NULL)) {  //标识插入p的位置
q = q->next;
}
            if ((p->pri > q->pri) && q->next == NULL) {      //新生成的作业的优先级比后备队列中所有作业的优先级都大——放在队尾
                q->next = p;
}
            else if (p->pri >= q->pri) {    //放置作业p的位置:这个位置作业p的pri大于等于下一个节点的pri
Job *t = jhead;
while (t->next != q) {
t = t->next;
}
t->next = p;
                p->next = q;
}
}
}
    return true;
}

bool CreateProcess(PCB* &head PCB* &tail Job* &jhead)    //创建新进程
{
    PCB* p = new PCB;
    char JobID = jhead->jname[3];
    strcpy(p->pname “Process“);     //进程名设置为Process+number格式      
    p->pname[7] = JobID;
    p->pname[8] = ‘\0‘;
p->pcbpri = jhead->pri;          //优先

评论

共有 条评论