Design a processor scheduling program based on priority scheduling algorithm.

(operating system) design a processor scheduling program according to the priority number scheduling algorithm. ...
(operating system) design a processor scheduling program according to the priority number scheduling algorithm.

(Tip: the code is for reference only. If there are any errors, please correct them. The comments are just written casually and record the thinking process. If you can't understand your own understanding, you csdn, don't ask others. In order to prevent the trouble of changing the code, the blogger directly made (8) and can modify it as needed.)

(1) It is assumed that the system has five processes, and each process is represented by a process control block PCB. The format of the process control block is:

Among them, process name - as the identification of the process, it is assumed that the process names of the five processes are P1, P2, P3, P4 and P5 respectively.
Pointer - connect the five processes into a queue according to the size of the priority number, use the pointer to indicate the first address of the process control block of the next process, and the pointer in the last process is "0".
Required run time - the number of unit times a process is assumed to need to run.
Priority number - the priority number given to the process. When scheduling, the process with a large priority number is always selected to execute first.
Status - two statuses can be assumed, "ready 8080" status and "end" status. The initial state of the five processes is "ready", represented by "R". When a process runs, its state is "end", represented by "E".
(2) Before running the processor scheduler you designed, arbitrarily determine its "priority number" and "required running time" for each process. For the convenience of inspection, the priority number and running time of this experiment adopt the values in the table below.
(3) For scheduling convenience, five processes are queued according to the given priority number from large to small. Use a unit to indicate the queue head process and a pointer to indicate the connection of the queue. Example:
Team leader logo

(4) Processor scheduling always selects the first process to run. By dynamically changing the priority number, the priority number will be reduced by "1" every time the process runs. Since this experiment simulates processor scheduling, the selected process is not actually started and run, but executed:
Priority number - 1
Required run time - 1
To simulate a run of the process.
It should be noted that in the actual system, when a process is selected to run, the site of the process must be restored and let it occupy the processor to run until a waiting event occurs or the run ends. These jobs are saved here.
(5) After the process runs once, if the required running time is  0, it will be added to the queue (inserted according to the priority number and set the queue head flag); If the required run time = 0, change its status to "end" (E) and exit the queue.
(6) If the process queue in the "ready" state is not empty, repeat the steps (4) and (5) above until all processes become in the "end" state.
(7) There should be display or print statements in the designed program, which can display or print the process name of each selected process and the change of process queue after running once.
Select as part
(8) Arbitrarily determine a set of "priority number" and "required running time" for the five processes, start the designed processor scheduler, display or print the process name of the selected process and the dynamic change process of the process control block.

No more nonsense. Go directly to the code. If you want to know whether the result is right, run it yourself.

#include <iostream> #include <queue> using namespace std; #define OK 1 #define ERROR 0 typedef int Status; typedef string PROC_STATE; int length = 5; typedef struct _proc {/*Process structure*/ //char henu[1912];/* Process name*/ string name;/*Process name*/ int run_time; /*Total required running time*/ int alloc_time; /*Elapsed time*/ int rel_time;/*Additional running time required*/ int pri;/*Priority: not used in time slice scheduling*/ PROC_STATE state;/*Process status*/ struct _proc* next; friend bool operator<(const _proc& a, const _proc& b) { return a.pri < b.pri; }//Sorting operation, from large to small according to the priority, forms a small root heap } PROC, * PROCPtr; typedef struct { PROCPtr front; PROCPtr rear; } LinkQueue; Status InitQueue(LinkQueue& Q) { //The chain queue is initialized to construct an empty queue Q Q.front = Q.rear = new PROC; Q.front->next = NULL; cout << "->Initialization creation chain team completed!" << endl; return OK; } Status EnQueue(LinkQueue& Q, string name, int run_time, int alloc_time, int rel_time, int pri, PROC_STATE state) { //Join the team PROCPtr p; p = new PROC; p->name = name; p->run_time = run_time; p->alloc_time = alloc_time; p->rel_time = rel_time; p->pri = pri; p->state = state; p->next = NULL; Q.rear->next = p; Q.rear = p; cout << "->Data element enlistment complete!" << endl; return OK; } void SortQueue(LinkQueue& Q) { priority_queue<_proc>M;//The queue is sorted from large to small to form a small root heap PROC s; PROCPtr t = Q.front->next; do {//First, take out the processes in Ready status and sort them according to priority if (t->state == "End") { break; } s.name = t->name; s.run_time = t->run_time; s.alloc_time = t->alloc_time; s.rel_time = t->rel_time; s.pri = t->pri; s.state = t->state; M.push(s); t = t->next; } while (t != NULL); t = Q.front->next; while (!M.empty()) {//Put the sorted processes into the queue t->name = M.top().name; t->run_time = M.top().run_time; t->alloc_time = M.top().alloc_time; t->rel_time = M.top().rel_time; t->pri = M.top().pri; t->state = M.top().state; M.pop(); t = t->next; }//After this process, the pointer order of the original process structure has not changed, but the value of the structure has been replaced. } void PrintQueue(LinkQueue Q) {//Printout if (Q.front == Q.rear) { cout << "->The queue is currently empty and there is no data element printout!" << endl; } else { PROCPtr p; p = Q.front->next; cout << "<--------------------------------------------->" << endl; cout << "Process name\t" << "Total time\t" << "function\t" << "surplus\t" << "priority\t" << "state\t" << endl; do { cout << p->name << "\t" << p->run_time << "\t" << p->alloc_time << "\t" << p->rel_time << "\t" << p->pri << "\t" << p->state << "\t" << endl; p = p->next; } while (p != NULL); } } void Running(LinkQueue& Q) {//Start running PrintQueue(Q); SortQueue(Q); while (Q.front->next->state != "End") { PROCPtr t = Q.front->next; t->pri--; t->alloc_time++; t->rel_time = t->run_time - t->alloc_time; if (t->rel_time == 0) {//When the process ends, the process is placed at the end of the queue t->state = "End"; Q.rear->next = t; Q.rear = t; Q.front->next = t->next; t->next = NULL; } SortQueue(Q); PrintQueue(Q); } } int main() { LinkQueue Q; InitQueue(Q); cout << "Please input five groups of program control blocks in turn PCB: (Process name-Total time-priority)" << endl; string n;/*Process name*/ int rut = 0; /*Total required running time*/ int alt = 0; /*Elapsed time*/ int ret = 0;/*Additional running time required*/ int pri = 0;/*Priority: not used in time slice scheduling*/ PROC_STATE state = "Ready";/*Process status*/ while (length--) { cin >> n >> rut >> pri; ret = rut; EnQueue(Q, n, rut, alt, ret, pri, state); } Running(Q); return 0; } /*Here are the test data P1 2 1 P2 3 5 P3 1 3 P4 2 4 P5 4 2 */

17 November 2021, 10:07 | Views: 2109

Add new comment

For adding a comment, please log in
or create account

0 comments