directory

First come, first served scheduling algorithm:

Short process priority scheduling algorithm:

Advantages and disadvantages of the two process scheduling algorithms

Mind mapping

Program code:


First come, first served scheduling algorithm :

First come, first served (FCFS) scheduling algorithm is one of the simplest scheduling algorithms, which can be used for job scheduling and process scheduling. When this algorithm is used in job scheduling, each schedule selects one or more jobs from the standby job queue that enter the queue first, calls them into memory, allocates resources to them, creates processes for them, and then puts them into the ready queue. When FCFS algorithm is used in process scheduling, each schedule selects a process that first enters the queue from the ready queue, allocates processors to it, and puts it into operation. The process does not abandon the processor until it is finished or blocked by an event.

 

Short process priority scheduling algorithm:

Short job (process) priority scheduling algorithm SJ(P)F refers to the algorithm of short job or short process priority scheduling. They can be used for job scheduling and process scheduling respectively. Short Job First (SJF) scheduling algorithm selects one or more jobs with the shortest estimated running time from the backup queue and calls them into memory to run. The short process first (SPF) scheduling algorithm selects a process with the shortest estimated running time from the ready queue, assigns the processor to it, and makes it execute immediately and execute until completion, or reschews when an event occurs and is blocked to give up processing.

Advantages and disadvantages of the two process scheduling algorithms

  advantages disadvantages
First come, first served scheduling algorithm 1. Fair, simple implementation, conducive to long process scheduling
2. Advantageous with CPU busy process, used in batch processing system 1. If waiting time and execution time are not considered, starvation will occur, which is not conducive to dealing with short process scheduling.
2. It is not conducive to busy I/O processes and is not suitable for time-sharing systems.
Short process priority scheduling algorithm 1. Facilitate short process scheduling
2. Limited allocation of processors to processes with short expected execution time, usually the subsequent short process does not beat the executing process 1. There is no consideration of the urgency of the job (process) and therefore no guarantee that the urgent job (process) will be handled in a timely manner.
2. It is not conducive to long process scheduling

Mind mapping

Program code:



/ * experiment topic: first come, first serve FCFS and short assignment priority SJF * * * * * * * process scheduling algorithm experiment required * * * * * * * * * 1. First come, first served scheduling algorithm FCFS: 1) scheduling algorithm is one of the most simple, suitable for job scheduling and process scheduling 2) from the backlog queue every time scheduling select one or more of the first to enter the queue assignments, put them to memory, allocation of resources, the creation process, and then into the ready queue 3) FCFS algorithm is beneficial to long assignments (process), 4) can be used for both job scheduling and process scheduling 2. Turnaround time = Completion time - Arrival time Turnaround time with rights = Turnaround time/service time */
#include<stdio.h>
#include <stdlib.h>  //malloc header file
#include <time.h>
#include <math.h>

struct node {     // Process control block
	char name;
	double arr;   // Time of arrival
	double ing;   // Service time
	double finish;// End time
	double round; // Turnaround time
	double daiquan;// Weighted turnaround time
	double pingjunround; // Average turnaround time
	double pingjundaiquan; // Average turnaround time with weights
}ai[100];
node t;

void FCFS(a)
{
	int n,i;

	printf("Please enter number of processes: \n");
	scanf("%d", &n);
	printf("Please enter %d process names \n", n);
	for (i = 0; i<n; i++) {getchar(a);scanf("%s",&ai[i].name);	
		ai[i].arr = (double) (rand() %10 + 1);  / / random
		ai[i].ing = (double) (rand() %10 + 1);  / / random
		 
	}
	/ / sorting
	for (i = 1; i < n; i++)
	{
		for (int j = 0; j < n - i; j++)
		{
			if (ai[j].arr > ai[j + 1].arr)
			{
				t = ai[j];
				ai[j] = ai[j + 1];
				ai[j + 1] = t; }}}printf("Process name \t arrival time \t service time \t end time \t turnaround time \t average turnaround time \t weighted turnaround time \t Average weighted turnaround time \n");
	ai[0].finish =ai[0].arr+ai[0].ing;
	ai[0].round=ai[0].finish-ai[0].arr;
	ai[0].daiquan=ai[0].round/ai[0].ing;
	ai[0].pingjunround=ai[0].round;
	ai[0].pingjundaiquan=ai[0].daiquan;
	printf("%c \t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf\n",ai[0].name,ai[0].arr,ai[0].ing,ai[0].finish,ai[0].round,ai[0].pingjunround,ai[0].daiquan,ai[0].pingjundaiquan);
	for (i = 1; i<n; i++) { ai[i].finish = ai[i- 1].finish + ai[i].ing;
		ai[i].round = ai[i].finish - ai[i].arr;
		ai[i].daiquan = ai[i].round / ai[i].ing;
		ai[i].pingjunround=(ai[i].round+ai[i- 1].round)/(double)(i+1);
		ai[i].pingjundaiquan=(ai[i].daiquan+ai[i- 1].daiquan)/(double)(i+1);
		printf("%c \t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf\n",ai[i].name,ai[i].arr,ai[i].ing,ai[i].finish,ai[i].round,ai[i].pingjunround,ai[i].daiquan,ai[i].pingjundaiquan); }}void SPF(a)
{
	int n,i,time=0;

	printf("Please enter number of processes: \n");
	scanf("%d", &n);
	printf("Please enter %d process names \n", n);
	for (i = 0; i<n; i++) {getchar(a);scanf("%s",&ai[i].name);	
		ai[i].arr = (double) (rand() %10 + 1);
		ai[i].ing = (double) (rand() %10 + 1);		
	}

	for ( i = 1; i<n; i++)
	{
		for (int j = 0; j<n - i; j++)
		{
			if (ai[j].arr>ai[j + 1].arr)// Swap the short arrival time to the front
			{
				t = ai[j];
				ai[j] = ai[j + 1];
				ai[j + 1] = t; }}for (int k = 0; k < n - i; k++)
		{
			if ((ai[k].ing > ai[k + 1].ing) && (ai[k].arr >= ai[k + 1].arr))// Switch the short service time to the front
			{
				t = ai[k];
				ai[k] = ai[k + 1];
				ai[k + 1] = t; }}}printf("Process name \t arrival time \t service time \t end time \t turnaround time \t average turnaround time \t weighted turnaround time \t Average weighted turnaround time \n");
	ai[0].finish =ai[0].arr+ai[0].ing;
	ai[0].round=ai[0].finish-ai[0].arr;
	ai[0].daiquan=ai[0].round/ai[0].ing;
	ai[0].pingjunround=ai[0].round;
	ai[0].pingjundaiquan=ai[0].daiquan;
	printf("%c \t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf\n",ai[0].name,ai[0].arr,ai[0].ing,ai[0].finish,ai[0].round,ai[0].pingjunround,ai[0].daiquan,ai[0].pingjundaiquan);
	

	for (i = 1; i < n; i++)  	/ / sorting
	{

		for (int j = i; j < n - 1; j++)
		{
			for (int d = i + 1; d<n; d++)
				if ((ai[i - 1].finish >= ai[j].arr) && (ai[i - 1].finish >= ai[d].arr) && (ai[j].ing > ai[d].ing)) { t = ai[j]; ai[j] = ai[d]; ai[d] = t; }}if (ai[i].arr<ai[i - 1].finish)	// The current arrival time is before the end time of the previous job
		{
			ai[i].finish = ai[i - 1].finish + ai[i].ing;
			ai[i].round = ai[i].finish - ai[i].arr;		
			ai[i].daiquan = ai[i].round / ai[i].ing;	
			ai[i].pingjunround=(ai[i].round+ai[i- 1].round)/(double)(i+1);
			ai[i].pingjundaiquan=(ai[i].daiquan+ai[i- 1].daiquan)/(double)(i+1);
		}
		else	// The current arrival time is after the end time of the previous job
		{
			ai[i].finish = ai[i].arr + ai[i].ing;
			ai[i].round = ai[i].finish - ai[i].arr;
			ai[i].daiquan = ai[i].round / ai[i].ing;
			ai[i].pingjunround=(ai[i].round+ai[i- 1].round)/(double)(i+1);
			ai[i].pingjundaiquan=(ai[i].daiquan+ai[i- 1].daiquan)/(double)(i+1); }}for (i = 1; i<n; i++) {printf("%c \t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf \t\t%.2lf\n",ai[i].name,ai[i].arr,ai[i].ing,ai[i].finish,ai[i].round,ai[i].pingjunround,ai[i].daiquan,ai[i].pingjundaiquan); }}int main(a)
{
	srand((unsigned)time( NULL));/ / random
	printf("Please select algorithm" 1-FCFS, 2-SPF "\n");
	int choose;
	scanf("%d",&choose);
	if(choose==1) {FCFS(a); }else if(choose==2) { SPF(a); }return 0;
}
Copy the code