⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 priority.cpp

📁 处理机优先级管理程序 此源码为操作系统设计与实践的实验作业
💻 CPP
字号:
#include <stdio.h> 
#include <stdlib.h> 

#define N 20
#define getpch(type) (type*)malloc(sizeof(type)) 

typedef struct pcb          /* 进程控制块定义 */ 
{ 
  char name[N];             /*进程名*/
  int priority;             /*优先级*/
  int runtime;              /*运行时间*/
  char state;               /*进程状态*/
  struct pcb* next;         /*链接指针*/
}PCB; 
PCB *ready=NULL;            /*定义就绪队列指针*/
PCB *p;                     /*定义当前运行进程指针*/
FILE *f;


void Input();               /* 建立进程函数*/
void Sequence();            /* 建立进程优先级排序函数*/
void Show();                /* 建立进程显示函数*/
void cheak();               /* 建立进程查看即运行函数 */
void Execute();             /* 建立进程执行函数*/ 
void Cancel();              /* 建立进程撤消函数*/
void main();                /* 建立主函数*/ 
int Space();                /* 建立进程运行数函数*/ 


void Input()                  /* 建立进程函数*/ 
{ 
  int i,num; 
  printf("\n How many processes do you want to run:"); /*输入要运行的进程数*/
  scanf("%d",&num); 
  for(i=0;i<num;i++)          /*输入PCB信息*/
   { 
      printf("\n   Input Process No.%d:\n",i+1);  
      p=getpch(PCB); 
      printf("                           Pname:");   
      scanf("%s",p->name); 
      printf("                        priority:"); 
      scanf("%d",&p->priority); 
      printf("                         runtime:"); 
      scanf("%d",&p->runtime); 
      p->state='W';       /*置进程状态为就绪*/
      p->next=NULL; 
      Sequence();  
   } 
} 

  
void Sequence()               /* 建立进程优先级排序函数*/ 
{ 
  PCB *p1, *p2; 
  int insert=0; 
  if((ready==NULL)||((p->priority)<=(ready->priority))) /*优先级最高者,插入队首*/ 
    { 
      p->next=ready; 
      ready=p; 
    } 
  else                       /* 比较进程优先级,插入适当的位置中*/ 
    { 
      p1=ready; 
      p2=p1->next; 
      while(p2!=NULL) 
        { 
            if((p->priority)<=(p2->priority)) /*若插入进程比当前进程优先数高,*/ 
               {                                  /*插入到当前进程前面*/ 
                   p->next=p2; 
                   p1->next=p; 
                   p2=NULL; 
                   insert=1; 
                   p->state='W';
               } 
            else                /* 插入进程优先数最低,则插入到队尾*/ 
               { 
                   p1=p1->next; 
                   p2=p2->next;
                   p->state='W'; 
               } 
       } 
     if(insert==0) p1->next=p;
     p->state='W';
   } 
} 


void Show(PCB * pr)             /*建立进程显示函数*/ 
{ 
  printf("\n pname \t state \t runtime  \t priority \n");
  fprintf(f,"\n pname \t state \t runtime  \t priority \n");  

  printf("    %s\t",pr->name); 
  fprintf(f,"    %s\t",pr->name); 

  printf("    %c\t",pr->state);
  fprintf(f,"    %c\t",pr->state);
  
  printf("      %d\t",pr->runtime); 
  fprintf(f,"      %d\t",pr->runtime); 

  printf("               %d\t",pr->priority); 
  fprintf(f,"              %d\t",pr->priority); 

  printf("\n"); 
  fprintf(f,"\n");
} 
 
 
void Check()                     /* 建立进程查看及运行函数 */ 
{ 
  PCB* pr; 
  printf("\n Now the Running process is: %s",p->name);    /*查看当前运行进程*/ 
  fprintf(f,"\n Now the Running process is: %s",p->name);
  Show(p); 
  pr=ready;
  printf("\n *******************************************\n"); 
  printf("\n Now the waiting list is:\n");            /*查看就绪队列状态*/ 
  fprintf(f,"\n Now the waiting list is:\n");
  while(pr!=NULL) 
   { 
     Show(pr); 
     pr=pr->next; 
   } 
} 


void Execute()              /* 建立进程执行函数*/ 
{ 
  (p->runtime)--;          /*当前运行进程运行时间减1*/
  (p->priority)++;         /*当前运行进程优先级加1*/
  if(p->runtime==0) 
  {
	p->state='C';          /*置进程状态为完成*/
    Cancel(); 
  }
  else 
  { 
     p->state='R'; 
     Sequence(); 
  } 
} 

void Cancel()              /*建立进程撤消函数*/ 
{ 
   printf("\n Process [%s] finished.\n",p->name); 
   fprintf(f,"\n Process [%s] finished.\n",p->name); 
   free(p); 
} 

 
int Space()               /* 建立处理机运行数函数*/    
{ 
  int k=0; PCB* pr=ready; 
  while(pr!=NULL) 
    { 
     k++; 
     pr=pr->next; 
    } 
  return(k); 
} 


void main()                /*主函数*/ 
{ 
  int num,i=0; 
  char ch;
  f=fopen("result.txt","w"); 
  Input(); 
  num=Space(); 
  while((num!=0)&&(ready!=NULL)) 
   { 
     ch=getchar(); 
     i++; 
     printf("\n The running number:%d \n",i); 
     fprintf(f,"\n The running number:%d \n",i);
     p=ready; 
     ready=p->next; 
     p->next=NULL; 
     p->state='R';      /*置进程状态为运行*/
     Check(); 
     Execute();
     printf("\n Press any key to continue......\n"); 
     fprintf(f,"\n Press any key to continue......\n"); 
     ch=getchar(); 
   } 
  printf("\n\n processes have finished.\n"); 
  fprintf(f,"\n\n processes have finished.\n");
  ch=getchar(); 
  fclose(f);
} 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -