#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define GETPCH (Type) (type*) malloc (sizeof (type))
#define NULL 0
struct PCB {//define Process Control block PCB
Char name[10];
Char state;
int super;
int ntime; Ready time
int rtime; Run time
struct pcb* link; Queue
}*ready=null,*p;//creating an empty ready queue
typedef struct PCB PCB;
void sort ()//Establish a priority arrangement function for the process
{
PCB *first, *second; Two pointers for arranging
int insert=0; Insert
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)//The second one is not empty
{
if ((P->super) > (second->super))//If the insert process is larger than the current process priority, insert it in front of the current process
{
p->link=second;
first->link=p;
Second=null;
Insert=1;
}
else//Move pointer backwards
{
first=first->link;
second=second->link;
}
}//Insert process with lowest priority, insert to end of queue
if (insert==0) first->link=p;
}
}
void input ()//Build Process Control block function
{
int i,num;
printf ("\ n Please enter the process number:");
scanf ("%d", &num);
for (i=1;i<=num;i++)
{
printf ("\ n process number no.%d:\n", i);
p=getpch (PCB);
printf ("\ n input process name:");
scanf ("%s", p->name);
printf ("\ n Input process priority number:");
scanf ("%d", &p->super);
printf ("\ n Enter process run time:");
scanf ("%d", &p->ntime);
printf ("\ n");
p->rtime=0;p->state= ' W ';
p->link=null;
sort ();//Call the sort function
}
}
int space ()
{
int L = 0; pcb* Pr=ready;
while (pr!=null)
{
l++;
pr=pr->link;
}
return (L);
}//View how many ready processes are in the ready queue
void disp (PCB * PR)//Establish a process display function for displaying the current process
{
printf ("\ QName \ t state \ t super \ ndtime \ t runtime \ n");
printf ("%s\t", pr->name);
printf ("%c\t", pr->state);
printf ("%d\t", pr->super);
printf ("%d\t", pr->ntime);
printf ("%d\t", pr->rtime);
printf ("\ n");
}
void check ()//Establish process view function
{
pcb* PR;
printf ("\ n currently running process is:%s", p->name); Show current Running process
DISP (p);
Pr=ready;
printf ("\ nthe current Ready queue status is: \ n"); Show Ready queue Status
while (Pr!=null)//Ready queue no end, always output
{
DISP (PR);
pr=pr->link;
}
}
void Destroy ()//build process Undo function (Process run end, undo process)
{
printf ("\ n process [%s] completed. \ n", p->name);
Free (p);
}//Free space
void running ()//build process-ready function (process run time to, set ready state
{
(p->rtime) + +; Run time plus One
if (p->rtime==p->ntime)
Destroy (); Call the Destroy function
Else
{
(P->super)--; Priority minus One
P->state= ' W ';
Sort (); Call the Sort function
}
}
void main ()//main function
{
int Len, h=0;
Char ch;
Input ();
Len=space (); Space function View Captain
while ((len!=0) && (ready!=null))
{
Ch=getchar ();//Accept a character
h++;
printf ("\ n the execute number:%d \ n", h);
P=ready;
ready=p->link;
p->link=null;
P->state= ' R '; Wait for change to run
Check ();
Running ();
printf ("\ n Press any key to continue ...");
Ch=getchar ();
}
printf ("\ n \ nthe process has completed. \ n");
Ch=getchar ();
}
Highest-Priority scheduling algorithm