• 大小: 7KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-06-08
  • 语言: C/C++
  • 标签: 操纵系统  CPU调度  

资源简介

这是模拟操纵系统中CPU调度问题,调度策略是最短剩余时间优先,声明只是模拟,并没有真正的进程调度。

资源截图

代码片段和文件信息

#include 
#include 
#include 

typedef struct
{
int remain_time; //进程剩余执行时间
int arrive_time; //进程到达时间
int Tp; //进入就绪队列的时间
int Tc; //进入执行队列的时间
int To; //进程执行结束的时间
int number; //进程编号
}Process_Block; //定义进程模块

typedef struct _Queue
{
Process_Block PB;
struct _Queue *next;
}_Block*Process; //定义一个进程模块队列中结点


typedef struct 
{
Process head; //队列头指针
Process end; //队列尾指针
}Process_Queue; //进程队列


Process_Queue PQ; //定义一个全局队列变量
int t; //全局时间
Process Run_Now; //当前正在运行的进程,作为全局变量


/*初始化队列*/
void InitQueue(Process_Queue PQ)
{
PQ.head ->next = NULL;
PQ.end ->next = PQ.head;
}
/*判定队列是否为空队列*/
int IsEmpty(Process_Queue PQ)
{
if(PQ.end->next == PQ.head)
return 1; //队列空的条件为头指针指向尾指针并且尾指针指向头指针
else
return 0;
}
/*插入队列操作*/
void EnQueue(Process_Queue PQProcess P)
{
Process temp =(Process)malloc(sizeof(_Block));
temp = PQ.end;
temp->next->next = P;
PQ.end->next = P;
}
/*出列操作*/
Process DeQueue(Process_Queue PQ)
{
if(IsEmpty(PQ))
return NULL;
Process temp = PQ.head->next;
PQ.head->next= temp ->next;
if(PQ.end->next == temp)
PQ.end->next = PQ.head;
return temp;

}

/*调度最短剩余时间的进程至队头*/
Process ShortestProcess(Process_Queue PQ)
{
if(IsEmpty(PQ)) //如果队列为空,返回
{
if(!Run_Now)
return NULL;
else
return Run_Now;
}
Process tempshortestprev;
//temp = (Process)malloc(sizeof(_Block));
//shortest = (Process)malloc(sizeof(_Block));
int min_time;
if(Run_Now) //如果当前有进程正在执行,
{
shortest = Run_Now; //那么最短进程初始化为当前正在执行的进程,
min_time = Run_Now->PB.remain_time;
}
else //如果当前没有进程执行,
{
shortest = PQ.head->next; //则最短进程初始化为队列中第一个进程
min_time = PQ.head->next->PB.remain_time;
}
temp = PQ.head;
prev = temp;

while(temp->next)
{
if(temp->next->PB.remain_time  {
shortest = temp->next; //则保存当前进程,
min_time = shortest->PB.remain_time;
prev=temp; //及其前驱
}
temp=temp->next;
}

if(shortest == PQ.end->next) //如果最短剩余时间进程是队列中最后一个进程,
PQ.end->next = prev; //则需要修改尾指针指向其前驱

prev->next = shortest->next; //修改指针将最短剩余时间进程插入到队头

return shortest;
//shortest->next=PQ.head->next;
//PQ.head->next=shortest;
}
/*运行函数*/
void Run()
{
Run_Now->PB.remain_time--; //某一时间运行它的剩余时间减1
return;
}

void Wait()
{
return ;
}

int sum(int array[]int n)
{
int isum=0;
for(i=0;i sum+=array[i];
return sum;
}
int main()
{
PQ.head = (Process)malloc(sizeof(_Block));
PQ.end = (Process)malloc(sizeof(_Block));
Run_Now = (Process)malloc(sizeof(_Block));
Run_Now =NULL;

InitQueue(PQ);

int iNTotal_Time=0; //Total_Time为所有进程的执行时间之和
printf(“请输入计算机中的进程数目:“);
scanf(“%d“&N);

Process *Ptemp;
P = (Process*)malloc(

评论

共有 条评论