#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#define RUNNING 0
#define READY 1
#define WAIT 2
typedef struct PCB
{
The identification number of the int pid;//process
int priority;//The priority of the process, the greater the value, the higher the priority level
int pstatus;//The state of the process
struct PCB * next;//pointer to next process
}PCB;
PCB * Create (int n)
{
A pointer to the head node, cur represents a pointer to the current junction
PCB * head,* temp,* cur;
int i;
head= (PCB *) malloc (sizeof (PCB));//Allocate space
if (head==null)//allocation failed
{
printf ("ERROR");
Exit (1);
}
cur=head;head->next=null;//head Node
for (i=0;i<n;i++)
{
temp= (PCB *) malloc (sizeof (PCB));
if (!temp)
{
printf ("ERROR");
Exit (1);
}
temp->pid=i+1; The identification number of the initialization process
When the temp->pstatus=ready;//process has just been created, initialize its state to "ready state"
printf ("\ n Please enter the priority level for process%d:", temp->pid);
scanf ("%d", &temp->priority);//user Input process priority and initialize, write to the corresponding PCB table
temp->next=null;
cur->next=temp;
cur=cur->next;
}
Return head;//returns the head pointer pointing to the head node.
}
void Insert (PCB * HEAD,PCB * cur)
{
if (cur==null)
Return
for (; head->next!=null;head=head->next);
cur->next=null;
head->next=cur;
}
PCB * Readytorun (PCB * head)
{
PCB * temp1=head,* temp=head->next;
if (head->next==null)
{
printf ("The Ready is null!");
Temp=null;
}
for (; head->next!=null;head=head->next)
{
if (Temp->priority < head->next->priority)//The current priority is less than the next-node priority
{
Temp1=head;
Temp=head->next;//temp record the PCB of the selected process
}
}
if (temp!=null)
temp1->next=temp->next;//removes the PCB from the selected process from the PCB link list of the Ready process
Return temp;//returns the node.
}
void Blocktoready (PCB * READY,PCB * wait,int j)
{
PCB * temp1=wait,* temp=wait->next;
if (wait->next==null)
{
printf ("The Wait is null!");
}
for (; wait->next!=null;wait=wait->next)//To find the PCB for the specified process
{
if (WAIT->NEXT->PID==J)
{
temp1=wait;
temp=wait->next;
}
}
if (temp!=null)
{
temp1->next=temp->next;//removing the selected PCB from the waiting PCB chain list
temp->pstatus=ready;//change the PCB status of the selected process to the Ready state
}
Insert (ready,temp);//Inserts the selected PCB into the Ready PCB link list
}
void Print (PCB * head)
{
PCB * temp=head->next;
if (head->next==null)
{
printf ("It is NULL");
Return
}
for (; temp!=null;temp=temp->next)
{
printf ("p<%d>", temp->pid);
}
}
void Display (PCB * r)
{
for (; r->next!=null;r=r->next)
{
printf ("\np<%d> Priority:%d", r->next->pid,r->next->priority);
}
}
int main ()
{
int n,k=1,j;
struct PCB * ready_list=null,* wait_list=null,* run=null;
wait_list= (struct PCB *) malloc (sizeof (struct PCB));
wait_list->next=null;
printf ("Please enter the number of processes you are preparing to create:");
scanf ("%d", &n);
Ready_list=create (n);//Create a process based on user input
Run=readytorun (ready_list);//Select the highest-priority process from the Ready PCB list and go to the CPU to run
Srand ((unsigned) time (NULL));//Generate a random seed
while (k!=0)
{
int i = rand ()% 4;//Generate a random number in 0-3, 0 means exit
K=i;
printf ("\n================= process dispatch ==================");
if (run!=null)
{
run->pstatus=running;
printf ("\np<%d> is Running ...", run->pid);
}
printf ("\nthe ready:");
Print (ready_list);
printf ("\nthe Wait:");
Print (wait_list);
Switch (i)
{
Case 1:
run->pstatus=ready;
Insert (Ready_list,run);//Convert the current running process to a ready state
Run=readytorun (ready_list);//system automatically selects high-priority processes from the Ready PCB list to run
Break
Case 2:
run->pstatus=wait;
Insert (Wait_list,run);//Convert the current running state's process to a waiting state
Run=readytorun (ready_list);//system automatically selects high-priority processes from the Ready PCB list to run
Break
Case 3:
printf ("\ n input wait state (wait) conversion to ready" process identification number: ");//Convert specified wait state process to ready state
scanf ("%d", &j);
Blocktoready (READY_LIST,WAIT_LIST,J);
Break
Default:break;
}
}
Scheduled end, output information about the creation process
printf ("\ n process simulation dispatch end \ n");
Display (wait_list);
Display (ready_list);
printf ("\np<%d> priority:%d \ n", run->pid,run->priority);
}
PCB Management and Scheduling