simple cyclic rotation scheduling algorithm
principle
when When the CPU is idle, select the first element of the ready queue to give time slices. When the time slice of the process is exhausted, the CPU control is freed, the end of the queue is entered, and the CPU control is given to the next first element in the ready queue, as the principle.
Implementation Flowchart
Process Dispatch Source code
#include"stdafx.h"#include<queue>#include<math.h>#include<vector>#include<iostream>#include<iomanip>using namespacestd;/*data structure of the process*/structpcb{intID;//Process ID DoubleIn_time;//entry time of the process DoubleRes_time;//response time of the process DoubleL_time;//time remaining for the process};/*Process scheduling Functions*//*Input: Process queue PRS, time slice timeslice*//*output: Process status Table*/ voidProcessscheduling (Queue<pcb>prs,Doubletimeslice) {cout<<Left ; Queue<PCB>ex_prs;//ready queue, the processes in that queue are waiting to be executedEx_prs.push (Prs.front ());//The first process first enters the ready queuePrs.pop ();//to exit the process queue for the first process DoubleSlice_start;//time slice start time DoubleSlice_end =0;//time slice End timecout <<"----------------------------------Process State table------------------------------------"<<Endl; cout<<SETW ( the) <<"Process ID"<<"|"<< SETW ( the) <<"Arrival Time"<<"|"<<SETW ( the) <<"Total response Time"<<"|"<< SETW ( the) <<"time slice start time"<<"|"<< SETW ( the) <<"time slice End time"<<"|"<<Endl; cout<<"--------------------------------------------------------------------------------"<<Endl; while(!prs.empty () | |!Ex_prs.empty ()) {Slice_start= Slice_end;//update time slice start and end timeSlice_end + = TimeSlice;//Add a time slice each time while(!prs.empty ())//The process that will be able to enter the ready queue at the time the process consumes CPU can join the ready queue before each process executes { if(Prs.front (). In_time > Slice_end) Break; Ex_prs.push (Prs.front ()); Prs.pop (); if(ex_prs.size () = =1) {Slice_start=Slice_end; Slice_end+=TimeSlice; } } //continue while loop if ready queue is empty if(Ex_prs.empty ())Continue; //If the remaining time is less than or equal to the time slice if(Ex_prs.front (). L_time <=timeslice) {Ex_prs.front (). L_time=0;//Place The remaining time of the process at 0 (optional)cout << SETW ( the) << Ex_prs.front (). ID <<"|"<< SETW ( the) << Ex_prs.front (). In_time <<"|"<< SETW ( the) << Ex_prs.front (). Res_time <<"|"<< SETW ( the) << slice_start<<"|"<< SETW ( the) << slice_end<<"|"<<Endl; Ex_prs.pop (); } Else{Ex_prs.front (). L_time-= TimeSlice;//reduce the remaining time of a process by one time slicecout << SETW ( the) << Ex_prs.front (). ID <<"|"<< SETW ( the) << Ex_prs.front (). In_time <<"|"<< SETW ( the) << Ex_prs.front (). Res_time <<"|"<< SETW ( the) << Slice_start <<"|"<< SETW ( the) << Slice_end <<"|"<<Endl; //Put the team's first process at the end of the teamPCB TMP_PR =Ex_prs.front (); Ex_prs.pop (); Ex_prs.push (TMP_PR); }} cout<<"--------------------------------------------------------------------------------"<<Endl;}intMain () {queue<PCB>PRS; //Total response Time DoubleAllres_time =0; //time slices DoubleTimeSlice =0; PCB PR1, PR2, PR3; PR1= {1,0,Ten,Ten }; PR2= {2,1,5,5 }; PR3= {3,2,5,5 }; Allres_time+ = Pr1.res_time + Pr2.res_time +Pr3.res_time; TimeSlice= Allres_time/3; Prs.push (PR1); Prs.push (PR2); Prs.push (PR3); cout<<"the time slices are:"<< TimeSlice <<Endl; Processscheduling (PRS, TimeSlice); cout<<"Add a process? (y/n):"; Chari; CIN>>i; while(I-'y'==0) {PCB PR; cout<<"Please enter the process ID:"; CIN>>pr.id; cout<<"Please enter the process arrival time:"; CIN>>Pr.in_time; cout<<"Please enter the process response time:"; CIN>>Pr.res_time; cout<<"Please enter the time remaining for the process:"; CIN>>Pr.l_time; Prs.push (PR); Allres_time+=Pr.res_time; TimeSlice= Allres_time/prs.size (); cout<<"the time slices are:"<< TimeSlice <<Endl; Processscheduling (PRS, TimeSlice); cout<<"continue adding processes? (y/n):"; CIN>>i; } return 0;}
Results
Compiling the program
Run the program
Add a process
Continue adding a process
Operating Environment: Ubuntu
Related questions:
①c++ output a table
Workaround: First set the left alignment with Cout<<left and then set the text width after setw () , such as: cout << SETW (<< "Miles"), set the following miles to a width of nine letters.
Process scheduling for analog processors-a simple cyclic rotation scheduling algorithm