Short job priority scheduling algorithm (c++)

The basic scheduling algorithm of the operating system process, short job first is an improvement of the first-come-first-serve algorithm, which can obtain the maximum system throughput. The difference between it and the first-come-first-serve algorithm is that when there are multiple processes in the ready queue, the one with the shortest service time should be selected for execution, so that the number of processes that can be executed within a certain period of time is the largest (maximum throughput)

Enter the number of upcoming processes and process information, time slice size, initialize pcb

The process that arrives first and the shortest is switched to position 1, and the completion time of the first process is calculated.

 Set the system time to this value, and set the next execution program number nextprocess

 The number of completed processes sum=1

Into an endless loop

{ if (number of completed processes >= total number of processes)

         Break out of the infinite loop

      Set nextprocess to sum+1

      Loop to find out if there is a process that has arrived and is shorter, and switch to the nextprocess position

      if (the process of the nextprocess bit has not arrived)

      The system time becomes the time it arrives

      Calculate the completion time of the nextprocess process, the system time is equal to this time, sum+1

}

The code is implemented as follows


#define N 5     //进程数量
#include<iostream>
using namespace std;

struct pcb
{
	char process_name[10];   //进程名字
	int arrive_time;   		//到达时间
	int service_time;   	//服务时间
	int complete_time;   	//完成时间

}PCB[N+1];             //0号单元不存数据,只做交换

int sjf(int n)
{
	int i;
	cout << "\n请输入各进程的信息\n" << endl;
	for(i=1;i<=n;i++)      //输入各进程信息
	{
		cout << "------\n请输入第" << i << "进程名字: ";
		cin >> PCB[i].process_name ;
		cout << "请输入到达时间: " ;
		cin >> PCB[i].arrive_time  ;
		cout << "请输入服务时间: " ;
		cin >> PCB[i].service_time ;

	}
	{	//最先到达且最短的进程换到1号位
		int temp = 1;
		for (i = 2; i <= n; i++)
		{
			if (PCB[temp].arrive_time > PCB[i].arrive_time ||
				(PCB[temp].arrive_time == PCB[i].arrive_time && PCB[temp].service_time > PCB[i].service_time))
			{
				temp = i;
			}
		}
		PCB[0] = PCB[temp];
		PCB[temp] = PCB[1];
		PCB[1] = PCB[0];				
	}
	
	int sum=1;     			  //已完成进程数
	PCB[1].complete_time = PCB[1].arrive_time + PCB[1].service_time;
	int time = PCB[1].complete_time;    //第一个进程结束时间(最先到达且最短的) ,也是当前系统时间

	cout << "\n第1次进程调度运行的进程为 :   " << PCB[1].process_name  << endl;

	int nextprocess ;     //下一次调度进程的pcb序号
	while(1)
	{	if(sum >= n)
		  break;

		nextprocess = i = sum+1;
		while(i<=n)
		{								//未执行进程中有已到达且更短的
			if(time >= PCB[i].arrive_time  && PCB[i].service_time < PCB[nextprocess].service_time )
				nextprocess = i ;
			i++;
		}

		{
			PCB[0]=PCB[nextprocess];       		//将下一个执行进程换到sum+1位
			PCB[nextprocess]=PCB[sum+1];
			PCB[sum+1]=PCB[0];
			nextprocess = sum + 1;
		}
		
		if(PCB[nextprocess].arrive_time > time)
		{
			time = PCB[nextprocess].arrive_time ;
		}

		time +=  PCB[nextprocess].service_time;     //nextprocess执行完的时间
		PCB[nextprocess].complete_time = time;
		sum++;
		cout << "第"<< sum << "次进程调度运行的进程为 :   "<< PCB[nextprocess].process_name << endl;
	}

	return 0;
}


void print()
{
	int i = 1;
	float round_time[N+1], force_round_time[N+1], sum = 0;
	cout << "\n 进程   |" << "到达时间  |" << "  服务时间   |" << "  完成时间   |" << "  周转时间  |" << " 带权周转时间" << endl;
	while (i<N+1)
	{
		round_time[i] = PCB[i].complete_time - PCB[i].arrive_time;
		force_round_time[i] = round_time[i] / PCB[i].service_time;
		cout << PCB[i].process_name
			<< "\t|  " << PCB[i].arrive_time
			<< "\t   | " << PCB[i].service_time << "\t\t |" << PCB[i].complete_time
			<< "\t       | " << round_time[i]
			<< "\t    | " << force_round_time[i]
			<< endl;
		sum += force_round_time[i];
		i++;
	}

	cout << "\n\n系统平均带权周转时间: " << (sum / --i) << endl;
}


int main()
{
	cout << "\t\t短作业优先调度算法" << endl;
	sjf(N);
	print();
	return 0;
}

Guess you like

Origin blog.csdn.net/ass133755/article/details/128600895