Computer operating system experiment: process scheduling experiment

Time:2024-1-26

catalogs

preamble

II. Purpose of the experiment

III. Experimental requirements

IV. Experimental principles

V. Experimental process

VI. Code details

summarize


preamble

Computer operating system is the core software that manages computer hardware and software resources, and it is responsible for providing users with a friendly, efficient, and secure environment to use. Process scheduling is an important function of the operating system, which determines the execution order and time of processes on the processor, thus affecting the system performance and user experience. This experiment aims to deepen the understanding and mastery of operating system principles and design by simulating different process scheduling algorithms and comparing their advantages and disadvantages.


I. Development language and experimental platform

C++/JAVA

Turbo C / Microsoft Visual Studio 6.0 / Microsoft Visual Studio .NET 2010

In this article the language used is c (?). The platform used is devc++.

II. Purpose of the experiment

(1) Deepen understanding of the concept of process and process scheduling algorithms;

(2) On the basis of understanding and mastering the process scheduling algorithm, prepare a general program for the process scheduling algorithm, display the debugging results on the computer screen, and test the consistency of the machine calculation and the written calculation.

III. Experimental requirements

(1) Understand process scheduling;

(2) Understand the principles of scheduling using process scheduling algorithms;

(3) Algorithmic simulation using some programming language.

IV. Experimental principles

  • Example: design a process scheduling algorithm with N processes.

Process scheduling algorithm: the highest priority scheduling algorithm is used (i.e., the processor is assigned to the process with the highest priority).

Each process is represented by a process control block (PCB). The PCB can contain the following information: process name, priority, arrival time, time needed to run, CPU time used, process status, and so on.

The priority of a process and the time it needs to run can be specified artificially in advance (it can also be generated by a random number). The arrival time of the process is the time of the input of the process. The running time of a process is calculated in time slices.

The state of each process can be one of three states: Ready W (Wait), Running R (Run), or Finished F (Finish). A ready process can only run for one time slice after it has been given CPU. This is represented by the occupied CPU time plus one.

If after running a time slice, the occupied CPU time of the process has reached the required running time, then the process is withdrawn. If after running a time slice, the occupied CPU time of the process has not reached the required running time, that is, the process still needs to continue to run, at this time, you should reduce the priority of the process by 1 (that is, to lower one level), and then it is inserted into the ready queue to wait for the CPU.

Each time the scheduler is performed it prints the running process, the ready queue, and the PCB of each process for inspection.

Repeat the above process until all the desired processes are completed.

analyze:

Using a fixed queue combined with static and dynamic prioritization, each priority is0~0xFF,And with small numbers as high priority and large numbers as low priority, each time a loop is used to get the highest priority process and execute it, then set its dynamic priority to the lowest and raise the dynamic priority of the other processes so that each process has a chance to run. The priority and runtime of the processes are generated by random numbers.

Computer operating system experiment: process scheduling experiment

V. Experimental process

The code is as follows:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

/* Constants and state definitions */
#define        PRO_NUM        0x05
#define        MAX_TIME    0xFF
   /*Status macros**/
#define        WAIT        0x01
#define        RUN            0x02
#define        FINISH        0x03

#define        ID_ERROR    0x10

#define        MIN_PRIOR    0xFF //255
#define        MAX_PRIOR    0x00 0


typedef unsigned int    Uint32;

/*Process PCB*/
struct PCB_Info
{
   Uint32    s_id;
   Uint32    s_static_prior;
   Uint32    s_dynamic_prior;
   Uint32    s_start_time;
   Uint32    s_need_time;
   Uint32    s_used_time;
   Uint32    s_state;
};

/* Process queue */
PCB_Info    g_queue[5];
Uint32        g_time = 0;
/* Simulation of process execution functions */
void Simulator();
/* Initialize 5 process functions */
void Init_Process();
/* Initialize process queue function */
void Init_Queue();
/* Create process function */
Uint32 Create_Process(Uint32 pri,Uint32 needtime);
/* System runtime functions */
void Run_Process();
/* Get highest priority process ID function */
Uint32 Get_PriProcess();
/* Process time-slice execution function */
void    Work_Process(Uint32 id);
/* Change process state and priority functions */
void    Change_Process(Uint32 id);
/* Print process status function */
void    Print_State();
/* End system function */
void End_Process();
/* Entry function */
int main( int argc, char *argv[ ])
{
   Simulator();
   return 0;
}
void Simulator()
{
   Init_Process();
   Run_Process();
   End_Process();
}

void Init_Process()
{
   int i;
   Uint32 id;
   srand( (unsigned)time( NULL ) );
   Init_Queue();
   for(i=0;i<PRO_NUM;++i)
   {
       /* Modify the range of random numbers here, suggesting that the priority take a value between 0 and 4, and that the total process working time be between 1 and 10 */
       id=Create_Process(rand()%5, 1+rand()%10);
       if(id!=ID_ERROR)
       {
           printf("**********************************\n");
           printf("Creating process successfully\n");.
           printf("The process ID number is: %d\n",id);
           printf("The static priority of the process is:%d\n",g_queue[id].s_static_prior);;
           printf("The dynamic priority of the process is:%d\n",g_queue[id].s_dynamic_prior);;
           printf("The arrival time of the process is: %d\n",g_queue[id].s_start_time);
           printf("The process takes time: %d\n",g_queue[id].s_need_time);
           printf("Process has used CPU time of:%d\n",g_queue[id].s_used_time);;
           printf("The state of the process is: %d\n",g_queue[id].s_state);
           printf("\n");
       }
       else
       {
           printf("Failed to create process\n");;
       }
   }
}

void Init_Queue()
{
   int i;
   for(i=0;i<PRO_NUM;++i)
   {
       g_queue[i].s_id=i;
       g_queue[i].s_dynamic_prior=MIN_PRIOR; //255
       g_queue[i].s_need_time=0;
       g_queue[i].s_start_time=0;
       g_queue[i].s_static_prior=MIN_PRIOR;
       g_queue[i].s_used_time=0;
       g_queue[i].s_state=FINISH;
//g_queue[i].s_state=Sart;//there is a mistake here, here Sart is not defined, according to the context, here it should be trying to set the state of the process to START or WAIT, according to the macro definition above, change here to WAIT
   }
}

Uint32 Create_Process(Uint32 pri,Uint32 needtime)
{
   int i=0;
   Uint32 id=ID_ERROR;
   for(i=0;i<PRO_NUM;++i)
   {
       if(g_queue[i].s_state ==FINISH)
       {
           id=g_queue[i].s_id;
           g_queue[i].s_dynamic_prior=MIN_PRIOR;
           g_queue[i].s_need_time=needtime;
           g_queue[i].s_start_time=g_time;
           g_queue[i].s_state=WAIT;
           g_queue[i].s_static_prior=pri;
           g_queue[i].s_used_time=0x0;
           break;
       }
   }
   return id;
}

void Run_Process()
{
   Uint32 id;    
   while((id=Get_PriProcess())!=ID_ERROR)
   {
       Work_Process(id);
       Change_Process(id);
   }
}

void    Print_State()
{
   int i;
   printf("Time Process ID\t Status Used Time Required Start Time Static Priority Acting Priority\n");;
   for(i=0;i<PRO_NUM;++i)
   {        
       printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",g_time,g_queue[i].s_id,g_queue[i].s_state,g_queue[i].s_used_time,g_queue[i].s_need_time,
           g_queue[i].s_start_time,g_queue[i].s_static_prior,g_queue[i].s_dynamic_prior);
   }
}

Uint32 Get_PriProcess()
{
   Uint32 id=ID_ERROR;
   int  i, prev_id=ID_ERROR;
   Uint32 prior=MIN_PRIOR*2, temp_prior;
   for(i=0;i<PRO_NUM;++i)
   {
       if(g_queue[i].s_state!=FINISH)
       {
           temp_prior=g_queue[i].s_dynamic_prior+g_queue[i].s_static_prior;
           if(temp_prior<=prior)
           {
               id=i;
               prior=temp_prior;
           }
       }
   }
   return id;
}

void    Work_Process(Uint32 id)
{
   ++g_time;
   g_queue[id].s_state=RUN;
   ++g_queue[id].s_used_time;
   Print_State();
}

void    Change_Process(Uint32 id)
{
   int i;
   if(g_queue[id].s_need_time==g_queue[id].s_used_time)
   {
       g_queue[id].s_state=FINISH; 
   }
   else
   {
       g_queue[id].s_dynamic_prior=MIN_PRIOR;
       g_queue[id].s_state=WAIT;
       
   }
   for(i=0;i<PRO_NUM;++i)
   {
       if((i!=id)&&(g_queue[i].s_state!=FINISH))
       {
           g_queue[i].s_dynamic_prior >0 ?--g_queue[i].s_dynamic_prior:g_queue[i].s_dynamic_prior=0;
       }
   }
}


void End_Process()
{
   printf("End state of all processes:\n");;
   Print_State();
   printf("All processes have ended! \n").
}

VI. Code details

This is a process scheduler in C. Now explain the code:

This code is a simple process scheduler that uses a priority scheduling algorithm to simulate the execution of processes. The program defines a number of constants and states, including the number of processes, maximum time, wait state, run state, and completion state. It also defines a PCB_Info structure to represent the PCB (Process Control Block) of the process, which contains the process ID, static priority, dynamic priority, start time, time needed, time used, and status.

The code also defines a global variable g_queue, which is used to represent the process queue, and a global variable g_time, which is used to represent the current time. In addition, a number of function prototypes are defined, including the simulation of the process execution function Simulator (), initialize 5 process function Init_Process (), initialize the process queue function Init_Queue (), create the process function Create_Process (), the system run function Run_Process (), get the highest Priority process ID function Get_PriProcess (), process time slice execution function Work_Process (), change the process state and priority function Change_Process (), print the process state function Print_State () and end the system function End_Process ().

In the main() function, the Simulator() function is called to simulate the operation of the entire system. the Simulator() function calls the Init_Process() function to initialize the five processes in turn, the Run_Process() function to run the system, and the End_Process() function is called at the end to end the system.

The Init_Process() function is used to initialize 5 processes. In this function, the Init_Queue() function is called first to initialize the process queue and then a for loop is used to create 5 processes. In the loop, the rand() function is used to generate a random number as a parameter for the priority and time needed to create the processes. Then the Create_Process() function is called to create the processes and based on the return value it determines whether the creation was successful or not. If the creation is successful, the information about the process is printed; otherwise, the information about the creation failure is printed.

The Init_Queue() function is used to initialize the process queue. In this function, a for loop is used to iterate through each element of the process queue and initialize it. Initialization includes setting the process ID, dynamic priority, time needed, start time, static priority, time used, and status.

The Create_Process() function is used to create a process. In this function, the process queue is first traversed to find a process with the status FINISH. If it is found, it assigns its ID to the id variable and initializes it, including setting the dynamic priority, time needed, start time, status, static priority, and time used. Finally the value of the id variable is returned.

The Run_Process() function is used to run the system. In this function, the Get_PriProcess() function is called first to get the highest priority process ID. if the return value is not equal to ID_ERROR, the Work_Process() function is called to execute the process, and the Change_Process() function is called to change the state and priority of the process. The loop then continues until the Get_PriProcess() function returns ID_ERROR.

The Get_PriProcess() function is used to get the highest priority process ID. in this function, an id variable is first defined and initialized to ID_ERROR, then a for loop is used to iterate through each element of the process queue. For each element, if its state is not FINISH, its priority (static priority + dynamic priority) is calculated and compared to the current highest priority. If its priority is less than or equal to the current highest priority, update the id variable and the current highest priority. Finally the value of the id variable is returned.

The Work_Process() function is used to execute the process. In this function, first the global variable g_time is added to 1, then the state of the specified process is set to RUN and its used time is added to 1. Finally the Print_State() function is called to print the process state.

The Change_Process() function is used to change the process state and priority. In this function, it first determines whether the specified process has completed (i.e., the time used is equal to the time needed). If it has completed, its state is set to FINISH; otherwise, its dynamic priority is set to MIN_PRIOR and its state is set to WAIT. then a for loop is used to iterate through the other elements of the process queue and update their dynamic priorities.

The End_Process() function is used to end the system. In this function, it first prints out the information about the end state of all the processes and then calls the Print_State() function to print the process state. Finally the information that all processes have ended is printed.

The effect is as follows:

Computer operating system experiment: process scheduling experiment

Computer operating system experiment: process scheduling experiment

Computer operating system experiment: process scheduling experiment

Above.


summarize

This experiment simulates the process and effect of priority scheduling algorithm by writing a process scheduling program in C language. The program defines the PCB structure of the process, which contains information such as process ID, static priority, dynamic priority, start time, required time, used time and status. The program also defines functions to initialize the process queue, create the process, run the system, get the highest priority process, execute the process, change the process status and priority, print the process status and end the system. The program uses random numbers to generate the priority and time needed for the processes and uses a loop to traverse the process queue, find the highest priority process and execute it, then lower its dynamic priority and raise the dynamic priorities of the other processes to ensure that every process has a chance to run. The program prints out the current time and the status of all processes after each execution of a process for observation and inspection. The program prints out the final result and ends the system after all processes have finished.

Through this experiment, it deepens the understanding and mastery of the process scheduling algorithm, familiarizes with the programming skills and debugging methods of C language, and improves the ability to analyze and solve problems.

Recommended Today

DML statements in SQL

preamble Previously we have explained DDL statements in SQL statements. Today we will continue with the DML statement of SQL. DML is the Data Manipulation Language.Used to add, delete, and change data operations on the tables in the library.。 1. Add data to the specified field(INSERT) 2. Modify data(UPDATE) 3. Delete data(DELETE) catalogs preamble I. […]