//Priority scheduling Algorithm#include <stdio.h>#include<stdlib.h>#include<conio.h>#defineGETPCH (Type) (type*) malloc (sizeof (type))#defineNULL 0structPCB {/*define Process Control block PCB*/ Charname[Ten]; CharState ; intSuper; intNtime; intRtime; structpcb*link; }*ready=null,*p; typedefstructPCB PCB;intnum=0;voidSort ()/*Establish a priority arrangement function for the process*/{PCB*first, *second; intinsert=0; if((ready==null) | | ((P->super) > (ready->super)))/*highest priority, insert team head*/{p->link=Ready ; Ready=p; } Else /*process comparison priority, inserted in the appropriate location*/{ First=Ready ; Second=first->link; while(second!=NULL) { if((P->super) > (second->super))/*If the insert process has a higher precedence than the current process,*/ { /*insert in front of current process*/P->link=second; First->link=p; Second=NULL; Insert=1; } Else /*insert process with lowest priority number, insert to end of queue*/{ First=first->link; Second=second->link; } } if(insert==0) first->link=p; }} intspace () {intL=0; PCB* pr=Ready ; while(pr!=NULL) {L++; PR=pr->link; } return(l);} voidDISP (PCB * PR)/*Establish a process display function to display the current process*/{printf ("\n\t process name \ t status \ t priority number \ t requires run time \ t has run time \ n"); printf ("\t%s",pr->name); printf ("\t%c",pr->State ); printf ("\t%d",pr->super); printf ("\t%d",pr->ntime); printf ("\t\t%d",pr->rtime); printf ("\ n"); } voidCheck ()/*Create a Process view function*/{PCB*PR; printf ("\n\t**** The process currently running is: \ n");/*Show current running process*/disp (p); PR=Ready ; printf ("\n\t**** The current Ready queue status is: \ n");/*Show ready queue status*/ while(pr!=NULL) {disp (PR); PR=pr->link; } } voidDestroy ()/*build process undo function (Process run end, undo process)*/{printf ("\n\t Process [%s] completed. \ n",p->name); Free(P);} voidRunning ()/*establish a process-ready function (process run time, ready state*/{(P->rtime) + +; if(p->rtime==p->Ntime) Destroy (); /*Call the Destroy function*/ Else { if(p->super>0) (P->super)--; P->state='W'; Sort (); /*Call the Sort function*/ } } //manual input of simulation functionsvoidManuallyenter () {inti; System ("CLS");/*Clear Screen*/printf ("\n\t Please enter the number of processes:"); scanf ("%d",&num); printf ("\ n"); for(i=1; i<=num;i++) {printf ("\ t%d processes: \ n", i); P=getpch (PCB); printf ("\ t Enter the process name:"); scanf ("%s",p->name); printf ("\ t Input process priority number:"); scanf ("%d",&p->super); printf ("\ t Enter the process run time:"); scanf ("%d",&p->ntime); printf ("\ n"); P->rtime=0; P->state='W'; P->link=NULL; Sort (); /*Call the Sort function*/ } }//Read FilevoidReadFile () {//Read FileFILE *FP = fopen ("Pcb.txt","R"); if(fp = =NULL) {printf ("File Open error!\n"); Exit (0); } while(!feof (FP) &&fgetc (fp)! =EOF) {fseek (FP),-1L, Seek_cur); FSCANF (FP,"%s%d%d",&p->name,&p->super,&p->ntime); Num++; } fclose (FP);}//Select how data is obtainedvoidDataAccess () {intK; printf ("\n\n\t**************************************\n"); printf ("\t\t1. Calling text to write data \ n"); printf ("\t\t2. Invoke manual input of analog data \ n"); printf ("\t**************************************\n\n\n"); printf ("\ t Please select a menu item:"); scanf ("%d",&k); Switch(k) { Case 1: ReadFile (); Break; Case 2: Manuallyenter (); Break; default: printf ("\n\n\t** Please output 1 or 2 to select"); DataAccess (); Break; }} voidoutput () {intLen,h=0; Charch; Len=space (); while((len!=0) && (ready!=NULL)) {CH=GetChar (); H++; printf ("\ t-----------------------------------------------------\ n"); printf ("\ t is now running%d times: \ n", h); P=Ready ; Ready=p->link; P->link=NULL; P->state='R'; Check (); Running (); printf ("\n\t Press any key to continue ... \ n"); } }intMain ()/*Main function*/{dataaccess (); Output (); printf ("The \n\n\t process is complete. \ n"); }
Operating system 3.0