• 进程调度模拟


    操作系统原理进程调度模拟,基本思想:时间片轮转+优先级调度+可抢占,总是运行最高优先级进程(0~sizeof(int))

    进程队列采用链表形式进行组织,进程数据结构如下:pro_id-->进程控制号,priority-->进程优先级,time_slice-->进程分配的时间片,*next-->指向下一进程

    进程组织形式为单链表,没有采用双链表,在进行进程调度(就绪态提升到运行态)和唤醒进程(阻塞态转为就绪态)时进行了一下特殊处理:1、优先级调度,通过pcb_ready *getHighRdy(int *head_flag)返回最高优先级进程控制块指针,添加了一个head_flag标志位,返回最高优先级进程前一个进程控制块地址,便于操作,head_flag标志位用来标记是否为头结点进行处理;2、通过进程控制号pri_id唤醒进程,同理根据pcb_blocked *findWaitById(int id,int *head_flag)查找所对应id前一个进程控制块的地址

    My OS Run a Pace 为运行一个系统时间片,运行一个系统时钟即系统滴答,若此时没有更高优先级的进程添加到就绪队列且当前运行进程的时间片没有运行完,就继续执行当前运行进程,否则从就绪队列中选择最高优先级的进程提升到运行态,支持可抢占式的

    运行环境:codeblocks

    整体代码如下:

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #define PACE 1  //系统滴答时钟
      5 
      6 typedef struct pcb          //处于运行状态的进程
      7 {
      8     int pro_id;     //进程控制号
      9     int priority;
     10     int time_slice; //时间片
     11     struct pcb *next;
     12 }pcb;
     13 
     14 typedef pcb pcb_running;
     15 typedef pcb pcb_ready;
     16 typedef pcb pcb_blocked;
     17 
     18 pcb_running *pcb_run;
     19 pcb_blocked *pcb_wait;
     20 pcb_ready   *pcb_rdy;
     21 
     22 int pid_no=1;                       //添加的进程id控制号
     23 
     24 void pcb_init();
     25 void print_menu();
     26 void process_create();
     27 void addProcessToRdy_Blocked(pcb *rdy_new,int flag);//0:添加pcb_rdy 1:添加pcb_wait
     28 void runToBlocked();                //将当前运行进程挂起
     29 void blockedToRdy();          //根据进程控制块中id号将阻塞进程唤醒
     30 void swByPrio();                    //根据优先级进行进程调度
     31 pcb_ready *getHighRdy(int *head_flag);               //优先级最高的进程控制块信息
     32 pcb_blocked *findWaitById(int id,int *head_flag);         //根据id号找到要唤醒的阻塞进程
     33 void printProcessInfo();
     34 void do_exit();                    //释放进程占用内存块
     35 
     36 int main()
     37 {
     38     char choice;
     39     pcb_init();
     40     while(1){
     41         print_menu();
     42         choice=getchar();
     43         if(choice>='0'&&choice<='5'){
     44             switch(choice){
     45             case '1': process_create(); break;
     46             case '2': swByPrio();break;
     47             case '3': runToBlocked();break;
     48             case '4': blockedToRdy();break;
     49             case '5': printProcessInfo();break;
     50             case '0': do_exit(); exit(0);
     51             default: break;
     52             }
     53             getchar();      //读入enter键
     54         }
     55         else
     56             printf("
    Please enter number 0~5:
    ");
     57     }
     58 
     59     return 0;
     60 }
     61 void pcb_init()
     62 {
     63     pcb_run=NULL;
     64     pcb_wait=NULL;
     65     pcb_rdy=NULL;
     66 }
     67 void print_menu()
     68 {
     69     printf("
    ");
     70     printf("1 - Create Process 
    ");
     71     printf("2 - My OS run a pace 
    ");
     72     printf("3 - Blocked the running process 
    ");
     73     printf("4 - Wakeup process by id 
    ");
     74     printf("5 - Display process info 
    ");
     75     printf("0 - Exit
    ");
     76 }
     77 void process_create()
     78 {
     79     int priority,time_slice;
     80     pcb_ready *rdy_new;
     81     rdy_new=(pcb_ready*)malloc(sizeof(pcb_ready));
     82     if(rdy_new==NULL)
     83         exit(-1);
     84 
     85     printf("Please enter the priority、time_slice of the process
    ");
     86     scanf("%d%d",&priority,&time_slice);
     87 
     88     rdy_new->pro_id=pid_no++;
     89     rdy_new->priority=priority;
     90     rdy_new->time_slice=time_slice;
     91     rdy_new->next=NULL;
     92 
     93     addProcessToRdy_Blocked(rdy_new,0);
     94 }
     95 void addProcessToRdy_Blocked(pcb *rdy_new,int flag)
     96 {
     97     if(flag){
     98         if(pcb_wait==NULL) //就绪进程队列为空
     99             pcb_wait=rdy_new;
    100         else{             //将添加进来的进程直接添加到队首
    101             rdy_new->next=pcb_wait->next;
    102             pcb_wait->next=rdy_new;
    103         }
    104     }
    105     else{
    106         if(pcb_rdy==NULL) //就绪进程队列为空
    107             pcb_rdy=rdy_new;
    108         else{             //将添加进来的进程直接添加到队首
    109             rdy_new->next=pcb_rdy->next;
    110             pcb_rdy->next=rdy_new;
    111         }
    112     }
    113 }
    114 void runToBlocked()
    115 {
    116     addProcessToRdy_Blocked(pcb_run,1);
    117     pcb_run=NULL;
    118 }
    119 pcb_blocked *findWaitById(int id,int *head_flag)
    120 {
    121     pcb_blocked *wait,*w;
    122     if(pcb_wait==NULL)
    123         wait=NULL;
    124     else if(pcb_wait->pro_id==id){
    125          wait=pcb_wait;
    126          *head_flag=1;
    127     }
    128     else{
    129         w=pcb_wait;
    130         while(w->next){
    131             if(w->next->pro_id==id){
    132                 wait=w;
    133                 *head_flag=0;
    134                 break;
    135             }
    136             w=w->next;
    137         }
    138     }
    139     return wait;
    140 }
    141 void blockedToRdy()
    142 {
    143     pcb_blocked *blocked;
    144     int process_id,head_flag=0;
    145     printf("Please enter the ready process id that you want to wakeup 
    ");
    146     scanf("%d",&process_id);
    147     blocked=findWaitById(process_id,&head_flag);         //返回id的头一个结点
    148     if(blocked==NULL){              //没有找到对应id的阻塞进程
    149         printf("The blocked process id is wrong 
    ");
    150     }
    151     else if(head_flag)             //头结点
    152         pcb_wait=pcb_wait->next;
    153     else                           //blocked指向唤醒进程前一个进程地址
    154         blocked->next=blocked->next->next;
    155 
    156     addProcessToRdy_Blocked(blocked,0);
    157 
    158 }
    159 pcb_ready *getHighRdy(int *head_flag)
    160 {
    161     pcb_ready *rdy,*r;
    162     if(pcb_rdy==NULL)
    163         rdy=NULL;
    164     else{
    165         r=rdy=pcb_rdy;
    166         *head_flag=1;
    167         while(r->next){
    168             if(rdy->priority>=r->next->priority){
    169                 rdy=r;              //返回最高优先级控制块前一个进程控制块
    170                 *head_flag=0;
    171             }
    172             r=r->next;
    173         }
    174     }
    175     //printf("%d
    ",rdy->pro_id);
    176     if(pcb_run!=NULL&&rdy!=NULL){
    177         if(rdy->next->priority>=pcb_run->priority)//就绪队列中最高优先级的进程小于运行进程的优先级
    178             rdy=NULL;
    179     }
    180     return rdy;
    181 }
    182 void swByPrio()
    183 {
    184     pcb_ready *rdy,*pcb_temp;
    185     int head_flag=0;
    186     if(pcb_run!=NULL){
    187         pcb_run->time_slice-=PACE;
    188         if(pcb_run->time_slice<=0){
    189             free(pcb_run);
    190             pcb_run=NULL;
    191             printf("The process has already finished 
    ");
    192         }
    193     }
    194 
    195     rdy=getHighRdy(&head_flag);
    196     if(rdy==NULL)
    197         printf("the running priority is highest or ready queue is null
    ");
    198     else if(head_flag){                 //运行的进程为就绪队列头结点
    199         pcb_temp=pcb_run;
    200         pcb_run=rdy;
    201         pcb_rdy=pcb_rdy->next;
    202 
    203         pcb_run->next=NULL;
    204 
    205         if(pcb_temp!=NULL)
    206             addProcessToRdy_Blocked(pcb_temp,0);       //将运行进程添加到就绪队列
    207     }
    208     else {                              //运行的进程非头结点
    209         pcb_temp=pcb_run;
    210         pcb_run=rdy->next;
    211         rdy->next=rdy->next->next;      //在就绪队列中删除转为运行的进程
    212 
    213         pcb_run->next=NULL;
    214         if(pcb_temp!=NULL)
    215             addProcessToRdy_Blocked(pcb_temp,0);       //将运行进程添加到就绪队列
    216     }
    217 }
    218 void printProcessInfo()
    219 {
    220     pcb_ready *rdy;
    221     pcb_blocked *wait;
    222 
    223     printf("run process:
    ");
    224     if(pcb_run)
    225         printf("id:%2d	priority:%2d	time_slice:%2d
    ",pcb_run->pro_id,pcb_run->priority,pcb_run->time_slice);
    226     else
    227         printf("the run queue is null
    ");
    228 
    229     rdy=pcb_rdy;
    230     printf("Ready process:
    ");
    231     if(rdy){
    232         while(rdy){
    233         printf("id:%2d	priority:%2d	time_slice:%2d
    ",rdy->pro_id,rdy->priority,rdy->time_slice);
    234         rdy=rdy->next;
    235         }
    236     }
    237     else
    238         printf("the ready queue is null
    ");
    239 
    240     wait=pcb_wait;
    241     printf("wait process:
    ");
    242     if(wait!=NULL){
    243         while(wait){
    244         printf("id:%2d	priority:%2d	time_slice:%2d
    ",wait->pro_id,wait->priority,wait->time_slice);
    245         wait=wait->next;
    246         }
    247     }
    248     else
    249         printf("the wait queue is null
    ");
    250 }
    251 void do_exit()
    252 {
    253     pcb *pcb_next;
    254     if(pcb_run!=NULL)
    255         free(pcb_run);
    256 
    257     if(pcb_rdy!=NULL){
    258         while(pcb_rdy){
    259             pcb_next=pcb_rdy->next;
    260             free(pcb_rdy);
    261             pcb_rdy=pcb_next;
    262         }
    263     }
    264 
    265     if(pcb_wait!=NULL){
    266         while(pcb_wait){
    267             pcb_next=pcb_wait->next;
    268             free(pcb_wait);
    269             pcb_wait=pcb_next;
    270         }
    271     }
    272 }

     

  • 相关阅读:
    Flutter—代码提示不区分大小写
    Flutter—语法解析
    在Android Studio中创建flutter工程
    iOS热重载
    在终端中创建flutter程序
    Flutter的安装以及一些坑
    iTerms的配置和安装
    QT QFileSystemWatcher 文件监控 [转载]
    QT SQL 操作SQLite数据库
    QT使用QtCharts绘制图形
  • 原文地址:https://www.cnblogs.com/Karma-wjc/p/4095427.html
Copyright © 2020-2023  润新知