The basic process scheduling method and implementation

   Four features of the process: concurrent, shared, virtual, asynchronously.

   The key to the process of scheduling is complicated by a ring.

   There are several scheduling algorithms in the operating system, some scheduling algorithm is suitable for job scheduling, some scheduling algorithm is suitable for process scheduling, both some scheduling algorithms are applicable.

  1. First come, first served (FCFS)

  A simple scheduling algorithm for job and process scheduling. First come first serve algorithm first-served basis in accordance with the processes / operations to be scheduled. When the job scheduling algorithm is employed, each time schedule will be taken from the backup job arrives first in the queue, to allocate memory for him, creating the PCB, into the ready queue; when using the process scheduling algorithm, each scheduler will from remove the first to enter the ready queue of the queue process, he assigned to processor (main processor memory = CPU + + IO devices).

  2. Short job first (SJF)

  The length of the process is a job or job or process requirements length of run time to measure.

       3. Priority Scheduling

  Operation or process priorities to determine priority scheduling rights.

  (1) static priority --- priority will be determined and will not change before the process / job scheduling.

  (2) dynamic priority --- priorities will change with the implementation of the process, more flexible and science.

  4. The method round-robin (RR)

  The process is mainly used for the scheduling of time-sharing system . Process / job on a queue , the CPU runs out the first process after a time slice, in which the tail, a polling performed.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include <algorithm>  
using namespace std;

int choice; // Options
int n; // number of processes  

bool ok [100] = {0}; // 1 is completed job completion is not completed 0
int last [100] = {0}; // time remaining tasks 

struct pcb{
	int pid; // process ID
	int atime; // arrival time
	int rtime; // run time
	int stime; // Start Time
	int etime; // End Time
	int time; // turnaround time
	float dtime; // Weighted turnaround time
	int priority; // priority
	int timechip; // time slices
} Pro [100]; // process the largest array of input and output parameters


// first come first serve algorithm collation process, first in ascending order according to the time of arrival, and then arranged in ascending order process ID  
bool fcfscmp(pcb a,pcb  b) {
	if (a.atime != b.atime)return a.atime < b.atime;
	return a.pid < b.pid;
}

// dynamic priority algorithm collation process, the first process in accordance with the number of ascending priority, then the arrival time ascending, then the process ID in ascending order  
bool pricmp(pcb a, pcb b) {
	if (a.priority != b.priority)return a.priority < b.priority;
	if (a.atime != b.atime)return a.atime < b.atime;
	return a.pid < b.pid;
}

// first-come, first-served
void FCFS()
{
	int t = 0;
	int dt = 0;
	// Sort the earliest arrival time job
	sort(pro + 1, pro + 1 + n, fcfscmp);
	// order to complete the job
	printf ( "\ n ==== PID arrival time running time start time end time weighted turnaround time turnaround time ==== \ n");
	for (int i = 1; i <= n; i++) {   

		pro [i] .stime = max (t, pro [i] .atime); // start time = max (System Time, Time)
		pro[i].etime = pro[i].stime + pro[i].rtime;
		pro [i] .time = pro [i] .etime - pro [i] .atime; // End = turnaround time - arrival
	    pro [i] .dtime = pro [i] .time / pro [i] .rtime; // = Weighted turnaround time turnaround time / operating time

		// total turnaround time, total weighted turnaround time
		t += pro[i].time;
		dt += pro[i].dtime;
		
		printf("      %d     %d        %d         %d       %d        %d          %3.3f\n",
			pro[i].pid, pro[i].atime, pro[i].rtime, pro[i].stime,pro[i].etime,pro[i].time,pro[i].dtime);
	}
	float avet = (float)t / n;
	float avedt = (float)dt / n;
	printf ( "turnaround time average% 3.3f, turnaround time weighted average% 3.3f \ n", avet, avedt);
	
}

// short operating priority
void SPF()
{
	// job completion of the first set to 0
	memset(ok, 0, sizeof(ok));
	int t = 0;
	int dt = 0;
	int min = 1;
	printf ( "\ n ==== PID arrival time running time start time end time weighted turnaround time turnaround time ==== \ n");
	for (int i = 1; i <= n; i++) {

		// 1. Current minimum time to find a job
		int flag = 0; // tag if there is a job arrival
		while (flag == 0){
			for (int j = 1; j <= n; j++) {
				if (ok [j] == 1) continue; // already running to complete the process  
				if (pro [j] .atime> t) continue; // process does not reach  
				// emergence of a process has been reached
				if (pro[j].atime <= t && flag == 0) {
					min = j;
					flag = 1;
				}
				// there are two processes has been reached, the process of comparing the shortest time
				else if (pro[j].atime <= t && flag == 1) {
					if ((pro[j].rtime < pro[min].rtime) || (pro[j].rtime == pro[min].rtime&&pro[j].pid < pro[min].pid)) {
						// run-time process priority short running time is the same, in order  
						min = j;
					}
				}
			}// end of for

			// At this point, there is no process to reach, time +1
			if (flag == 0) t++;
		}

		// 2. To complete the job
		pro[i].stime = max(t, pro[min].atime);
		pro[i].etime = pro[i].stime + pro[min].rtime;
		pro [i] .time = pro [i] .etime - pro [i] .atime; // End = turnaround time - arrival
		pro [i] .dtime = pro [i] .time / pro [i] .rtime; // = Weighted turnaround time turnaround time / operating time

		// total turnaround time, total weighted turnaround time
		t += pro[i].time;
		dt += pro[i].dtime;

		printf("      %d     %d        %d         %d       %d        %d          %3.3f\n",
			pro[i].pid, pro[i].atime, pro[i].rtime, pro[i].stime, pro[i].etime, pro[i].time, pro[i].dtime);

		ok [min] = 1; // this process completed flag
	}
	float avet = (float)t / n;
	float avedt = (float)dt / n;
	printf ( "turnaround time average% 3.3f, turnaround time weighted average% 3.3f \ n", avet, avedt);

}

/ * Determine whether or not all the processes are finished * /
int charge()
{
	for (int i = 1; i<=n; i++)
	{
		if (last[i] != 0)
			return 1;
	}
	return 0;
}
// Round-Robin
void RR() {
	// job is completed array is set to 0
	memset(ok, 0, sizeof(ok));
	int t = 0 , dt = 0;
	sort (pro + 1, pro + n + 1, fcfscmp); // Sort: first-come, first-served basis behind the poll in favor ---
	
	int i = 0;
	// * key: the process of recording time remaining
	for (int i = 1; i <= n; i++)
	{
		last[i] = pro[i].rtime;
	}
	int chip = pro [1] .timechip; // time slices
	int time = pro [1] .atime; // initial value of the current time 
	
	// sequential polling, run ready state, blocking state waiting time of arrival
	printf ( "\ n ==== PID arrival time running time start time end time weighted turnaround time turnaround time ==== \ n");
	while (charge ()) // can also explain the process into the charge
	{
		int flag = 0; // tag if there are ready task
		for (i = 1; i <= n; i ++) // perform the processes round-robin method 
		{
			if (ok[i] == 1)
				continue; // process has been completed 
				
				// ready process cycle
				if (last [i] <= chip && time> = pro [i] .atime) // process is not completed and services but much less than a time slice is equal to 
				{
					flag = 1;
					// records first started running time
					if (pro[i].rtime == last[i]) 
						pro [i] .stime = time; // start time = current time

					// update time, the task is completed
					time += chip;
					last[i] = 0;
					ok[i] = 1;
	
					pro [i] .etime = time; // End Time
					pro [i] .time = pro [i] .etime - pro [i] .atime; // End = turnaround time - arrival
					pro [i] .dtime = pro [i] .time / pro [i] .rtime; // = Weighted turnaround time turnaround time / operating time

					// total turnaround time, total weighted turnaround time
					t += pro[i].time;
					dt += pro[i].dtime;

					printf("      %d     %d        %d         %d       %d        %d          %3.3f\n",
						pro[i].pid, pro[i].atime, pro[i].rtime, pro[i].stime, pro[i].etime, pro[i].time, pro[i].dtime);
						
				}
				else if (last [i]> chip && time> = pro [i] .atime) // unfinished process but need the service time is greater than the at least one chip 
				{
					flag = 1;
					// records first started running time
					if (last[i]==pro[i].rtime)
						pro [i] .stime = time; // start time = current time

					time += chip;
					last[i] -= chip;
					
				}
		}//end of for

		// do not have a ready process, increment time
		if (flag == 0)
		{
			time += chip;
		}
	}//end of while
	float avet = (float)t / n;
	float avedt = (float)dt / n;
	printf ( "turnaround time average% 3.3f, turnaround time weighted average% 3.3f \ n", avet, avedt);

}

// priority scheduling
void PRI() {
	// job is completed array is set to 0
	memset(ok, 0, sizeof(ok));
	int t = 0, dt = 0;
	sort (pro + 1, pro + n + 1, pricmp); // Sort: Priority

	int i = 0;
	// * key: the process of recording time remaining
	for (int i = 1; i <= n; i++)
	{
		last[i] = pro[i].rtime;
	}
	int chip = pro [1] .timechip; // time slices
	int time = pro [1] .atime; // initial value of the current time 
	
	// poll in order of priority, prioritization polling again End
	printf ( "\ n ==== PID arrival time running time start time end time weighted turnaround time turnaround time ==== \ n");
	while (charge ()) // can also explain the process into the charge
	{
		int flag = 0; // tag if there are ready task
		for (i = 1; i <= n; i ++) // perform the processes round-robin method 
		{
			if (ok[i] == 1)
				continue; // process has been completed 

			// ready process cycle
			if (last [i] <= chip && time> = pro [i] .atime) // process is not completed and services but much less than a time slice is equal to 
			{
				flag = 1;
				// records first started running time
				if (pro[i].rtime == last[i])
					pro [i] .stime = time; // start time = current time

				// update time, the task is completed
				time += chip;
				last[i] = 0;
				ok[i] = 1;

				pro [i] .etime = time; // End Time
				pro [i] .time = pro [i] .etime - pro [i] .atime; // End = turnaround time - arrival
				pro [i] .dtime = pro [i] .time / pro [i] .rtime; // = Weighted turnaround time turnaround time / operating time

				// total turnaround time, total weighted turnaround time
				t += pro[i].time;
				dt += pro[i].dtime;

				printf("      %d     %d        %d         %d       %d        %d          %3.3f\n",
					pro[i].pid, pro[i].atime, pro[i].rtime, pro[i].stime, pro[i].etime, pro[i].time, pro[i].dtime);

			}
			else if (last [i]> chip && time> = pro [i] .atime) // unfinished process but need the service time is greater than the at least one chip 
			{
				flag = 1;
				// records first started running time
				if (last[i] == pro[i].rtime)
					pro [i] .stime = time; // start time = current time

				time += chip;
				last[i] -= chip;
				pro [i] .priority - = 1; // if not completed, the priority of -1
			}
		}//end of for

		sort (pro + 1, pro + n + 1, pricmp); // Sort: Priority
		// do not have a ready process, increment time
		if (flag == 0)
		{
			time += chip;
		}
	}//end of while
	float avet = (float)t / n;
	float avedt = (float)dt / n;
	printf ( "turnaround time average% 3.3f, turnaround time weighted average% 3.3f \ n", avet, avedt);

}

// input interface
void Menu()
{
	while (1){
		n = 0;
		choice = 0;
		printf ( "=== select algorithm: === \ n * 1. FCFS * \ n * 2. short operating priority * \ n * 3. round-robin * \ n * 4. priority scheduling * \ n * 0. exit * \ n selection: ");
		scanf("%d", &choice);
		if (choice == 0) 
			return;
		printf ( "Please select the number of processes:");
		scanf("%d", &n);
		printf ( "*** Please write sequentially *** \ n PID arrival time running time priority slot size: \ n");
		for (int i = 1; i <= n; ++i) {
			scanf("%d %d %d %d %d", &pro[i].pid, &pro[i].atime, &pro[i].rtime, &pro[i].priority, &pro[i].timechip);
		}
		if (n == 0)
			return;
		switch (choice) {
		case 1: FCFS (); break; // first-come, first-served scheduling algorithm  
		case 2: SPF (); break; // short operating priority scheduling algorithm  
		case 3: RR (); break; // round-robin scheduling algorithm  
		case 4: PRI (); break; // priority scheduling algorithm  
		}
		printf("\n");
	}
}

int main ()
{
	Menu();
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/Duikerdd/p/11963579.html