SHARE
TWEET

Untitled

a guest Nov 19th, 2019 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int proc_create_vm(struct Kernel * kernel, int size) {
  2.     // Fill your codes below.
  3.  
  4.     int check_valid(int size,struct Kernel * kernel){
  5.         // A process whose memory size larger than the limit, return failure flag
  6.         if(VIRTUAL_SPACE_SIZE<size){
  7.             return -1;
  8.         }
  9.     }
  10.  
  11.     int check_full(int size,struct Kernel * kernel){
  12.         // Check whether the table is full
  13.         if(kernel->allocated_pages == KERNEL_SPACE_SIZE/PAGE_SIZE){
  14.             return -1;
  15.         }
  16.     }
  17.  
  18.     // Return how many pages I need
  19.     int pages_demanded_func(int size){
  20.         int pages_num = size/PAGE_SIZE;
  21.         // The size isn't divisible
  22.         if (size % PAGE_SIZE != 0){
  23.             return (pages_num+1);
  24.         }
  25.         // The size is divisible
  26.         else{
  27.             return (((size+PAGE_SIZE)/PAGE_SIZE)-1);
  28.         }
  29.     }
  30.  
  31.     int process_id = 0;
  32.     //check whther I don't have enough memory to allocate
  33.     if(check_valid(size,kernel) == -1 || check_full(size,kernel) == -1){
  34.         // Fail to create vm
  35.         return -1;
  36.     }
  37.     else if(check_valid(size,kernel) != -1){
  38.         for(process_id;process_id<MAX_PROCESS_NUM;process_id++){
  39.             if(kernel->running[process_id] == 0){
  40.                 // Update allocated_pages
  41.                 kernel->allocated_pages = kernel->allocated_pages + pages_demanded_func(size);
  42.                 // Malloc memory location for table
  43.                 kernel->mm[process_id].page_table = (struct PTE *)malloc(sizeof(struct PTE) * pages_demanded_func(size));
  44.                 // Update size
  45.                 kernel->mm[process_id].size = size;
  46.  
  47.                 for(int index=0;index<pages_demanded_func(size);index++){
  48.                     kernel->mm[process_id].page_table[index].present = 0;
  49.                     kernel->mm[process_id].page_table[index].PFN = -1;
  50.                 }
  51.                 kernel->running[process_id] = 1;
  52.                 return process_id;
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58.  
  59.  
  60. ####### read/write 差唔多, 做左write先, read 係調返轉
  61. ####### 下面係錯錯地, 改緊
  62.  
  63.  
  64. int vm_write(struct Kernel * kernel, int pid, char * addr, int size, char * buf){
  65.     // Fill your codes below.
  66.     int existPages;
  67.  
  68.     for(int i=0;i<KERNEL_SPACE_SIZE;i++){
  69.         if(kernel->mm[pid].page_table[i/PAGE_SIZE].present == 0){
  70.             existPages = i;
  71.             break;
  72.         }
  73.     }
  74.  
  75.     for(int i=(int)addr;i<(int)addr+size;i++){
  76.         if(kernel->occupied_pages[existPages] == 0){
  77.             if(kernel->mm[pid].page_table[i/PAGE_SIZE].present == 0){
  78.                 kernel->mm[pid].page_table[i/PAGE_SIZE].present = 1;
  79.                 kernel->mm[pid].page_table[i/PAGE_SIZE].present == 0;
  80.                 kernel->occupied_pages[existPages] = 1;
  81.                 existPages++;
  82.             }
  83.         }
  84.         for(int j=0;j<size;j++){
  85.             buf[j] = kernel->space[kernel->mm[pid].page_table[i/PAGE_SIZE].PFN + j%PAGE_SIZE] ;
  86.         }
  87.     }
  88.  
  89.     return 0;
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top