• 大小: 3KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: C/C++
  • 标签:

资源简介

按剥夺式优先数法对三个进程P1,p2,p3进行模拟调度,各进程的优先数静态设置,其中P1的优先数最高,P3的优先数最低。每个进程都处于执行E(execute),就绪R(ready)和等待W(wait)三种状态之一,并假定初始状态均为R.。

资源截图

代码片段和文件信息

#include “stdio.h“
#include “stdlib.h“
#include “string.h“
#include “malloc.h“
#include “time.h“

#define getpch(type) (type*)malloc(sizeof(type)) //控制输入流
#define NULL 0 
struct pcb{ /* 定义进程控制块PCB */ 
char name[10]; //进程名
char state; //进程状态
int super; //进程优先级
struct pcb* next; 
}*ready=NULL *p;//ready表示就绪队列,p表示当前要插入的队列

typedef struct pcb PCB; 
void sort();

void create()  /*手动输入进程*/
{    
   int inum; 
   system(“CLS“); //清屏 
   printf(“\n 请输入进程数:“); 
   scanf(“%d“&num); 

   for(i=0;i { 
  p=getpch(PCB); 
  printf(“\n 输入进程名:“); 
  scanf(“%s“p->name); 
  printf(“\n输入进程优先数:“); 
  scanf(“%d“&p->super); 
  printf(“\n进程状态: R“);
  printf(“\n“); 
  p->state=‘W‘; //当前要插入就绪队列的进程控制块处于等待状态
  p->next=NULL; 
  sort(); //调用sort函数
}    




void sort() /* 建立对进程进行优先级排列函数*/ 



PCB *first *second; 
int flag=0; //flag标志,flag=0表示将进程插入到就绪队列首后面
if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者插入队首*/ 

p->next=ready; 
ready=p; 

else /* 进程比较优先级插入适当的位置中*/ 

first=ready; 
second=first->next ;
while(second!=NULL) 


if((p->super)>(second->super)) /*若插入进程比当前进程优先数大插入到当前进程前面*/
{  
p-

评论

共有 条评论

相关资源