"Operating system-1" first come first service FCFS and short job priority SJF process scheduling algorithm __ algorithm

Source: Internet
Author: User

Operating System Series

Learning to this point, found a lot of learned but long useless knowledge, over time, slowly forgotten. When the day is needed, but found to have forgotten almost, even if the document (Word, etc.), still have to learn from the beginning. Study the first semester, found that a lot of things can be learned from the blog, there are a lot of bloggers have worked hard to organize a lot of useful Bowen. So, I take this opportunity, also slowly began to organize some blog posts, constantly improve and improve. The following are the purposes of organizing your blog (IT):
Primary purpose: To record the "learning career" of the learned, constantly modify, constantly updated. (Interacting with the reader) secondly : in this "open source" era, it is a mutually beneficial act to organize and share what is learned.

Bowen series: Related experiments on operating system courses 1. First come first service FCFS and short job priority SJF process scheduling algorithm
2. Time slice rotation RR process scheduling algorithm
3. The banker algorithm to prevent process deadlock
4. Dynamic Partition Assignment algorithm
5. Virtual memory page Replacement algorithm
6. Disk Scheduling algorithm

6 Experiments related code download address: http://download.csdn.net/detail/houchaoqun_xmu/9865648

-------------------------------

First come first service FCFS and short job priority SJF process scheduling algorithm
Introduction to the concept and analysis of the case

FCFS Scheduling algorithmfirst come first service (FCFS) scheduling algorithm is a simple scheduling algorithm, which can be used both for job scheduling and process scheduling. When this algorithm is used in job scheduling, each schedule selects one or more jobs that first entered the queue from the fallback job queue, puts them into memory, assigns them resources, creates processes, and then puts them in the ready queue. When the FCFS algorithm is used in process scheduling, each schedule selects a process from the ready queue that first enters the queue, assigns the processor to it, and puts it into operation. The process does not abandon the processor until it has been run until an event has been completed or something is blocked. Case ANALYSIS: FCFS algorithm is more advantageous to long work (process), but not to the short work (process). The following table lists the time when a, B, C, D four jobs arrive at the system, the time required for service, the time to start execution, and the respective completion time, and calculates their turnaround time and weighted turnaround time.

From the table, it can be seen that short job C with the right turnaround time of up to 100, which is intolerable, and long job D with the right turnaround time is only 1.99. As a result, the FCFS scheduling algorithm is advantageous to the CPU busy type job, but is not advantageous to I/O busy type Job (process).
CPU Busy Type jobIt means that this type of job requires a lot of CPU time to compute and rarely requests I/O. The usual scientific calculations are CPU-busy jobs.
I/O busy type jobRefers to the need to frequently request I/O for CPU processing. Most of the current transactions are in I/O busy jobs.
Case Analysis 2: Scheduling performance when using FCFS scheduling algorithm
The figure above shows five processes a, B, C, D, E, their arrival time is 0, 1, 2, 3 and 4, the required service time is 4, 3, 5, 2 and 4, and the completion time is 4, 7, 12, 14 and 18 respectively. Subtracting its arrival time from the completion time of each process, the turnaround time is obtained and the weighted turnaround time of each process can be calculated.
SJF Scheduling algorithm: Short job (process) Priority scheduling algorithm SJ (P) F, refers to the short job or short process priority scheduling algorithm. They can be used for job scheduling and process scheduling, respectively. The scheduling algorithm for short job first (SJF) is to select one or more jobs from the fallback queue that estimate the shortest running time and bring them into memory.   And the short process first (SPF) scheduling algorithm is to select an estimated running time from the ready queue process, the processor assigned to it, so that it immediately execute and always execute to completion, or the occurrence of an event is blocked to abandon the processor and then rescheduling. In order to compare with the FCFS scheduling algorithm, we still use the example in the FCFS algorithm and use the SJ (P) f algorithm to reschedule, then perform the performance analysis. As can be seen from (a) and (b) above, the use of SJ (P) F algorithm, whether the average turnaround time or the average weighted turnaround time, there are more obvious improvements, especially for the short job D, the turnaround time from the original (with the FCFS algorithm) 11 down to 3 , while the average weighted turnaround time is reduced from 5.5 to 1.5.   This shows that the SJF scheduling algorithm can effectively reduce the average waiting time and improve the throughput of the system. The SJ (P) F scheduling algorithm also has some disadvantages: the algorithm is disadvantageous to long operation, such as the turnaround time of job C from 10 to 16, and its weighted turnaround time increases from 2 to 3.1. More seriously, if a long job (process) enters the system's fallback queue (the ready queue), the scheduler will always prioritize those (even backward) short jobs (processes), resulting in long jobs (processes) not being scheduled for long periods of time.
The algorithm does not take into account the urgency of the operation, so it can not guarantee the urgency of the operation (process) will be processed in time.
Because the length of the job (process) is based on the estimated execution time provided by the user, and the user may intentionally or unintentionally shorten the estimated running time of the job, the algorithm is not necessarily able to really do short job priority scheduling.
Ii. Introduction of the experimentProblem Description:

The design program simulates the process of first come first service FCFS and short job first SJF scheduling process. It is assumed that there are n processes that arrive at the T1,..., TN at all times, and that they require a service time of S1,..., Sn respectively. First-come first-served FCFS and short job priority SJF process scheduling algorithm are used to calculate the completion time, turnaround time and weighted turnaround time of each process, and to count the average turnaround time of n processes and the average weighted turnaround time.

Program Requirements:

Number of----processes N; arrival time of each process T1 ..., TN and service time S1, ..., Sn; select Algorithm 1-FCFS,2-SJF.

The----requires first-come first-served FCFS and short job priority SJF to run separately, calculate the turnaround time and weighted turnaround time of each process, and calculate the average turnaround time and weighted average turnaround time of all processes.

----Output: Requires the simulation of the entire scheduling process, output every moment of the process running state, such as "moment 3: Process B began to run" and so on.

----Output: Requires output turnaround time, weighted turnaround time, average turnaround time of all processes, and weighted average turnaround time for each process. third, the program design and development programming: Initial () for initialization. Input () Inputs to arrival time and service time. Get_firstprocess () Gets the first process, the FCFS and SJF algorithms are implemented the same. FCFS () processes the algorithm. SJF () processes the algorithm. Choose_algorithm (); Select the category of the implementation algorithm: it has fault-tolerant characteristics. FCFS:

void FCFS () {/* 1. Locate the coordinates of the process that arrived first, and compute the relevant information 2. Find the process of arrival in turn/int startworktime = 0;  Represents the start of execution time = All service time before the current process and int first = Get_firstprocess ();
	Get first process isfinished_fcfs[first] = true;
	Finishtime[first] = Arrivaltime[first] + Servicetime[first];   Startworktime + = Servicetime[first];   Start execution time of next process wholetime[first] = Finishtime[first]-Arrivaltime[first]; Turnaround time = Completion time-arrival time Weightwholetime[first] = Wholetime[first]/servicetime[first]; Weighted turnaround time = turnaround time/service time/Next process int nextprocess = n;  Initializes the next process's subscript beyond the bounds for (int i=1;i<n;i++) {nextprocess = n; Each time the subscript for the next process is updated for (int j=0;j<n;j++) {if (!isfinished_fcfs[j])//indicates that the current process has not completed the calculation of the relevant information {if (arrivaltime
					[j]<=startworktime)//satisfies the arrival time less than or equal to the start execution time {if (nextprocess==n) {nextprocess = J;    else {if (arrivaltime[nextprocess]>arrivaltime[j])//filter out the first arrived process {nextprocess=j; Get current process: first reached process}}}}//for (j)//GetThe relevant information is computed isfinished_fcfs[nextprocess] = True after the process nextprocess currently needs to be processed;
		Finishtime[nextprocess] = servicetime[nextprocess] + startworktime;  Startworktime + = servicetime[nextprocess];
		Get the "Start execution time" wholetime[nextprocess] = finishtime[nextprocess]-arrivaltime[nextprocess] corresponding to the next process;

	Weightwholetime[nextprocess] = (double) wholetime[nextprocess]/servicetime[nextprocess];
	}//for (i)//Calculate average turnaround time and mean weighted turnover time double TOTALWT = 0;
	Double TOTALWWT = 0;
		for (int i=0;i<n;i++) {totalwt+=wholetime[i];
	Totalwwt+=weightwholetime[i];
	} AVERAGEWT_FCFS = totalwt/n;

	AVERAGEWWT_FCFS = totalwwt/n;
	Output detection display ();
	cout<< "average turnaround time =" <<AverageWT_FCFS<<endl;
	cout<< "Average weighted turnover time =" <<AverageWWT_FCFS<<endl;
cout<< "******************************************************" <<endl; }
SJF:
void Sjf () {//similar to SCSF, the same method obtains the first process int STARTWORKTIME_SJF = 0;  Represents the start of execution time = the sum of all service times before the current process int first = get_firstprocess ();
	Get first process isfinished_sjf[first] = true;
	Finishtime[first] = Arrivaltime[first] + Servicetime[first];   STARTWORKTIME_SJF + = Servicetime[first];   Start execution time of next process wholetime[first] = Finishtime[first]-Arrivaltime[first]; Turnaround time = Completion time-arrival time Weightwholetime[first] = (double) Wholetime[first]/servicetime[first];
	Weighted turnaround time = turnaround time/service time//Get the next process's subscript int NEXTPROCESS_SJF = n;
		for (int i=1;i<n;i++) {NEXTPROCESS_SJF = n; for (int j=0;j<n;j++) {if (!isfinished_sjf[j]) {if (ARRIVALTIME[J]&LT;=STARTWORKTIME_SJF) {if (
					Nextprocess_sjf==n) {NEXTPROCESS_SJF = J;   else {if (Servicetime[nextprocess_sjf]>servicetime[j]) {NEXTPROCESS_SJF = J;
		Get the lowest run time job's Subscript}}}//for (j)//To process the obtained processes ISFINISHED_SJF[NEXTPROCESS_SJF] = true; finishtime[NEXTPROCESS_SJF] = SERVICETIME[NEXTPROCESS_SJF] + startworktime_sjf; 
		STARTWORKTIME_SJF + = SERVICETIME[NEXTPROCESS_SJF];
		WHOLETIME[NEXTPROCESS_SJF] = FINISHTIME[NEXTPROCESS_SJF]-ARRIVALTIME[NEXTPROCESS_SJF];

	WEIGHTWHOLETIME[NEXTPROCESS_SJF] = (double) WHOLETIME[NEXTPROCESS_SJF]/SERVICETIME[NEXTPROCESS_SJF];
	}//for (i) Double TOTALWT = 0;
	Double TOTALWWT = 0;
		for (int i=0;i<n;i++) {totalwt+=wholetime[i];
	Totalwwt+=weightwholetime[i];
	} AVERAGEWT_SJF = totalwt/n;

	AVERAGEWWT_SJF = totalwwt/n;
	Output detection display ();
	cout<< "average turnaround time =" <<AverageWT_SJF<<endl;
	cout<< "Average weighted turnover time =" <<AverageWWT_SJF<<endl;
cout<< "******************************************************" <<endl; }
Iv. Analysis of experimental results--Input data:--Number of processes n = 5--Arrival time: 0 1 2 3 4--Service Time: 4 3 5 2-4 FCFS algorithm:
SJF algorithm:
v. The source of the experiment
Operating system _ Experiment one. CPP: Defines the entry point for a console application. //////Test topic: First come first service FCFS and short job priority SJF process scheduling algorithm ******* concept ******* 1. First come first serve fcfs:2. Short job priority Sjf:3. Advanced scheduling: According to some algorithm, in the external memory in the back of the queue in the operation of those operations into RAM, when the completion of the operation to do the treatment of 4. Intermediate Dispatch 5. Low-level scheduling: objects are processes (or kernel-level threads); three basic mechanisms: queuing, dispatcher, context switching mechanism 6. Guidelines for scheduling methods and algorithms 1 user-oriented guidelines: Short cycle time, fast response time, guarantee of cut-off time, priority guideline 2, System-oriented guidelines: High system throughput, good processor utilization, and balanced utilization of all kinds of resources 7. Scheduling algorithm: According to the system's resource allocation strategy, the resource allocation algorithm 8. FCFS scheduling algorithm is advantageous to the CPU busy type job, but is unfavorable to I/O busy type job or the process ******* experiment request ********* 1.
	First come first. Service Scheduling algorithm (FCFS:1) is one of the simplest scheduling algorithms, suitable for job scheduling and process scheduling 2 each schedule is to select one or more of the first jobs from the fallback queue, transfer them into memory, allocate resources, create processes, and then put them in the ready queue 3) The FCFS algorithm is advantageous to the long job (process), does not favor the short job (process) 4) can be used for job scheduling, also can be used for process scheduling 2.

Turnaround time = Completion time-arrival time with right turnaround time = turnaround time/Service time * * #include <iostream> #include <iomanip> using namespace std;  #define Maxnum//typedef struct//{//int arrivaltime[maxnum];  Arrival time//int servicetime[maxnum];   Service time//int finishtime[maxnum];

Completion time/////}process; /* Algorithm idea: 1. Initial () to initialize 2. Input () Enter 3 for arrival time and service time. Get_firstprocess () Gets the first process, and the implementation of the FCFS and SJF algorithms is equal to 4. FCFS () handles the algorithm 5.
  SJF () to process the algorithm 6. Choose_algorithm (); Select the category of the implementation algorithm: The fault-tolerant feature///the same array subscript corresponding to the same process information int arrivaltime[maxnum];  Arrival time int Servicetime[maxnum];   Service time int finishtime[maxnum];    Completion time int wholetime[maxnum];      Turnover time double weightwholetime[maxnum];  With right turnover time double averagewt_fcfs,averagewt_sjf; The mean turnaround time of the FCFS algorithm, the average turnaround time of the SJF algorithm is double AVERAGEWWT_FCFS,AVERAGEWWT_SJF;
The mean weighted turnaround time of FCFS algorithm, the mean weighted turnaround time of SJF algorithm is bool Isfinished_fcfs[maxnum];

BOOL Isfinished_sjf[maxnum];

static int n;
	void Initial ()///Determine the number of processes before initializing {cout<< "Please enter the number of jobs (process) n=";

	cin>>n;
		for (int i=0;i<n;i++) {Arrivaltime[i] = 0;
		Servicetime[i] = 0;
		Finishtime[i] = 0;
		Wholetime[i] = 0;
		Weightwholetime[i] = 0;
		AVERAGEWT_FCFS = 0;
		AVERAGEWT_SJF = 0;
		AVERAGEWWT_FCFS = 0;
		AVERAGEWWT_SJF = 0;
		Isfinished_fcfs[i] = false;
	Isfinished_sjf[i] = false;
	} void Input () {cout<<) Enter the arrival time for each process, respectively: "<<endl;
	for (int i=0;i<n;i++) {cin>>arrivaltime[i]; cout<< "Please enter the service time for each process individually:" <<endl;
	for (int i=0;i<n;i++) {cin>>servicetime[i];
	//output user input information cout<< "******************************************************" <<endl;

	cout<< "Number of processes entered by the user n=" <<n<<endl;
	cout<< "The service time entered by the user is:" <<endl;
	for (int i=0;i<n;i++) {cout<<arrivaltime[i]<< "";

	} cout<<endl;
	cout<< "The service time entered by the user is:" <<endl;
	for (int i=0;i<n;i++) {cout<<servicetime[i]<< "";
} cout<<endl<< "******************************************************" <<endl;
	int get_firstprocess () {int-i = maxnum;
		for (int i=0;i<n;i++) {if (Arrivaltime[i]<=arrivaltime[first]) {a = i;
} return to the I;
	} void Display () {cout<< "******************************************************" <<endl;
	cout<< "process-related information is as follows:" <<endl;
	COUT&LT;&LT;SETW << "process name (ID)" << "";
	COUT&LT;&LT;SETW << "Arrival Time" << ""; COUT&LT;&LT;SETW << "Service Time" <<"
	";
	COUT&LT;&LT;SETW << "Completion time" << "";
	COUT&LT;&LT;SETW << "Turnaround time" << "";
	COUT&LT;&LT;SETW << "With power turnaround time" <<endl;
		for (int i = 0;i<n;i++) {COUT&LT;&LT;SETW (a) <<i+1<< "";
		COUT&LT;&LT;SETW (a) <<ArrivalTime[i]<< "";
		COUT&LT;&LT;SETW (a) <<ServiceTime[i]<< "";
		COUT&LT;&LT;SETW (a) <<FinishTime[i]<< "";
		COUT&LT;&LT;SETW (a) <<WholeTime[i]<< "";
	COUT&LT;&LT;SETW (a) <<WeightWholeTime[i]<< "" <<endl;  } void FCFS () {/* 1. Locate the coordinates of the first process to arrive, and calculate the relevant information 2. Find the process of the next arrival/int startworktime = 0;  Represents the start of execution time = All service time before the current process and int first = Get_firstprocess ();
	Get first process isfinished_fcfs[first] = true;
	Finishtime[first] = Arrivaltime[first] + Servicetime[first];   Startworktime + = Servicetime[first];   Start execution time of next process wholetime[first] = Finishtime[first]-Arrivaltime[first]; Turnaround time = Completion time-arrival time Weightwholetime[first] = Wholetime[first]/servicetime[first]; With Right weekTurn time = turnaround time/service time/Next process int nextprocess = n;  Initializes the next process's subscript beyond the bounds for (int i=1;i<n;i++) {nextprocess = n; Each time the subscript for the next process is updated for (int j=0;j<n;j++) {if (!isfinished_fcfs[j])//indicates that the current process has not completed the calculation of the relevant information {if (arrivaltime
					[j]<=startworktime)//satisfies the arrival time less than or equal to the start execution time {if (nextprocess==n) {nextprocess = J;    else {if (arrivaltime[nextprocess]>arrivaltime[j])//filter out the first arrived process {nextprocess=j; Get the current process: first-reached process}}}}//for (j)////Nextprocess the relevant information after obtaining the current process to be processed isfinished_fcfs[nextproces
		s] = true;
		Finishtime[nextprocess] = servicetime[nextprocess] + startworktime;  Startworktime + = servicetime[nextprocess];
		Get the "Start execution time" wholetime[nextprocess] = finishtime[nextprocess]-arrivaltime[nextprocess] corresponding to the next process;

	Weightwholetime[nextprocess] = (double) wholetime[nextprocess]/servicetime[nextprocess];
	}//for (i)//Calculate average turnaround time and mean weighted turnover time double TOTALWT = 0;
	Double TOTALWWT = 0; for (int i=0;i<n;i++) {Totalwt+=wholetime[i];
	Totalwwt+=weightwholetime[i];
	} AVERAGEWT_FCFS = totalwt/n;

	AVERAGEWWT_FCFS = totalwwt/n;
	Output detection display ();
	cout<< "average turnaround time =" <<AverageWT_FCFS<<endl;
	cout<< "Average weighted turnover time =" <<AverageWWT_FCFS<<endl;
cout<< "******************************************************" <<endl;  } void Sjf () {//SCSF is similar to the same method of obtaining the first process int STARTWORKTIME_SJF = 0;  Represents the start of execution time = the sum of all service times before the current process int first = get_firstprocess ();
	Get first process isfinished_sjf[first] = true;
	Finishtime[first] = Arrivaltime[first] + Servicetime[first];   STARTWORKTIME_SJF + = Servicetime[first];   Start execution time of next process wholetime[first] = Finishtime[first]-Arrivaltime[first]; Turnaround time = Completion time-arrival time Weightwholetime[first] = (double) Wholetime[first]/servicetime[first];
	Weighted turnaround time = turnaround time/service time//Get the next process's subscript int NEXTPROCESS_SJF = n;
		for (int i=1;i<n;i++) {NEXTPROCESS_SJF = n;
for (int j=0;j<n;j++) {if (!isfinished_sjf[j]) {				if (ARRIVALTIME[J]&LT;=STARTWORKTIME_SJF) {if (nextprocess_sjf==n) {NEXTPROCESS_SJF = J;   else {if (Servicetime[nextprocess_sjf]>servicetime[j]) {NEXTPROCESS_SJF = J;
		Get the lowest run time job's Subscript}}}//for (j)//To process the obtained processes ISFINISHED_SJF[NEXTPROCESS_SJF] = true;
		FINISHTIME[NEXTPROCESS_SJF] = SERVICETIME[NEXTPROCESS_SJF] + startworktime_sjf; 
		STARTWORKTIME_SJF + = SERVICETIME[NEXTPROCESS_SJF];
		WHOLETIME[NEXTPROCESS_SJF] = FINISHTIME[NEXTPROCESS_SJF]-ARRIVALTIME[NEXTPROCESS_SJF];

	WEIGHTWHOLETIME[NEXTPROCESS_SJF] = (double) WHOLETIME[NEXTPROCESS_SJF]/SERVICETIME[NEXTPROCESS_SJF];
	}//for (i) Double TOTALWT = 0;
	Double TOTALWWT = 0;
		for (int i=0;i<n;i++) {totalwt+=wholetime[i];
	Totalwwt+=weightwholetime[i];
	} AVERAGEWT_SJF = totalwt/n;

	AVERAGEWWT_SJF = totalwwt/n;
	Output detection display ();
	cout<< "average turnaround time =" <<AverageWT_SJF<<endl; cout<< "Average weighted turnover time =" <<averagewwt_sjf<<endl;
cout<< "******************************************************" <<endl;
	} void Choose_algorithm () {cout<< "Please select algorithm" 1-FCFS,2-SJF "" <<endl;
	int choose;
	cin>>choose;
	if (choose==1) {FCFS ();
		else if (choose==2) {SJF ();
		else {cout<< "Please enter the correct selection" 1-FCFS,2-SJF "<<endl;
		cout<< "******************************************************" <<endl;  Choose_algorithm ();
	Recursive calls, implementation of the option to exclude errors can also continue to input} int main () {Initial ();
	Input ();
	Choose_algorithm ();
	System ("pause");
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.