• [操作系统作业]os


    View Code
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <sys/types.h>
      4 #include <unistd.h>
      5 #include <string.h>
      6 #include  <sys/stat.h>
      7 #include  <fcntl.h>
      8 #include  <error.h>
      9 #include  <wait.h>
     10 #define PSIZE 2
     11 #define total_instruction 10
     12 #define frame_num 4
     13 
     14 int Acess_Series[total_instruction];//存访问页面序列
     15 struct one_frame {
     16    int page_no;
     17    char flag;
     18 };
     19 struct one_frame M_Frame[frame_num];
     20 //这里frame_num是给一个进程分配的最大的内存页面数
     21 int isExist(int page){
     22   int i;
     23   for(i = 0; i < frame_num; i++){
     24     if(M_Frame[i].page_no==page)
     25       return 1;
     26   }
     27   return 0;
     28 };
     29 int getPagePtr(int page){
     30   int i;
     31   for(i = 0; i < frame_num; i++){
     32     if(M_Frame[i].page_no==page)
     33       return i;
     34   }
     35   return 0;
     36 };
     37 void printMsg(int full_ptr){
     38   int i;
     39   for(i = 0; i < full_ptr; i++){
     40     printf("%d ",M_Frame[i].page_no);
     41   }
     42   printf("\n");
     43 }
     44 
     45 
     46 int main(){
     47   int i;
     48   //随机产生内存访问页面序列,存于数组Acess_Series[total_instruction]中
     49   srand(time(NULL));
     50   for(i = 0; i < total_instruction; i++){
     51     Acess_Series[i] = rand()%7 + 1;//1 ~ 7
     52   }
     53   printf("the pages that will be touch:\n");
     54   for(i = 0; i < total_instruction; i++){
     55     printf("%d ",Acess_Series[i]);
     56   }
     57   printf("   最大的内存页面数:%d \n",frame_num);
     58   pid_t pid_1,pid_2;
     59   if((pid_1=fork()) == -1){
     60     printf("error in fork()!");
     61     exit(1);
     62   }else if(pid_1 == 0){
     63     printf("FIFO child pid:%d , my father pid:\n",getpid(),getppid());
     64     int full_ptr = 0;
     65     int ming_num = 0,que_num = 0;
     66     for(i = 0; i < total_instruction; i++){
     67       if(isExist(Acess_Series[i])){
     68         ming_num++;
     69       }else{
     70         que_num++;//看M_Frame[]中有无空闲页面,如果有,装入待访问页,并统计缺页情况,缺页次数加1
     71         if(full_ptr < frame_num){//isFull(M_Frame)
     72           M_Frame[full_ptr].page_no = Acess_Series[i];
     73           M_Frame[full_ptr].flag = 'y';
     74           full_ptr++; 
     75         }else{//如果M_Frame[]中的所有页面均被占满,则淘汰M_Frame[0],装入待访问页,重新调整各页面在数组中的位置。并统计缺页情况,缺页次数加1
     76           int j;
     77           printf("ss\n");
     78           for(j = 1; j < frame_num; j++){
     79             M_Frame[j - 1].page_no = M_Frame[j].page_no;
     80             M_Frame[j - 1].flag = M_Frame[j].flag;
     81           }
     82           M_Frame[j - 1].page_no = Acess_Series[i];
     83           M_Frame[j - 1].flag = 'y';
     84         }
     85       }
     86       printMsg(full_ptr);
     87     }
     88     printf("FIFO child end, ming : %d,que : %d.\n",ming_num,que_num);
     89     exit(0);
     90   }else{
     91     if((pid_2=fork()) == -1){
     92       printf("error in fork()!");
     93       exit(1);
     94     }else if(pid_2 == 0){
     95       printf("LRU child pid:%d , my father pid:\n",getpid(),getppid());
     96       int full_ptr = 0;
     97       int ming_num = 0,que_num = 0;
     98       for(i = 0; i < total_instruction; i++){
     99         if(isExist(Acess_Series[i])){
    100           ming_num++; 
    101           int page_ptr = getPagePtr(Acess_Series[i]);
    102           int j;
    103           struct one_frame frame_temp;
    104           frame_temp.page_no = M_Frame[page_ptr].page_no;
    105           frame_temp.flag = M_Frame[page_ptr].flag;
    106           for(j = page_ptr+1; j < full_ptr; j++){
    107             M_Frame[j - 1].page_no = M_Frame[j].page_no;
    108             M_Frame[j - 1].flag = M_Frame[j].flag;
    109           }
    110           M_Frame[j - 1].page_no = frame_temp.page_no;
    111           M_Frame[j - 1].flag = frame_temp.flag;
    112         }else{
    113           que_num++;//看M_Frame[]中有无空闲页面,如果有,装入待访问页,并统计缺页情况,缺页次数加1
    114           if(full_ptr < frame_num){//isFull(M_Frame)
    115             M_Frame[full_ptr].page_no = Acess_Series[i];
    116             M_Frame[full_ptr].flag = 'y';
    117             full_ptr++; 
    118           }else{//如果M_Frame[]中的所有页面均被占满,则淘汰M_Frame[0],装入待访问页,重新调整各页面在数组中的位置。并统计缺页情况,缺页次数加1
    119             int j;
    120             for(j = 1; j < frame_num; j++){
    121               M_Frame[j - 1].page_no = M_Frame[j].page_no;
    122               M_Frame[j - 1].flag = M_Frame[j].flag;
    123             }
    124             M_Frame[j - 1].page_no = Acess_Series[i];
    125             M_Frame[j - 1].flag = 'y';
    126           }
    127         }
    128         printMsg(full_ptr);
    129       }
    130       printf("LRU child end, ming : %d,que : %d.\n",ming_num,que_num);
    131       exit(0);
    132     }else{//等待子进程执行结束,退出。
    133       pid_t pid;
    134       while(i < PSIZE){
    135         if((pid = wait(0)) < 0){
    136           printf("error in waitepid.\n");
    137           exit(1);
    138         }else{
    139           printf("child process %d is endreturn.\n",pid);
    140         }
    141         i++;
    142       }
    143       exit(0);
    144     }
    145   }
    146 }
  • 相关阅读:
    springboot:springboot初识(相关概念、新建项目)
    ssm项目无法加载静态资源
    js:初识(相关概念、js代码的书写位置、注释方式、输入输出语句)
    lucene:索引维护(删除、更新、查询)
    数据库连接池:Durid(执行流程、工具类)
    redis:HyperLogLog&发布订阅(HyperLogLog的概念和命令、redis的发布订阅)
    redis:zset(赋值、取值、删除、修改分数)
    css:css3新特性(过渡)
    css:css3新特性(盒子模型的box-sizing属性、图片模糊处理、calc函数)
    css:css3新特性(属性选择器、结构伪类选择器)
  • 原文地址:https://www.cnblogs.com/wanping/p/2537322.html
Copyright © 2020-2023  润新知