[operating system disk scheduling algorithm] OS experiment C language code to realize FCFS/SSTF/SCAN/CSCAN

preface

  this paper implements four disk scheduling algorithms FCFS/SSTF/SCAN/CSCAN in C language. It provides manual input of track number and random generation of track number. The output is the track number accessed at each step, the moving distance at each step and the average moving distance.
   in addition to the consistency between the operation results and the teaching materials, it also has two highlights: 1. The maximum random track number can be modified (when the track serial number is generated randomly) 2. The track number of initial access can be modified (which track number to start searching in SSTF, scan and cscan algorithm).
   the program has good interaction and clear interface. It is the program I accepted in the operating system class. If there are any problems in the text, please point them out in the comment area and I will modify them in time. I hope this article can help you.

  this paper will roughly sort out the overall idea and implementation highlights, and will not repeat the problems that are too detailed.
  at the end of the article, there is a complete source code link for download. Friends in need can download it by themselves. (it can be used as the test acceptance procedure or the verification means of the postgraduate entrance examination hh)

1, Program running result display





Here, first paste the program header file and main function (excluding custom functions) [the complete program download link is in the last part], which is convenient for readers to understand and expand the following description.

#include <stdio.h>
#include <stdlib.h>
#define MAX_DISK 200
#define BEGIN_DISK 100
int main()
{
    while (1)
    {
        int disk_num = 0;
        printf("Please enter the track number sequence length:\n");
        scanf("%d", &disk_num);
        int list[disk_num];
        init(list, disk_num);
        int result_list[disk_num];
        init(result_list, disk_num);
        int choice=0;
        printf("Please select: 1.Self input page data 2.Randomly generate page data\n");
        scanf("%d", &choice);
        if(choice==1)
        {
	        printf("Please enter the track number sequence:\n");
	        for (int i = 0; i < disk_num; i++)
	            scanf("%d", &list[i]);	
		}else if(choice==2)
		{	
			int flag[MAX_DISK+1];init(flag,MAX_DISK+1);
			for (int i = 0; i < disk_num; i++)
			{
				int a = rand() % (MAX_DISK) + 1;//Generate disk_num random numbers from 1 to 200 
				if (flag[a]==-1)
				{
					list[i]=a; 
					flag[a]=1;
				}else
				{
					i=i-1;
				}				
			}
			printf("Track number sequence is:\n");
			for(int i=0;i<disk_num;i++)
			{
				printf("%d ",list[i]);
				if((i+1)%10==0)
           		printf("\n");
			}
	        printf("\n");  
		}else
		{
			 printf("----------Illegal number entered, please enter 1 or 2----------\n\n");
			 continue;
		} 
        FCFS(list, disk_num, result_list);
        init(result_list, disk_num);
        SSTF(list, disk_num, result_list);
        init(result_list, disk_num);
        SCAN(list, disk_num, result_list);
        init(result_list, disk_num);
        CSCAN(list, disk_num, result_list);
        init(result_list, disk_num);
		printf("\n\n--------------------\n\n");
    }
}

Of which:
void init(int list[], int num)
void show_result(int list[], int num)
void quicksort(int num[],int count,int left,int right)
void FCFS(int list[], int disk_num,int result_list[])
void SSTF(int list[], int disk_num,int result_list[])
void SCAN(int list[], int disk_num,int result_list[])
void CSCAN(int list[], int disk_num,int result_list[])
Are user-defined functions

2, Highlights of this procedure

2.1 the maximum random track number can be modified

   due to the teacher's requirements, it is necessary to run the program by randomly inputting track number sequence in order to avoid the tediousness of manually inputting data each time. Therefore, a question arises: what should be the random track number range: is it [1100], [1200] or [1500]? Therefore, the maximum track number needs to be easily modified in the program.

                       . At this time, the minimum track number is 18, the maximum track number is 184, and the track number of the initial access is 100.
   if we choose to manually input the track number, we basically don't need to pay attention to the track number range, and the input number only needs to be within the [- 214748364824147483647] range of the defined int variable. However, if a random sequence is generated, a constant is required to control the maximum track number generated.
   ps: in fact, the minimum track number can also be saved with a constant. The implementation method is similar to the maximum track number, but generally starts from 1. Therefore, it is not considered to set another minimum track number constant.

2.1.1 how to generate track numbers randomly

   first, we should make it clear that the track number sequence of the disk scheduling algorithm is determined. Therefore, there can be no duplicate numbers in the input track number, so the random track number sequence we generate needs to ensure this, which is mainly realized by the following code (the generated results are stored in the list [] array).

int flag[MAX_DISK+1];init(flag,MAX_DISK+1);
			for (int i = 0; i < disk_num; i++)
			{
				int a = rand() % (MAX_DISK) + 1;//Generate disk_num random numbers from 1 to 200 
				if (flag[a]==-1)
				{
					list[i]=a; 
					flag[a]=1;
				}else
				{
					i=i-1;
				}				
			}

   the main idea is to open another array flag [], which is as large as the maximum track number, to store whether the number generated each time has been generated before. If it has been generated, you need to reduce i by 1 and regenerate the random number until enough disk is generated_ Num is a number.

2.1.2 use constants to define the maximum value of random track number

   I believe you have found through the above code that the program uses the method of defining macro constants to control the maximum random number generated to be 200, and defines max_ The code of disk constant is as follows:

#define MAX_DISK 200

  at the same time, if you want the range of random numbers, you just need to modify the following sentence

int a = rand() % (MAX_DISK) + 1;//Generate disk_num random numbers from 1 to 200 

    for example, if you want to modify the random number range to [50450], modify the code to:

#define MAX_DISK 400
int a = rand() % (MAX_DISK) + 50;//Generate disk_num 50 to 450 random numbers 

2.2 the track number of the initial access can be modified

   the four disk scheduling algorithms can share BEGIN_DISK constant to quickly modify the initial access track number. Take SSTF algorithm as an example:

#define BEGIN_DISK 100
void SSTF(int list[], int disk_num,int result_list[])
{	printf("----------SSTF----------\n");
    int temp_list[disk_num];
    init(temp_list, disk_num);
    for(int i=0;i<disk_num;i++)
    {
    	temp_list[i]=list[i]; 	
	}
	int last=BEGIN_DISK; //First look from BEGIN_DISK track start
	int flag[disk_num];//Defines an array that indicates whether tracks have been accessed 
	init(flag,disk_num); //Initialize the array and set it to - 1, indicating that it has not been accessed 
	for (int n=0;n<disk_num;n++)
	{	
		int distance=999;//Looking for distance
		int mindistance=999;
		int mint=0; 
		for(int t=0;t<disk_num;t++)
		{	
			
			if(flag[t]==-1)
			{
				distance=abs(last-list[t]);
				if(distance<mindistance)
				{
					mindistance=distance;
					mint=t;
				}
			}	
		} 
		result_list[n]=list[mint];
		last=list[mint];
		flag[mint]=1;//Flag track number has been accessed 
	}
    show_result(result_list,disk_num);
    printf("----------SSTF end----------\n");
}

  where begin_ The disk constant is used as the value of the initial last variable. If you want to change the initial track to 150, you only need to modify the following code:

#define BEGIN_DISK 150

3, Implementation process of four disk scheduling algorithms

3.1 overall structure of the procedure

  according to the code of mian() function, first describe the overall structure of the program. Paste the main() function again:

#include <stdio.h>
#include <stdlib.h>
#define MAX_DISK 200
#define BEGIN_DISK 100
int main()
{
    while (1)
    {
        int disk_num = 0;
        printf("Please enter the track number sequence length:\n");
        scanf("%d", &disk_num);
        int list[disk_num];
        init(list, disk_num);
        int result_list[disk_num];
        init(result_list, disk_num);
        int choice=0;
        printf("Please select: 1.Self input page data 2.Randomly generate page data\n");
        scanf("%d", &choice);
        if(choice==1)
        {
	        printf("Please enter the track number sequence:\n");
	        for (int i = 0; i < disk_num; i++)
	            scanf("%d", &list[i]);	
		}else if(choice==2)
		{	
			int flag[MAX_DISK+1];init(flag,MAX_DISK+1);
			for (int i = 0; i < disk_num; i++)
			{
				int a = rand() % (MAX_DISK) + 1;//Generate disk_num random numbers from 1 to 200 
				if (flag[a]==-1)
				{
					list[i]=a; 
					flag[a]=1;
				}else
				{
					i=i-1;
				}				
			}
			printf("Track number sequence is:\n");
			for(int i=0;i<disk_num;i++)
			{
				printf("%d ",list[i]);
				if((i+1)%10==0)
           		printf("\n");
			}
	        printf("\n");  
		}else
		{
			 printf("----------Illegal number entered, please enter 1 or 2----------\n\n");
			 continue;
		} 
        FCFS(list, disk_num, result_list);
        init(result_list, disk_num);
        SSTF(list, disk_num, result_list);
        init(result_list, disk_num);
        SCAN(list, disk_num, result_list);
        init(result_list, disk_num);
        CSCAN(list, disk_num, result_list);
        init(result_list, disk_num);
		printf("\n\n--------------------\n\n");
    }
}

Use the while(1) {} statement to make the program run circularly, which is convenient for re running.
Step 1. Let the user enter the length of the track number sequence.
Step 2. Let the user choose 1. Input the track number sequence by himself or 2. Generate the track number sequence randomly. After the track number sequence is stored in the list [] array, the list [] array is output and displayed.
Step3. Perform four disk scheduling algorithms and output the running results (call the show_result() function inside the four scheduling algorithms).
Note: you need to use the custom function void init(int list[], int num) to initialize the result after calling the disk scheduling algorithm each time_ List array to prevent the running result of the previous algorithm from affecting the next algorithm
The void init(int list[], int num) function is as follows:

void init(int list[], int num)
{
    for (int i = 0; i < num; i++)
        list[i] = -1;
}

void show_ The result (int list [], int Num) function is as follows:

void show_result(int list[], int num)
{	int last=BEGIN_DISK;
	double move=0;
	printf("       (Starting from 100 tracks)\n");
	printf("Next track number    Moving distance(Track number)\n");
	for(int i=0;i<num;i++)
		{
			printf("%10d%10d\n",list[i],abs(last-list[i]));
			move+=abs(last-list[i]);
			last=list[i];
		}
		move=move/num;
	    printf("Average seek length:%.1lf\n",move);  
}

3.2 FCFS first come first serve algorithm

void FCFS(int list[], int disk_num,int result_list[])
{
    printf("----------FCFS----------\n");
    for(int i=0;i<disk_num;i++)
    result_list[i]=list[i]; 
    show_result(result_list,disk_num);
    printf("----------FCFS end----------\n");
}

   this is the simplest. You only need to assign the input array directly to result_list[] array, then call show_ The result() function can output by the number of cells.

3.3 SSTF shortest seek time first algorithm

void SSTF(int list[], int disk_num,int result_list[])
{	printf("----------SSTF----------\n");
    int temp_list[disk_num];
    init(temp_list, disk_num);
    for(int i=0;i<disk_num;i++)
    {
    	temp_list[i]=list[i]; 	
	}
	int last=BEGIN_DISK; //First look from BEGIN_DISK track start
	int flag[disk_num];//Defines an array that indicates whether tracks have been accessed 
	init(flag,disk_num); //Initialize the array and set it to - 1, indicating that it has not been accessed 
	for (int n=0;n<disk_num;n++)
	{	
		int distance=999;//Looking for distance
		int mindistance=999;
		int mint=0; 
		for(int t=0;t<disk_num;t++)
		{	
			
			if(flag[t]==-1)
			{
				distance=abs(last-list[t]);
				if(distance<mindistance)
				{
					mindistance=distance;
					mint=t;
				}
			}	
		} 
		result_list[n]=list[mint];
		last=list[mint];
		flag[mint]=1;//Flag track number has been accessed 
	}
    show_result(result_list,disk_num);
    printf("----------SSTF end----------\n");
}

   the implementation idea of SSTF algorithm is also relatively clear. After each track number is selected, calculate the nearest track number.

3.4 SCAN algorithm

void SCAN(int list[], int disk_num,int result_list[])
{
   printf("----------SCAN----------\n");
    int temp_list[disk_num];
    init(temp_list, disk_num);
    for(int i=0;i<disk_num;i++)
    {
    	temp_list[i]=list[i]; 	
	}
	quicksort(temp_list,disk_num,0,disk_num-1);
	int index=disk_num-1;
	int last=BEGIN_DISK;//Find the track number closest to 100 
	for(int i=0;i<disk_num;i++)
	{
		if(temp_list[i]>last)
		{
			index=i;
			break;
		}
	}
	int fangxiang=1;//Direction variable, 1 is the increasing direction of track number, and 0 is the decreasing direction. First make from BEGIN_DISK number starts scanning in the increasing direction. 
	int flag[disk_num];//Defines an array that indicates whether tracks have been accessed 
	init(flag,disk_num); //Initialize the array and set it to - 1, indicating that it has not been accessed 
	int n=disk_num;
	int result_index=0;
	while(n)//If there are track numbers that have not been accessed 
	{	
		if(fangxiang)
		{
			while(index<=disk_num-1&&flag[index]==-1)
			{
				result_list[result_index]=temp_list[index];
				flag[index]=1;//Mark the track number that has been accessed 
				index++; 
				result_index++;
				n--;//Total number of track numbers minus 1 
			}
			if(index==disk_num)
			{
				fangxiang=0;//Set direction to 0 
				index--;//Prevent the next visit from crossing the border 
			}
		}else
		{
			while(index>=0&&flag[index]==-1)
			{
				result_list[result_index]=temp_list[index];
				flag[index]=1;//Mark the track number that has been accessed 
				index--;
				result_index++;
				n--;
			}
			index--;
		}	
	}   
    show_result(result_list,disk_num);
    printf("----------SCAN end----------\n");
}

   my implementation idea of SCAN algorithm is: first, use the passed in list [] array Quick sort Save to temp after_ List [] array, and then the moving direction of the magnetic head is controlled by the value (1 or 0) of the direction variable fangxiang to scan the track number sequence.
   it should be noted that another flag [] array needs to be opened to indicate whether the track number has been accessed. If the track number with the subscript index has been accessed, the value of flag[index] needs to be - 1.
   quick sort void quicksort(int num[],int count,int left,int right) functions are as follows:

void quicksort(int num[],int count,int left,int right)
{
    if (left >= right){
        
        return ;
    }
    int key = num[left];
    int lp = left;           //Left pointer
    int rp = right;          //Right pointer
    while (lp < rp) {
        if (num[rp] < key) {
            int temp = num[rp];
            for (int i = rp - 1; i >= lp; i--) {
                num[i + 1] = num[i];
            }
            num[lp] = temp;
            lp ++;
            rp ++;
        }
        rp --;
    }
    quicksort(num,count,left,lp - 1);
    quicksort(num,count,rp + 1,right);
}

3.5 CSCAN cyclic scanning algorithm

void CSCAN(int list[], int disk_num,int result_list[])
{printf("----------CSCAN----------\n");
    int temp_list[disk_num];
    init(temp_list, disk_num);
    for(int i=0;i<disk_num;i++)
    {
    	temp_list[i]=list[i]; 	
	}
	quicksort(temp_list,disk_num,0,disk_num-1);
	int index=disk_num-1;
	int last=BEGIN_DISK;//Find the track number closest to 100 
	for(int i=0;i<disk_num;i++)
	{
		if(temp_list[i]>last)
		{
			index=i;
			break;
		}
	}
	int flag[disk_num];//Defines an array that indicates whether tracks have been accessed 
	init(flag,disk_num); //Initialize the array and set it to - 1, indicating that it has not been accessed 
	int n=disk_num; 
	int result_index=0;//Store the subscript in the result array 
	while(n)//If there are track numbers that have not been accessed 
	{	
		while(index<=disk_num-1&&flag[index]==-1)
		{
			result_list[result_index]=temp_list[index];
			flag[index]=1;//Mark the track number that has been accessed 
			index++;
			result_index++;
			n--;//Total number of track numbers minus 1 
		}
		index=0;
	}   
    show_result(result_list,disk_num);
    printf("----------CSCAN end----------\n");
}

   the implementation idea of CSCAN is basically similar to that of SCAN, except that the moving direction control part of the magnetic head is when index > disk_ When num-1, you need to reset the index to 0.

4, Verify the correctness of the program

Note: this part needs to be verified by manually entering the specified track number sequence, rather than randomly generating the track number sequence.

4.1 FCFS verification

FCFS operation results Program FCFS running results

4.2 STTF verification

Teaching material SSTF operation results Program SSTF running results

4.3 SCAN verification

Textbook SCAN running results Program SCAN run results

4.4 CSCAN verification


Teaching material CSCAN operation results

Program CSCAN operation results

5, Full program download link

[operating system disk scheduling algorithm] OS experiment C language code to realize FCFS/SSTF/SCAN/CSCAN
The code has been updated. The new resource is still under review, which takes 2-4 days

6, Summary

I'm tired. I'll write it in a few days

7, Gossip

  the original intention of writing this blog is to recall the knowledge points in the future, and also to recall the afternoon of struggle in a few years.
  there are two best moments: first, when the program is written and the program runs without errors. Second, click the upload button to upload the blog successfully.
Finally, make complaints about csdn's online editor. When you write to 8k, it will become very Catton. This article will be about 1w2k words. The middle term is written in ppt card. At last, you can only copy and paste to write the content.
If there are any problems or improvements in the program, please leave a message in the comment area below. Thank you for seeing here. Thank you for your time.

Tags: C Operating System

Posted on Fri, 03 Dec 2021 15:12:18 -0500 by Archy