西南交大操作系统实验-进程调度

西南交大操作系统实验-进程调度

对操作系统中进程调度的模拟算法。

实验要求实现操作系统进程调度中的SRT与FEEDBACK算法。

进程调度常见算法概要:

FCFS(先到先服务): 先来先服务,也可以称为先进先出
ROUND-ROBIN(轮询): 以一个周期性间隔产生时钟中断,此时当前正在运行的进程被置于就绪队列,基于FCFS选择下一个就绪进程运行。
SPN:最短进程优先,下一次选择所需处理时间最短的进程
SRT:最短剩余时间优先,总是选择预期剩余时间最短的进程
HRRN:最高响应比优先,R=(w+s)/s,其中R表示响应比,w表示已经等待的时间,s表示期待服务的时间
FEEDBACK(反馈):进程第一次进入系统是放置于RQ0,第一次被强占并返回就绪态时,放入RQ1,以后每次被强占就下降一级。如果进程处于最低等级,则不再降级,反复返回到该队列,直到结束。

西南交大操作系统实验-进程调度_第1张图片

SRT具体实现原理略;

FEEDBACK实现原理:全局定义进程结构体,包括到达时间,剩余服务时间,进程号,FEEDBACK q参数,优先级,完成状态。再建立一个以该结构体为元素的队列数组,数组角标越大代表优先级越低,进程每次被分配执行时间成等比数列。

int time = pow(pro.tq, pro.priority);//tq为公比,priority为优先级

若q=1则每次分配时间为1。进程用完本次分配时间若未完成,则进入下一个低优先级队列(上图q=2时A第二次执行只被分配了1个时间片,具体原因不明,我猜测是当A第一次执行完成后,除了它自己没有别的进程在等待,所以又被放进了原队列,如有错误请指正),有一点需要注意的是当有新进程到达后,当前正在运行的进程使用完被分配的时间片后会被最新的进程抢占,即永远最高优先级优先,算法实现可以检测当有新进程到达时,将队列数组角标重置为零。

FIFO,ROUNDROBIN代码参见课程资源示例程序。

FEEDBACKANDSRT源代码:

/*
Title:FEEDBACKANDSRT
Author:Uranuslight
School:SWJTU
Build Date:2017/05/15
Last revision:2017/05/16
Version:1.2
*/
#include "iostream"
#include "cstdlib"
#include "vector"
#include "map"
#include "deque"
#include "sstream"
#include "cstdio"
#include "cmath"
#include "cstring"
#include "ctime"
#include "queue"
#include "fstream"
#include "iomanip"
#define MAXN 20
#define ll long long
#define mst(ss,b) memset(ss,b,sizeof(ss))
using namespace std;
bool state[5][20];
int clocks = 0;
int totalExcuteTime = 0;
bool newflag = false;//new process coming change to true
typedef struct process
{
	int start;//start time
	int remain;//service time
	int pid;//id of process
	int tq;//min time slice
	int priority;//
	int done;
};
process p[5];
queue  que[MAXN];

bool isnewarrived()
{
	for (int i = 0; i<5; i++)
	{
		if (p[i].start == clocks)
		{
			que[0].push(p[i]);
			newflag = true;
			return true;
		}
	}
	return false;
}
bool isallqueueempty()
{
	bool flag = true;
	for (int i = 0; i0)
			return false;
		else
			return true;
	} while (0);
}
void init(int tq)
{
	p[0].start = 0; p[0].remain = 3;
	p[1].start = 2; p[1].remain = 6;
	p[2].start = 4; p[2].remain = 4;
	p[3].start = 6; p[3].remain = 5;
	p[4].start = 8; p[4].remain = 2;
	for (int i = 0; i<5; i++)
	{
		p[i].pid = i;
		p[i].priority = 0;
		p[i].tq = tq;
		p[i].done = 0;
	}
}
void printinfo()
{
	cout << "task id   start  excute" << endl;
	for (int i = 0; i<5; i++)
	{
		printf("task %2d: %6d %6d\n", i + 1, p[i].start, p[i].remain);
	}
}
void printans()
{
	for (int i = 0; i<5; i++)
	{
		for (int j = 0; j<20; j++)
		{
			if (state[i][j])
				cout << "#";
			else
				cout << " ";
		}
		cout << endl;
	}
	cout << endl;
}
void Feedback(int tq)
{
	init(tq);
	mst(state, false);
	clocks = 0;
	int i = 0;
	int executeflag = 0;
	{
		que[0].push(p[0]);
		while (i < 20)
		{
			while (!que[i].empty())
			{
				process ptemp = que[i].front();
				que[i].pop();
				if (!runprocess(ptemp, clocks))
				{
					executeflag = 1;
					if(!isallqueueempty())
					{
						ptemp.priority++;
						que[i + 1].push(ptemp);
					}
					else   que[i].push(ptemp);
				}
				if (newflag)
				{
					break;
				}
			}
			i++;
			if (!isallqueueempty() && executeflag == 1 || newflag)
			{
				i = 0; executeflag = 0; newflag = false;
			}
			if (clocks == 20)
				break;
		}
	}
	cout << "Slice q: " << p[0].tq << endl;
	printans();
}


void SRT()
{
	init(1);
	mst(state, false);
	for (int j = 0; j<20; j++)
	{
		int minre = 9999, pos=0;
		for (int i = 0; i<5; i++)
		{
			if (minre>p[i].remain && j >= p[i].start && p[i].remain>0)
			{
				minre = p[i].remain;
				pos = i;
			}
		}
		state[pos][j] = true;
		p[pos].remain = p[pos].remain - 1;
	}
	printans();
}
int main()
{
	char choice;
	while (1)
	{
		//system("cls");
		printf("please choice the schedule measure:\n");
		printf("f : Feedback\n");
		printf("s : SRT\n");
		printf("q : Exit\n");
		printf("choice = ");
		choice = getchar();
		if (choice == '\n')
			choice = getchar();
		switch (choice)
		{
			case 'f': init(1);printinfo();Feedback(1); Feedback(2); break;
			case 's': init(1);printinfo();SRT(); break;
			case 'q': return 0;
			default:
			{
				cout << "please input the true symbol(p or f or r)!" << endl;
				continue;
			}
		}
		cout << endl;
		//system("pause");
	}
	return 0;
}


你可能感兴趣的:(OS)