zoukankan      html  css  js  c++  java
  • 实验三

    一,实验目的 

    用高级语言完成一个进程调度程序,以加深对进程的概念及进程调度算法的理解。 

    二,实验内容和要求

    1.2.1例题:设计一个有 N个进程并发执行的进程调度模拟程序。

    进程调度算法:采用最高优先级优先的调度算法(即把处理机分配给优先级最高的进程)算法。

    (1).   每个进程有一个进程控制块(PCB)表示。进程控制块包含如下信息:进程名、优先级、到达时间、需要运行时间、已用CPU时间、进程状态等等。

    (2).   进程的优先级及需要的运行时间可以事先人为地指定,进程的运行时间以时间片为单位进行计算。

    (3).   每个进程的状态可以是就绪 r(ready)、运行R(Running)、或完成F(Finished)三种状态之一。

    (4).   就绪进程获得 CPU后都只能运行一个时间片。用已占用CPU时间加1来表示。

    (5).   如果运行一个时间片后,进程的已占用 CPU时间已达到所需要的运行时间,则撤消该进程,如果运行一个时间片后进程的已占用CPU时间还未达所需要的运行时间,也就是进程还需要继续运行,此时应将进程的优先数减1(即降低一级),然后把它插入就绪队列等待调度。

    (6).   每进行一次调度程序都打印一次运行进程、就绪队列中各个进程的 PCB,以便进行检查。   

    (7).   重复以上过程,直到所要进程都完成为止。

    1.2.2实验题A:编写并调试一个模拟的进程调度程序,采用“最高优先数优先”调度算法对N(N不小于5)个进程进行调度。

    “最高优先级优先”调度算法的基本思想是把CPU分配给就绪队列中优先数最高的进程。

    (1). 静态优先数是在创建进程时确定的,并在整个进程运行期间不再改变。

    (2). 动态优先数是指进程的优先数在创建进程时可以给定一个初始值,并且可以按一定规则修改优先数。例如:在进程获得一次CPU后就将其优先数减少1,并且进程等待的时间超过某一时限(2个时间片时间)时增加其优先数等。 

    1.2.3实验题B:编写并调试一个模拟的进程调度程序,采用“基于时间片轮转法”调度算法对N(N不小于5)个进程进行调度。 “轮转法”有简单轮转法、多级反馈队列调度算法。

    (1). 简单轮转法的基本思想是:所有就绪进程按 FCFS排成一个队列,总是把处理机分配给队首的进程,各进程占用CPU的时间片长度相同。如果运行进程用完它的时间片后还未完成,就把它送回到就绪队列的末尾,把处理机重新分配给队首的进程。直至所有的进程运行完毕。(此调度算法是否有优先级?)

     (2). 多级反馈队列调度算法的基本思想是:

    将就绪队列分为N级(N=3~5),每个就绪队列优先数不同并且分配给不同的时间片:队列级别越高,优先数越低,时间片越长;级别越小,优先数越高,时间片越短。

    系统从第一级调度,当第一级为空时,系统转向第二级队列,.....当处于运行态的进程用完一个时间片,若未完成则放弃CPU,进入下一级队列。

    当进程第一次就绪时,进入第一级队列。

    完成设计、编码和调试工作,完成实验报告。

    三、主要程序及其解释

    复制代码
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<conio.h>
      4 #include<stdlib.h>
      5   
      6 struct Process
      7 {
      8     char name[20];
      9     int requesttime;
     10     int priority;
     11                 int arrivetime;
     12     int waittime;
     13     int runtime;
     14     int CPUtime;
     15     char status[20];
     16 };
     17 
     18 struct Process PCB[20]={0};
     19  
     20 int input(Process *PCB,int n);
     21 void output(Process *PCB,int n);
     22 int unfinished(Process *PCB,int n,int runtime);
     23 int finshed(Process *PCB,int n);
     24 void algorithm(Process *PCB,int n,int piecetime);
     25 
     26 int main(void)
     27 {
     28     int n=0;  
     29     int piecetime;
     30     n=input(PCB,n);
     31     printf("please input the time of piece:");
     32     scanf("%d",&piecetime);
     33     algorithm(PCB,n,piecetime);
     34     printf("
    ");
     35     return 0;
     36 }
     37 
     38 int intput(Process *PCB,int n)/*初始化*/
     39 {
     40     int i;
     41     printf("please input the number of process:");
     42     scanf("%d",&n);
     43     
     44     for(i=0;i<n;i++)
     45     {
     46         printf("please input the name of process:",i+1);
     47         scanf("%s",&PCB[i].name);
     48 
     49         printf("please input the priority of process:",i+1);
     50         scanf("%d",&PCB[i].priority);
     51 
     52          printf("please input the arrivetime of process:",i+1);
     53         scanf("%d",&PCB[i].arrivetime);
     54         
     55         printf("please input the requesttime of process:",i+1);
     56         scanf("%d",&PCB[i].requesttime);
     57 
     58         strcpy(PCB[i].status,"r");
     59         PCB[i].CPUtime=0;
     60         PCB[i].waittime=0;
     61     }
     62     return n;
     63 }
     64 
     65 void output(Process *PCB,int n)
     66 {
     67     int i;
     68     
     69     printf("
    ");
     70     printf("
      name"); 
     71     printf("  priority ");
     72     printf("   arrivetime");
     73     printf("    requesttime");
     74     printf("     CPUtime");
     75     printf("      status");
     76     
     77     for(i=0;i<n;i++)
     78     {
     79         printf("
    %s",PCB[i].name);
     80         printf("        %d",PCB[i].priority);
     81         printf("             %d",PCB[i].arrivetime);
     82         printf("               %d",PCB[i].requesttime);
     83         printf("              %d",PCB[i].CPUtime);
     84         printf("           %s",PCB[i].status);     
     85     }
     86 }
     87 
     88 int unfinished(Process *PCB,int n,int runtime)//找最高优先级且没运行完的进程
     89 {
     90     int i=0;
     91     int b=0;
     92     int c=0;
     93     
     94     for(;i<n;i++)
     95     {
     96         if(PCB[i].arrivetime<=runtime&&PCB[i].status[0]=='r'&&PCB[i]. priority>b)
     97         {
     98             b=PCB[i]. priority;
     99             c=i;
    100         }       
    101     }
    102     return c;
    103 }
    104 
    105 int finshed(Process *PCB,int n)//判断是否全部作业都调度完成
    106 {
    107     int i=0;
    108     int count=0;
    109     for(;i<n;i++)
    110     {
    111         if(PCB[i].status[0]=='f')
    112             count++;
    113     }
    114     if(count==n)
    115     {
    116         return 1;
    117     }
    118     return 0;
    119 }
    120 
    121 void algorithm(Process *PCB,int n,int piecetime)//主算法
    122 {
    123     int a=0;
    124     int i=0;
    125     int runtime=0;
    126     output(PCB, n);
    127     
    128     do{        
    129         a=unfinished(PCB, n,runtime);
    130         PCB[a]. priority--;
    131         PCB[a].CPUtime++;
    132         if(PCB[a]. priority<0)
    133         {         
    134             PCB[a]. priority=0;
    135         }
    136         
    137         if(PCB[a].CPUtime*piecetime>=PCB[a].requesttime)
    138         {
    139             strcpy(PCB[a].status,"f");
    140         }
    141 
    142         for(i=0;i<n;i++)
    143         {
    144             if(i==a)
    145             {
    146                 PCB[i].waittime=0;
    147 }
    148                 if(i!=a&&PCB[i].arrivetime<=runtime&&PCB[i].status[0]!='f')
    149                 {
    150                     PCB[i].waittime++;
    151                     if(PCB[i].waittime==2)
    152                     {
    153                         PCB[i].priority++;
    154                         PCB[i].waittime=0;
    155                     }
    156                 }
    157             }
    158             output(PCB, n);
    159             runtime++;
    160         }while(finshed(PCB, n)!=1);
    161     }
    162 }
    复制代码

    运行结果

    四,实验总结

    这个实验和上一个实验思路是一样的,不过注重细节表达方法。网上有的资料是链表,老师讲的上一个实验是数组,所以我就参考了指针和数组相结合,大概有5个函数块。

    还需要多练习,才能有较清晰的逻辑思路。

  • 相关阅读:
    今天本来还打算继续更新微信开发的,但是没办法接口调用次数已经达到上限了,唉
    夺命雷公狗---微信开发43----用户分组管理接口(删)
    夺命雷公狗---微信开发43----用户分组管理接口(改)
    夺命雷公狗---微信开发42----用户分组管理接口(查)
    夺命雷公狗---微信开发41----用户分组管理接口(增)
    夺命雷公狗---微信开发40----微信语言识别接口2(点歌系统)
    [LeetCode] Balanced Binary Tree
    [LeetCode] Minimum Depth of Binary Tree
    [LeetCode] Path Sum
    [LeetCode] Pascal's Triangle
  • 原文地址:https://www.cnblogs.com/baoan/p/4534997.html
Copyright © 2011-2022 走看看