• 实验3


      1 #pragma comment(linker, "/subsystem:console")
      2 #include<stdio.h>
      3 #include<stdlib.h> 
      4 #include<conio.h> 
      5 #include<dos.h>
      6 #define getpch(type) (type*)malloc(sizeof(type)) 
      7 #define NULL 0 
      8 struct pcb
      9 { /* 定义进程控制块PCB */ 
     10     char name[10]; 
     11     char state; 
     12     int super; 
     13     int ntime; 
     14     int rtime; 
     15     struct pcb* link; 
     16 }
     17 *ready=NULL,*p; 
     18 typedef struct pcb PCB; 
     19 sort() /* 建立对进程进行优先级排列函数*/ 
     20 { 
     21     PCB *first, *second; 
     22     int insert=0; 
     23     if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/ 
     24     { 
     25         p->link=ready; 
     26         ready=p; 
     27     } 
     28     else /* 进程比较优先级,插入适当的位置中*/ 
     29     { 
     30         first=ready; 
     31         second=first->link; 
     32         while(second!=NULL) 
     33         { 
     34             if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/     
     35             { /*插入到当前进程前面*/ 
     36                 p->link=second; 
     37                 first->link=p; 
     38                 second=NULL; 
     39                 insert=1;             
     40             } 
     41             else /* 插入进程优先数最低,则插入到队尾*/     
     42             { 
     43                 first=first->link; 
     44                 second=second->link; 
     45             }         
     46         } 
     47         if(insert==0) first->link=p; 
     48     } 
     49     
     50 } 
     51 input() /* 建立进程控制块函数*/ 
     52 { 
     53     int i,num; 
     54 
     55     printf("
     请输入进程号?"); 
     56     scanf("%d",&num); 
     57     for(i=0;i<num;i++) 
     58     { 
     59         printf("
     进程号No.%d:
    ",i); 
     60         p=getpch(PCB); 
     61         printf("
     输入进程名:"); 
     62         scanf("%s",p->name); 
     63         printf("
     输入进程优先数:"); 
     64         scanf("%d",&p->super); 
     65         printf("
     输入进程运行时间:"); 
     66         scanf("%d",&p->ntime); 
     67         printf("
    "); 
     68         p->rtime=0;p->state='w'; 
     69         p->link=NULL; 
     70         sort(); /* 调用sort函数*/ 
     71     } 
     72 } 
     73 int space() 
     74 { 
     75     int l=0; PCB* pr=ready; 
     76     while(pr!=NULL) 
     77     { 
     78         l++; 
     79         pr=pr->link; 
     80     } 
     81     return(l); 
     82 } 
     83 disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ 
     84 { 
     85     printf("
     qname 	 state 	 super 	 ndtime 	 runtime 
    "); 
     86     printf("|%s	",pr->name); 
     87     printf("|%c	",pr->state); 
     88     printf("|%d	",pr->super); 
     89     printf("|%d	",pr->ntime); 
     90     printf("|%d	",pr->rtime); 
     91     printf("
    "); 
     92 } 
     93 check() /* 建立进程查看函数 */ 
     94 { 
     95     PCB* pr; 
     96     printf("
     **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ 
     97     disp(p); 
     98     pr=ready; 
     99     printf("
     ****当前就绪队列状态为:
    "); /*显示就绪队列状态*/ 
    100     while(pr!=NULL) 
    101     { 
    102         disp(pr); 
    103         pr=pr->link; 
    104     } 
    105 } 
    106 destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ 
    107 { 
    108     printf("
     进程 [%s] 已完成.
    ",p->name); 
    109     free(p); 
    110 } 
    111 running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ 
    112 { 
    113     (p->rtime)++; 
    114     if(p->rtime==p->ntime) 
    115         destroy(); /* 调用destroy函数*/ 
    116     else 
    117     { 
    118         (p->super)--; 
    119         p->state='w'; 
    120         sort(); /*调用sort函数*/ 
    121     } 
    122 } 
    123 main() /*主函数*/ 
    124 { 
    125     int len,h=0; 
    126     char ch; 
    127     input(); 
    128     len=space(); 
    129     while((len!=0)&&(ready!=NULL)) 
    130     { 
    131         ch=getchar(); 
    132         h++; 
    133         printf("
     The execute number:%d 
    ",h); 
    134         p=ready; 
    135         ready=p->link; 
    136         p->link=NULL; 
    137 p->state='R'; 138 check(); 139 running(); 140 printf(" 按任一键继续......"); 141 ch=getchar();
    142 } 143 printf(" 进程已经完成. "); 144 ch=getchar(); 145 }


    心得:还是参考网上的,然后修改了些错误,然后结合书本验证。对于学习不上心的我,在这过程中还是学到了很多,如利用一些快捷键能提高效率还有一些方法
  • 相关阅读:
    hdu1875(最小生成树prime)
    hdu1839(最小生成树)
    poj2739(尺取法+质数筛)
    poj2100(尺取法)
    codeforces-div2-449-B
    gym-101350M
    gym-10135I
    gym-101350H
    gym-101350D
    hdu 5569
  • 原文地址:https://www.cnblogs.com/cjy40/p/4502541.html
Copyright © 2020-2023  润新知