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

📄 操作系统实习1.cpp

📁 优先数处理器调度算法实现
💻 CPP
字号:
#define N 10    
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct pcb      /*进程控制块定义*/
{
     char name[10];     /*进程的名字*/
     int needtime;      /*进程运行需要的时间*/
     int usetime;       /*进程已经运行的时间*/
     int prior;         /*进程运行的优先数*/
     char state;        /*进程的状态*/ 
     struct pcb *next;  /*指向下一个进程的指针*/
};
typedef struct pcb PCB;
typedef PCB *point;
point head,tail,q,r;       /*定义首指针、尾指针、和指针q*/

void insert(point p)     /*按优先数插入队列的函数*/
{
    point p1,p2;
    p1=head->next;
    p2=head;
    if(head->next==NULL)   /*如果还没有进程,直接插入*/ 
        {  
              head->next=p;
        }
    else{
         while((p1->next!=NULL)&&((p1->prior)>=(p->prior)))  /*循环查找近来进程的插入位置*/
          {
              p2=p1;
              p1=p1->next;
          }
        if((p1->prior)<(p->prior))    /*将p插入p2和p1中间*/
          {
              p2->next=p;
              p->next=p1;
          }
          else  {p1->next=p;}
        }
}

void input()    /*输入进程信息的函数*/
{   
    head=(struct pcb*)malloc(sizeof(struct pcb));
    head->next=NULL;
    char str[10];
    for(int i=0;i<5;i++)
    { 
      point p;
      p=(struct pcb*)malloc(sizeof(struct pcb));     /*为新的进程申请空间*/
      printf("请输入第%d个进程的名字(Q1~Q5):",i+1);
      scanf("%s",str);
      strcpy(p->name,str);
      printf("请输入进程%s需要运行的时间:",str);
      scanf("%d",&p->needtime);
      printf("请输入进程%s的进程优先数:",str);
      scanf("%d",&p->prior);
      p->usetime=0; 
      p->next=NULL; 
      p->state='R';    
      insert(p);     /*每输入一个进程就插入进程队列中*/
    }
}                 


void find()      /*找到进程循环队列的首指针、尾指针*/
{   
    point q;
    tail=(struct pcb*)malloc(sizeof(struct pcb));
    tail->next=NULL;
    q=(struct pcb*)malloc(sizeof(struct pcb));
    q->next=NULL;
    tail=head;
    for(int i=0;i<5;i++)     /*找出尾指针*/
    {
        tail=tail->next;
    }
    q=head;
    head=head->next;      /*头指针指向第一个进程*/
    tail->next=head;
    free(q);
}

void run1()       /*按优先数调度运行*/
{ 
    while(head!=NULL)
    {
        getchar();
        printf("\n进程%s正在运行...",head->name);
        head->usetime++;
        head->prior--;
        if(head->usetime==head->needtime)
        {
            head->state='E';
            printf("进程%s运行结束.",head->name);
            r=head;
            head=head->next;   /*将结束的进程从进程队列中删除*/
            free(r);
            
        } 
        else
        {   
            printf("还需要%d个运行时间.",head->needtime-head->usetime);
            if(head->next!=NULL)
               {
                  r=head;
                  head=head->next;  
                  r->next=NULL;
                  insert(r);
               }
         }
    }
}
     
void run2()   /*按时间片调度运行*/
{
    while(head->next!=NULL)
    {
        getchar();
        printf("\n进程%s正在运行...",head->name);
        head->usetime++;
        if(head->usetime==head->needtime)
        {
            head->state='E';
            printf("进程%s运行结束.",head->name);
            q=head;
            head=head->next;   /*将结束的进程从进程队列中删除*/
            tail->next=head;
            free(q);
            
        } 
        else
        {
            printf("时间片已用完,还需要%d个运行时间.",head->needtime-head->usetime);
            head=head->next;  
            tail=tail->next;
               
        }
        if(head==tail&&head->usetime==head->needtime)   /*如果是最后一个进程,则将它指向的下一个进程置为空*/
        head->next=NULL;
    }
}

void print()      /*显示*/
{
     printf("进程名    需要运行的时间      优先数            状态\n");
     point k=(struct pcb*)malloc(sizeof(struct pcb));
     k->next=NULL;
     k=head;
     for(int i=0;i<5;i++)
         {
            printf(" %s \t\t %d \t\t %d \t\t %c\n",k->name,k->needtime,k->prior,k->state); 
            k=k->next;
         } 
}     
     
int main()     /*主函数*/
{   
    int meth;
    char end;
    end='y';    
    while(end=='y')
    {
       printf("请选择你需要的处理器调度算法:\n");
       printf("-------------------------------------------\n");
       printf("      1为按优先数实现处理器调度\n");
       printf("      2为按时间片轮转法实现处理器调度\n");
       printf("-------------------------------------------\n");
       printf("请选择:");
       scanf("%d",&meth);
       printf("请输入各进程初始化信息:\n");
       printf("---------------------------------------------------\n");
       if(meth==1)     /*按优先数实现处理器调度*/
          {    printf("             按优先数实现处理器调度\n");
               input();  /*输入*/
               point q;
               q=head;
               head=head->next;      /*头指针指向第一个进程*/
               free(q);
               printf("\n运行前进程的信息:\n");
               print();
               printf("                   进程运行情况\n");
               printf("--------------------------------------------------\n");
               run1();    /*运行*/
               printf("\n");
               printf("--------------------------------------------------\n");
          } 
               
      else if(meth==2)   /*按时间片轮转法实现处理器调度*/
          {
              printf("              按时间片轮转法实现处理器调度\n");
              input();  /*输入*/
              find();
              printf("\n运行前的五个进程的信息:\n");
              print();
              printf("                   进程运行情况\n");
              printf("--------------------------------------------------\n");
              run2();    /*运行*/
              printf("\n");
              printf("--------------------------------------------------\n");
          }
    printf("\n是否还需要服务? y or n:");
    scanf("%s",&end);
    }
}          

⌨️ 快捷键说明

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