Demo entry 6338285

SJF

   

Submitted by anonymous on Dec 11, 2016 at 09:53
Language: C++. Code size: 3.3 kB.

//调度算法的模拟  
//1.SJF 短作业优先算法  

#include<stdio.h>  
#include <malloc.h>  
#include <string>  
#include <string.h>  
#include <iostream>  
using namespace std;

#define N 5  //假设5个进程  

struct PCB {
	string name;//进程name  
	int reachTime;//标志进程到达时间  
	int needTime;//进程所需的时间  
	bool state;
	/*进程的状态,false表示挂起,true表示激活*/
	int alltime;//总共所用的时间,即周转时间  
	bool over;   //进程的状态,true表示结束  
	bool isrunning;
}P[5];  //声明5个进程  
void init();
void processSchedule();
void printPCB(PCB P);

int arr[10];  //队列存放可以运行但是没有被调度的进程  
int Size = 0; //队列的长度  
int RunningP = -1; //当前运行进程编号  

void Sort(int a[], int Size);

int main() {
	init();  //初始化这五个进程  
	processSchedule();//进程调度  

	cout << "*-----------------------输出部分------------------------*" << endl;
	cout << "进程名称" << "\t" << "到达时间" << "\t" << "所需时间" << "\t" << "周转时间" << endl;
	for (int i = 0; i < 5; i++) {
		printPCB(P[i]); //打印进程信息  
	}

	return 0;
}

void init() {

	cout << "*------------------短作业优先算法-------------------*" << endl;
	for (int i = 0; i<N; i++) {
		cout << "输入进程" << i + 1 << "名称,到达时间,所需时间(用空格键隔开):" << endl;
		cin >> P[i].name >> P[i].reachTime >> P[i].needTime;
	}
	for (int i = 0; i<N; i++) {
		P[i].state = 0;
		P[i].alltime = 0;
		P[i].isrunning = 0;
		P[i].over = 0;
	}
	/*P[0].name = "P1";
	P[0].reachTime = 0;
	P[0].needTime = 4;
	P[0].state = 0;
	P[0].alltime = 0;
	P[0].over = 0;
	P[0].isrunning = 0;

	P[1].name = "P2";
	P[1].reachTime = 1;
	P[1].needTime = 3;
	P[1].state = 0;
	P[1].alltime = 0;
	P[1].over = 0;
	P[1].isrunning = 0;

	P[2].name = "P3";
	P[2].reachTime = 2;
	P[2].needTime = 5;
	P[2].state = 0;
	P[2].alltime = 0;
	P[2].over = 0;
	P[2].isrunning = 0;

	P[3].name = "P4";
	P[3].reachTime = 3;
	P[3].needTime = 2;
	P[3].state = 0;
	P[3].alltime = 0;
	P[3].over = 0;
	P[3].isrunning = 0;

	P[4].name = "P5";
	P[4].reachTime = 4;
	P[4].needTime = 4;
	P[4].state = 0;
	P[4].alltime = 0;
	P[4].over = 0;
	P[4].isrunning = 0;*/
}

void printPCB(PCB P) {
	cout << P.name << "\t\t" << P.reachTime << "\t\t" << P.needTime << "\t\t" << P.alltime << endl;
}

void setState(int all) { //找到该时刻到达的进程,并将它放入队列中  
	for (int i = 0; i < N; i++) {
		if (P[i].reachTime == all) {
			arr[Size] = i;  //放到队列的最后  
			P[i].state = 1;  //标志该进程状态激活  
			Size++;   //队列长度++  
		}
	}
}

void Sort(int Size) {  //对队列中的进程按照执行需要的时间排序,从长到短  
	for (int i = 0; i < Size; i++) {
		for (int j = i + 1; j < Size; j++) {
			if (P[arr[i]].needTime < P[arr[j]].needTime) {  //短作业放最后  
				int tmp = arr[i];
				arr[i] = arr[j];
				arr[j] = tmp;
			}
		}
	}
}

void processSchedule() {
	int all = 0; //记录系统运行总时间  

	for (int n = 0; n < N; n++) { //五个进程  
		int time = 0;  //记录该进程执行时间  
		while (true) {

			setState(all);
			Sort(Size);
				time++;
			all++;
			if (RunningP == -1) {  //如果没有进程在执行  
				if (Size > 0) {
					RunningP = arr[Size - 1];//调入运行所需时间最短的进程,即队列中最后边的进程  
					Size--; //队列长度减一  
				}
			}
			if (RunningP != -1) { //如果有进程在运行  
				if (P[RunningP].needTime == time) {//如果该进程已经执行完成  
					P[RunningP].alltime = all - P[RunningP].reachTime;//计算该进程周转时间  
					RunningP = -1;//重新标记没有进程在运行  
					break;//跳出while  
				}
			}
		}
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).