操作系统实验一 处理器调度

 

 

一、实验目的与要求

本实验目的是模拟在单处理器情况下处理器调度,加深了解处理器调度的工作。

要求是从优先级调度和时间片轮转法调度算法中选取一个进行实验。

  • 程序中使用的数据结构及符号说明

使用的数据结构:链式循环队列

 

 

PCB类:

 

三、流程图

 

四、实验测试结果及结果分析

输入五个进程,进程名,优先级,需要运行的时间分别是P1 1 1;P2 2 2;P3 3 3;P4 4 4;P5 5 5

 

输入后进行打印输出

 

开始运行:

 

进程5运行两次后,优先级变为3,比P4小,所以P4进程代替P5开始运行,两次后优先级比P3小。。。一直这样运行,直到各个进程都运行了所需的时间。

  • 实验小结

做完本次实验,我对于动态优先级调度算法有了更加直观清楚的了解,我的编程能力也得到了不小的提升。我认为,本题最关键的就是优先级随着运行变化后,对于进程的排序调度,我利用数组的sort函数,对进程块队列进行了排序,解决了这个最关键的问题,剩下的就是简单的编程了。

附件:含比较详细注释说明的源程序清单

处理机调度.cpp

#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#include "PCB.h"
#include "LinkQueue.h"
#define N 5//进程数量
using namespace std;
static PCB *head;
int cnt = N;
PCB *runningprocess;
PCB P[N];
LinkQueue<PCB> *PcbQueue = new LinkQueue<PCB>();
bool cmprank(PCB a, PCB b);
void print(const PCB &pcb);
void sort(LinkQueue<PCB> *PcbQueue) //队列排序
{
	for (int i = 0; i < cnt; i++)
	{
		PcbQueue->DelQueue(P[i]);
	}
	sort(P, P + cnt, cmprank);
	for (int i = 0; i < cnt; i++)
	{
		PcbQueue->EnQueue(P[i]);
	}
}

bool cmprank(PCB a, PCB b)
{ //优先级从大到小比较函数
	return a.rank > b.rank;
}
void print(const PCB &pcb)
{
	cout << "\n进程名: " << pcb.name << endl;
	cout << "时间: " << pcb.reqtime << endl;
	cout << "要求运行时间: " << pcb.inittime << endl;
	cout << "优先数: " << pcb.rank << endl;
	cout << "状态: " << pcb.state << endl;
}
bool cmpreqtime(PCB a, PCB b)
{
	return a.reqtime > b.reqtime;
}

int run()
{
	runningprocess = &(PcbQueue->GetFront()->next->data);
	cout << "\n##########进程" << runningprocess->name << "正在运行##########\n";
	runningprocess->reqtime--; //剩余运行时间-1
	runningprocess->rank--;	   //优先级-1
	if (runningprocess->reqtime == 0)
	{ //某个进程运行结束,打印提示信息
		PCB temp;
		cout << "-----------------进程" << runningprocess->name << "运行结束------------------";
		runningprocess->state = 'E'; //赋予结束状态E
		print(*runningprocess);
		PcbQueue->DelQueue(temp);
		cnt--;
		cout << cnt << endl;
	}
	if (cnt == 0)
	{
		cout << "\n******所有进程都已结束******\n";
		return 0;
	}
	sort(PcbQueue); //按照进程优先级排序
	run();			//递归调用进程运行函数
}
void input()
{
	for (int i = 0; i < N; i++) //输入各进程状态
	{
		cout << "\n请输入第 " << i + 1 << "个进程名 :\n";
		cin >> P[i].name;
		cout << "\n请输入当前队列的优先级 :\n";
		cin >> P[i].rank;
		cout << "\n请输入队列需要运行的时间 :\n";
		cin >> P[i].inittime;
		P[i].reqtime = P[i].inittime;
		P[i].state = 'R'; //初始赋予就绪状态Running
	}
	for (int i = 0; i < cnt; i++)
	{
		print(P[i]);
		PcbQueue->EnQueue(P[i]);
	}
}
int main()
{
	input(); //输入各个进程的信息
	sort(PcbQueue);
	run(); //进程运行
	system("pause");
	return 0;
}

LinkQueue.h

#pragma once
#include "Node.h"
#include "status.h"
template <class ElemType>
class LinkQueue
{
protected:
    Node<ElemType> *front, *rear; // 带有队头队尾指针的循环队列
public:
    LinkQueue();
    virtual ~LinkQueue();
    int GetLength() const;
    bool IsEmpty() const;
    void Clear();
    Status DelQueue(ElemType &e );
    Status GetHead(ElemType &e) const; //获取第一个元素
    Status EnQueue(const ElemType e);
    void Traverse(void (*Visit)(const ElemType &)) const;
    Node<ElemType> * GetFront();//获取队列头指针
};
template <class ElemType>
Node<ElemType> * LinkQueue<ElemType>::GetFront(){
    return front;
}
template <class ElemType>
void LinkQueue<ElemType>::Traverse(void(Visit)(const ElemType &)) const
{
    Node<ElemType> *p;
    for (p = front->next; p != NULL; p = p->next)
        (Visit)(p->data);
}

template <class ElemType>
Status LinkQueue<ElemType>::DelQueue(ElemType &e)
{
    if (!IsEmpty())
    {
        Node<ElemType> *p = front->next;
        e = p->data;
        front->next = p->next;
        if (rear == p)
            rear = front; //队列中只有一个元素
        delete p;
        return SUCCESS;
    }
    else
        return OVER_FLOW;
}

template <class ElemType>
Status LinkQueue<ElemType>::EnQueue(const ElemType e)
{
    Node<ElemType> *p;
    p = new Node<ElemType>(e);
    if (p)
    {
        rear->next = p;
        rear = rear->next;
        return SUCCESS;
    }
    else
        return OVER_FLOW;
}
template <class ElemType>
Status LinkQueue<ElemType>::GetHead(ElemType &e) const
{
    if (!IsEmpty())
    {
        e = front->next->data;
        return SUCCESS;
    }
    else
        return OVER_FLOW;
}
template <class ElemType>
void LinkQueue<ElemType>::Clear()
{
    Node<ElemType> *p = front->next;
    while (p != NULL)
    {
        front->next = p->next;
        delete p;
        p = front->next;
    }
    rear = front;
}
template <class ElemType>
LinkQueue<ElemType>::~LinkQueue()
{
    Clear();
    delete front;
}
//判断链式队列是否为空
template <class ElemType>
bool LinkQueue<ElemType>::IsEmpty() const
{
    return rear == front;
}
template <class ElemType>
LinkQueue<ElemType>::LinkQueue()
{
    rear = front = new Node<ElemType>();
    //rear = front = NULL;
}

Node.h

#pragma once
template<class ElemType>
struct Node{
        ElemType data;
        Node<ElemType>* next;
        Node();
        Node(ElemType e,Node<ElemType>* link = NULL);
};
template<class ElemType>
Node<ElemType>::Node()
{
    next = NULL;
    //data = 0;
}
template<class ElemType>
Node<ElemType>::Node(ElemType e,Node<ElemType> *link)
{
    data = e;
    next = link;
}

PCB.h

#pragma once
#include <string>
using namespace std;
class PCB
{ //进程块
public:
	string name;  //进程名
	int reqtime;  //时间
	int inittime; //要求运行时间
	int rank;	//优先级
	char state; //状态,R running E end
	PCB *next; //指向下一个进程
};

status.h

#pragma once
typedef enum{NOT_PRESENT,ENTRY_FOUND,RANGE_ERROR,SUCCESS,OVER_FLOW} Status ;

  • 21
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值