Process Dynamic Priority scheduling

Source: Internet
Author: User

1 /*******************************************2 *3 * FILE:PRO.C4 * Describe: Operating system process scheduling, dynamic priority algorithm5 * Author: Ah q6 * iime:2016.11.197 *8 *******************************************/9#include <stdio.h>Ten #defineP 5 One #defineprintf (proprety) printf ("%s\t", proprety) A structPCB {//Define Process Structure -     intpid//Process ID -     structPCB *next;//point to Next process the     intTime//the time required for the process to execute -     intPriority//Process Priority -     intState//the status of the process execution, only 0, 1 states. 0 Not executed, 1 executed - }; + structPCB pro[p]= {//Initialize 5 processes -{0,1,6,3,0}, +{1,2,4,4,0}, A{2,3,4,3,0}, at{3,4,4,2,0}, -{4,0,1,0,0}, - }; -  - /******************************************* - * in * Function: Shows the status of all Processes - * to *******************************************/ + voiddisplay () { -     intI=0, property=5; thePrintF ("\npid"); *      for(i=0; i<p; i++) { $printf"%d\t", pro[i].pid);Panax Notoginseng     } -PrintF ("\nnext"); the      for(i=0; i<p; i++) { +printf"%d\t", pro[i].next); A     } thePrintF ("\ntime"); +      for(i=0; i<p; i++) { -printf"%d\t", pro[i].time); $     } $PrintF ("\npri"); -      for(i=0; i<p; i++) { -printf"%d\t", pro[i].priority); the     } -PrintF ("\nstate");Wuyi      for(i=0; i<p; i++) { theprintf"%d\t", pro[i].state); -     } Wuprintf"\ n"); - } About  $ /******************************************* - * - * Function: Sort the structure, descending by priority * Time Ascending - * A *******************************************/ + intcmpConst void*a,Const void*b) { the     structPCB *aa= (structPCB *) A; -     structPCB *bb= (structPCB *) b; $  the     //returns the status directly if the process execution finishes the     if(aa->time==0|| aa->state==1)return 1; the     Else if(bb->time==0|| bb->state==1)return-1; the  -     if(bb->priority!=aa->Priority ) in         return(bb->priority-aa->Priority ); the     Else the         return(aa->time-bb->Time ); About } the  the /******************************************* the * + * Function: Process sequencing requires sub-function cmp () - * the *******************************************/Bayi voidReSort () { theQsort (Pro,p,sizeof(pro[0]), CMP); the } -  - /******************************************* the * the * Function: Check if there are any programs that have not finished executing the * the *******************************************/ - intCheck () { the     intI=0; the      for(i=0; i<p; i++) { the         if(!pro[i].state)return 1;94     } the     return 0; the } the 98 /******************************************* About * - * Function: Modify pointer to point101 *102 *******************************************/103 voidRepoint () {104     intI=0; the      for(; i<p; i++) {106         if(pro[i].state&&i>0) {107pro[i-1].next=0;108         }109         if(i< (P-1)) thepro[i].next=pro[i+1].pid;111     } the }113  the intMain () { the     intf=0, i=1; theprintf"the initial state is:");117 display ();118      while(check ()) {//after each execution, test if there are still processes not finished119printf"after the first%d run:", i++); -         //Pro[0] is the first process, which is considered to be a process executed in the cup. Each execution time is reduced by one, and priority is reduced by one. 121pro[0].time-=1;//perform a process execution time minus one122pro[0].priority-=1;//dynamic priority, minus one priority per execution123 124         if(pro[0].time==0) pro[0].state=1, pro[0].priority=-1, pro[0].next=0;//If the process finishes, and the execution time is 0, the status is 1, the priority is adjusted, and the pointer is modulated to 0 theReSort ();//Reflow Process126Repoint ();//Rearrange pointers127Display ();//Output -     }129printf"The process is complete. \ n"); the     return 0;131}

Process Dynamic Priority scheduling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.